| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- /**
- * 账户绑定
- */
- namespace app\api\controller\v1;
- use app\common\library\Sms;
- use app\common\model\Members;
- use think\Db;
- use think\Session;
- class Mobile extends UserCenter
- {
- protected $memberModel;
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 手机绑定页面
- * @return mixed
- * @throws \app\api\controller\HttpResponseException
- */
- public function index()
- {
- if (!empty($this->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, '手机号绑定失败');
- }
- }
- }
|