| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- /**
- * 账户绑定
- */
- namespace app\api\controller\v1;
- use app\common\library\Mail;
- use app\common\model\Members;
- use think\Validate;
- use think\Session;
- use think\Db;
- class Email extends UserCenter
- {
- protected $memberModel;
- public function _initialize()
- {
- parent::_initialize();
- $this->memberModel = new Members;
- }
- /**
- * 邮箱绑定页面
- * @return mixed
- * @throws \app\api\controller\HttpResponseException
- */
- public function index()
- {
- //判断是否已经绑定邮箱
- if (!empty($this->userInfo['email'])) {
- $this->error('您已经绑定邮箱', url('v1.userCenter/account'));
- }
- return $this->fetch();
- }
- /**
- * 解绑邮箱页面
- * @return mixed
- * @throws \app\api\controller\HttpResponseException
- */
- public function resetEmail()
- {
- if (empty($this->userInfo['email'])) {
- $this->error('您还未绑定邮箱', url('v1.userCenter/account'));
- }
- //Session::set('reset_email', true);
- $this->assign('email', ($this->userInfo['email']));
- return $this->fetch();
- }
- /**
- * 发送邮箱验证邮件
- * @throws \app\api\controller\HttpResponseException
- */
- public function sendEmail()
- {
- //未绑定 接受数据 验证邮箱合法性
- if ($this->request->isPut()) {
- $email = $this->request->put('email', '', 'trim');
- if (!Validate::is($email, 'email')) {
- $this->jsonResult('', 0, '邮箱格式不正确');
- }
- $userinfo = Members::where(['username' => $this->username])->field('email')->find();
- $url = !empty($userinfo['email']) ? url('v1.Checkemail/unBindEmail', ['username' => $this->username]) :
- url('v1.Checkemail/checkEmail', ['username' => $this->username]);
- $result = (new Mail())->sendCheckMail($this->username, $email, $url);
- return $this->json('', (int)$result['status'], ($result['status'] ? '发送成功' : $result['msg']));
- }
- }
- /**
- * 发送邮箱验证邮件
- * @throws \app\api\controller\HttpResponseException
- */
- public function sendEmailCode()
- {
- if ($this->request->isPost()) { // 手机号绑定页面,发送验证码
- $data = $this->request->put();
- $runnable = true;
- $message = '';
- $code = 0;
- //1.判断验证码防止被刷
- if (true !== ($res = $this->validate($data,
- [
- ['img_code', 'require|captcha', '请输入图形验证码|图形验证码错误'],
- ['email', 'require|email', '请输入邮箱|邮箱格式不正确'],
- ]
- ))) {
- $runnable = false;
- $message = $res;
- }
- //3.发送短信验证码
- if ($runnable) {
- $userinfo = Members::where(['username' => $this->username])->field('email')->find();
- $temple = !empty($userinfo['email']) ? 'bindEmailVLEmail' : 'bindEmail';
- $result = (new Mail())->sendCodeMailByType($this->username, $data['email'],$temple);
- if (!$result['status']) {
- $message = $result['msg'];
- }
- $code = $result['status'];
- }
- return $this->json('', $code, $message);
- }
- }
- /**
- * 邮箱换绑原邮箱验证
- * @throws \app\api\controller\HttpResponseException
- */
- public function velidateCode()
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- //1.验证数据合法性
- if (true !== ($res = $this->validate($data, [
- 'sms_code|验证码' => 'require|isEmpty',
- 'email|邮箱地址' => 'require|isEmpty|email',
- ]))) {
- return $this->json([], 0, $res);
- }
- //2.验证对应的邮箱和验证码是否匹配
- $res = (new Mail())->checkCode($this->username , $data['email'] , $data['sms_code']);
- if ($res['status'] == false) {
- return $this->json([], 0, $res['msg']);
- }
- else{
- Session::set('emailVelidate', 1);
- return $this->json(['url' => url('v1.email/changeEmail')], 1, '原邮箱验证成功');
- }
- }
- }
- /**
- * 解绑邮箱页面
- * @return mixed
- * @throws \app\api\controller\HttpResponseException
- */
- public function changeEmail()
- {
- if (empty($this->userInfo['email'])) {
- $this->error('您还未绑定邮箱', url('v1.userCenter/account'));
- }
- if (!Session::get('emailVelidate')) {
- $this->error('请先进行原邮箱验证', url('v1.email/resetEmail'));
- }
- //Session::set('reset_email', true);
- $this->assign('email', ($this->userInfo['email']));
- return $this->fetch();
- }
- /**
- * 发送邮箱验证邮件
- * @throws \app\api\controller\HttpResponseException
- */
- public function sendNewEmailCode()
- {
- if ($this->request->isPost()) { // 邮箱绑定页面,发送验证码
- $data = $this->request->put();
- $runnable = true;
- $message = '';
- $code = 0;
- //1.判断验证码防止被刷
- if (true !== ($res = $this->validate($data,
- [
- ['img_code', 'require|captcha', '请输入图形验证码|图形验证码错误'],
- ['email', 'require|email', '请输入邮箱|邮箱格式不正确'],
- ]
- ))) {
- $runnable = false;
- $message = $res;
- }
- //2.检查换绑手机是否跟原绑定手机一致
- if (isset($this->userInfo['email']) && ($data['email'] == $this->userInfo['email'])) {
- $runnable = false;
- $message = '邮箱跟原绑定邮箱一样,请重新填写';
- }
- //3.发送验证码
- if ($runnable) {
- $userinfo = Members::where(['username' => $this->username])->field('email')->find();
- $temple = !empty($userinfo['email']) ? 'bindEmailVLEmail' : 'bindEmail';
- $result = (new Mail())->sendCodeMailByType($this->username, $data['email'],$temple);
- if (!$result['status']) {
- $message = $result['msg'];
- }
- $code = $result['status'];
- }
- return $this->json('', $code, $message);
- }
- }
- /**
- * 邮箱换绑操作
- * @throws \app\api\controller\HttpResponseException
- */
- public function chgBindEmail()
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- //1.验证数据合法性
- if (true !== ($res = $this->validate($data, [
- 'sms_code|短信验证码' => 'require|isEmpty',
- 'email|邮箱' => 'require|isEmpty|email',
- ]))) {
- return $this->json([], 0, $res);
- }
- //2.验证对应的邮箱和验证码是否匹配
- $res = (new Mail())->checkCode($this->username , $data['email'] , $data['sms_code']);
- if ($res['status'] == false) {
- return $this->json([], 0, $res['msg']);
- }
- //3.验证是否重复绑定
- if (isset($this->userInfo['email']) && ($data['email'] == $this->userInfo['email'])) {
- return $this->json([], 0, '邮箱跟原绑定邮箱一样,请重新填写');
- }
- //4.更新数据 记录日志
- if ($res = Members::where(['username' => $this->username])->update(['email' => $data['email'],'update_time'=>time()])) { // 插入玩家历史记录
- Db::table('cy_member_history')->insert([
- 'userid' => $this->userInfo['id'], //todo
- 'password' => '',
- 'email' => $data['email'],
- 'ip' => request()->ip(),
- 'create_time' => time(),
- ]);
- Session::delete('emailVelidate');
- return $this->json(['url' => url('v1.UserCenter/account')], 1, '邮箱换绑成功');
- //TODO 手动失效短信验证码
- }
- return $this->json([], 0, '邮箱换绑失败');
- }
- }
- /**
- * 邮箱绑定操作
- */
- public function bindEmail()
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- //1.验证数据合法性
- if (true !== ($res = $this->validate($data, [
- 'sms_code|验证码' => 'require|isEmpty',
- 'email|邮箱' => 'require|isEmpty|email',
- ]))) {
- return $this->json([], 0, $res);
- }
- //2.验证对应的手机号和验证码是否匹配
- $res = (new Mail())->checkCode($this->username , $data['email'] , $data['sms_code']);
- if ($res['status'] == false) {
- return $this->json([], 0, $res['msg']);
- }
- //4.更新数据 记录日志
- if ($res = Members::where(['username' => $this->username])
- ->update(['email' => $data['email'],'update_time'=>time()])) { // 插入玩家历史记录
- Db::table('cy_member_history')->insert([
- 'userid' => $this->userInfo['id'], //todo
- 'password' => '',
- 'email' => $data['email'],
- 'ip' => request()->ip(),
- 'create_time' => time(),
- ]);
- return $this->json([], 1, '手机号绑定成功');
- //TODO 手动失效短信验证码
- }
- return $this->json([], 0, '手机号绑定失败');
- }
- }
- // /**
- // * 验证邮箱链接地址
- // */
- // public function checkEmail()
- // {
- // $token = input('t', '', 'trim');
- // if (empty($token)) {
- // $this->error('邮箱验证地址错误');
- // }
- // //验证token
- // $res = (new Mail())->checkMailToken($this->username, $token);
- //
- // if (!$res['status']) {
- // $this->error($res['msg']);
- // }
- // $data = $res['data'];
- // //3.验证是否重复绑定
- // if (isset($this->userInfo['email']) && ($data['mail'] == $this->userInfo['email'])) {
- // $this->error('邮箱地址跟原来邮箱地址一样,不要重复绑定', url('v1.userCenter/account'));
- // }
- // //4.更新数据 记录日志
- // if ($res = Members::where(['username' => $this->username])->update(['email' => $data['mail']])) {
- // Db::table('cy_member_history')->insert([
- // 'userid' => $this->userInfo['id'], //todo
- // 'password' => '',
- // 'mobile' => $data['mail'],
- // 'ip' => request()->ip(),
- // 'create_time' => time(),
- // ]);
- //
- // //删除解绑密码状态
- // if (Session::has('reset_email')) {
- // Session::delete('reset_email');
- // }
- // (new Mail)->clearKeyByUsername($this->username);
- // return $this->success('邮箱绑定成功');
- // }
- // return $this->error('邮箱绑定失败');
- // }
- //
- //
- // /**
- // * 解绑邮箱
- // * @throws \app\api\controller\HttpResponseException
- // */
- // public function unBindEmail()
- // {
- // //删除解绑密码状态
- // if (!Session::has('reset_email')) {
- // return $this->error('参数错误', url('v1.userCenter/account'));
- // }
- // $token = input('t', '', 'trim');
- // if (empty($token)) {
- // $this->error('邮箱验证地址错误');
- // }
- // //验证token
- // $res = (new Mail())->checkMailToken($this->username, $token);
- //
- // if (!$res['status']) {
- // $this->error($res['msg']);
- // }
- // //4.更新数据 记录日志
- // if ($res = Members::where(['username' => $this->username])->update(['email' => ''])) {
- // Db::table('cy_member_history')->insert([
- // 'userid' => $this->userInfo['id'], //todo
- // 'password' => '',
- // 'email' => 'unbind',
- // 'ip' => request()->ip(),
- // 'create_time' => time(),
- // ]);
- // //删除解绑密码状态
- // if (Session::has('reset_email')) {
- // Session::delete('reset_email');
- // }
- // (new Mail)->clearKeyByUsername($this->username);
- // return $this->success('邮箱解绑成功');
- // }
- // return $this->error('邮箱解绑失败');
- // }
- }
|