userInfo['mobile'])) {// 手机已绑定,直接返回绑定中心 $this->error('您已经绑定手机号', url('v1.userCenter/index')); } return $this->fetch(); } /** * @return mixed * @throws \app\api\controller\HttpResponseException */ public function resetMobile() { if (empty($this->userInfo['mobile'])) { $this->error('用户还未绑定手机', url('v1.userCenter/account')); } $this->assign('mobile', ($this->userInfo['mobile'])); return $this->fetch(); } /** * 手机换绑原手机号验证 * @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', 'mobile|手机号' => 'require|isEmpty|mobile', ]))) { return $this->json([], 0, $res); } //2.验证对应的手机号和验证码是否匹配 $res = (new Sms())->checkCode($data['mobile'], $data['sms_code']); if ($res['status'] == false) { return $this->json([], 0, $res['msg']); } else{ Session::set('mobileVelidate', 1); return $this->json(['url' => url('v1.mobile/changeMobile')], 1, '原手机号验证成功'); } } } /** * @return mixed * @throws \app\api\controller\HttpResponseException */ public function changeMobile() { if (empty($this->userInfo['mobile'])) { $this->error('用户还未绑定手机', url('v1.userCenter/account')); } if (!Session::get('mobileVelidate')) { $this->error('请先进行原手机验证', url('v1.mobile/resetMobile')); } $this->assign('mobile', ($this->userInfo['mobile'])); return $this->fetch(); } /** * 解除手机绑定 * @throws \app\api\controller\HttpResponseException */ public function unBindMobile() { if ($this->request->isPost()) { $data = $this->request->post(); //1.验证数据合法性 if (true !== ($res = $this->validate($data, [ 'sms_code|短信验证码' => 'require|isEmpty', 'mobile|手机号' => 'require|isEmpty|mobile', ]))) { return $this->json([], 0, $res); } //2.验证对应的手机号和验证码是否匹配 $res = (new Sms())->checkCode($data['mobile'], $data['sms_code']); if ($res['status'] == false) { return $this->json([], 0, $res['msg']); } //3.验证是否重复绑定 if (isset($this->userInfo['mobile']) && ($data['mobile'] == $this->userInfo['mobile'])) { return $this->json([], 0, '手机号跟原绑定手机号一样,请重新填写'); } //4.更新数据 记录日志 if ($res = Members::where(['username' => $this->username])->update(['mobile' => $data['mobile'],'update_time'=>time()])) { // 插入玩家历史记录 Db::table('cy_member_history')->insert([ 'userid' => $this->userInfo['id'], //todo 'password' => '', 'mobile' => $data['mobile'], 'ip' => request()->ip(), 'create_time' => time(), ]); Session::delete('mobileVelidate'); return $this->json(['url' => url('v1.UserCenter/account')], 1, '手机号换绑成功'); //TODO 手动失效短信验证码 } return $this->json([], 0, '手机号换绑失败'); } } /** * 发送手机短信验证码 * @throws \app\api\controller\HttpResponseException */ public function sendSmsCode() { if ($this->request->isPost()) { // 手机号绑定页面,发送验证码 $data = $this->request->put(); $runnable = true; $message = ''; $code = 0; //1.判断验证码防止被刷 if (true !== ($res = $this->validate($data, [ ['img_code', 'require|captcha', '请输入图形验证码|图形验证码错误'], ] ))) { $runnable = false; $message = $res; } //3.发送短信验证码 if ($runnable) { $result = (new Sms())->sendCode($data['mobile']); if (!$result['status']) { $message = $result['msg']; } $code = $result['status']; } return $this->json('', $code, $message); } } /** * 发送手机短信验证码 * @throws \app\api\controller\HttpResponseException */ public function sendNewSmsCode() { if ($this->request->isPost()) { // 手机号绑定页面,发送验证码 $data = $this->request->put(); $runnable = true; $message = ''; $code = 0; //1.判断验证码防止被刷 if (true !== ($res = $this->validate($data, [ ['img_code', 'require|captcha', '请输入图形验证码|图形验证码错误'], ] ))) { $runnable = false; $message = $res; } //2.检查换绑手机是否跟原绑定手机一致 if (isset($this->userInfo['mobile']) && ($data['mobile'] == $this->userInfo['mobile'])) { $runnable = false; $message = '手机号跟原绑定手机号一样,请重新填写'; } //3.发送短信验证码 if ($runnable) { $result = (new Sms())->sendCode($data['mobile']); if (!$result['status']) { $message = $result['msg']; } $code = $result['status']; } return $this->json('', $code, $message); } } /** * 手机绑定操作 */ public function bindMobile() { if ($this->request->isPost()) { $data = $this->request->post(); //1.验证数据合法性 if (true !== ($res = $this->validate($data, [ 'sms_code|短信验证码' => 'require|isEmpty', 'mobile|手机号' => 'require|isEmpty|mobile', ]))) { return $this->json([], 0, $res); } //2.验证对应的手机号和验证码是否匹配 $res = (new Sms())->checkCode($data['mobile'], $data['sms_code']); if ($res['status'] == false) { return $this->json([], 0, $res['msg']); } //3.验证是否重复绑定 if (isset($this->userInfo['mobile']) && ($data['mobile'] == $this->userInfo['mobile'])) { return $this->json([], 0, '手机号跟原绑定手机号一样,请重新填写'); } //4.更新数据 记录日志 if ($res = Members::where(['username' => $this->username]) ->update(['mobile' => $data['mobile']])) { // 插入玩家历史记录 Db::table('cy_member_history')->insert([ 'userid' => $this->userInfo['id'], //todo 'password' => '', 'mobile' => $data['mobile'], 'ip' => request()->ip(), 'create_time' => time(), ]); return $this->json([], 1, '手机号绑定成功'); //TODO 手动失效短信验证码 } return $this->json([], 0, '手机号绑定失败'); } } }