Mobile.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * 账户绑定
  4. */
  5. namespace app\api\controller\v1;
  6. use app\common\library\Sms;
  7. use app\common\model\Members;
  8. use think\Db;
  9. use think\Session;
  10. class Mobile extends UserCenter
  11. {
  12. protected $memberModel;
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. }
  17. /**
  18. * 手机绑定页面
  19. * @return mixed
  20. * @throws \app\api\controller\HttpResponseException
  21. */
  22. public function index()
  23. {
  24. if (!empty($this->userInfo['mobile'])) {// 手机已绑定,直接返回绑定中心
  25. $this->error('您已经绑定手机号', url('v1.userCenter/index'));
  26. }
  27. return $this->fetch();
  28. }
  29. /**
  30. * @return mixed
  31. * @throws \app\api\controller\HttpResponseException
  32. */
  33. public function resetMobile()
  34. {
  35. if (empty($this->userInfo['mobile'])) {
  36. $this->error('用户还未绑定手机', url('v1.userCenter/account'));
  37. }
  38. $this->assign('mobile', ($this->userInfo['mobile']));
  39. return $this->fetch();
  40. }
  41. /**
  42. * 手机换绑原手机号验证
  43. * @throws \app\api\controller\HttpResponseException
  44. */
  45. public function velidateCode()
  46. {
  47. if ($this->request->isPost()) {
  48. $data = $this->request->post();
  49. //1.验证数据合法性
  50. if (true !== ($res = $this->validate($data, [
  51. 'sms_code|短信验证码' => 'require|isEmpty',
  52. 'mobile|手机号' => 'require|isEmpty|mobile',
  53. ]))) {
  54. return $this->json([], 0, $res);
  55. }
  56. //2.验证对应的手机号和验证码是否匹配
  57. $res = (new Sms())->checkCode($data['mobile'], $data['sms_code']);
  58. if ($res['status'] == false) {
  59. return $this->json([], 0, $res['msg']);
  60. }
  61. else{
  62. Session::set('mobileVelidate', 1);
  63. return $this->json(['url' => url('v1.mobile/changeMobile')], 1, '原手机号验证成功');
  64. }
  65. }
  66. }
  67. /**
  68. * @return mixed
  69. * @throws \app\api\controller\HttpResponseException
  70. */
  71. public function changeMobile()
  72. {
  73. if (empty($this->userInfo['mobile'])) {
  74. $this->error('用户还未绑定手机', url('v1.userCenter/account'));
  75. }
  76. if (!Session::get('mobileVelidate')) {
  77. $this->error('请先进行原手机验证', url('v1.mobile/resetMobile'));
  78. }
  79. $this->assign('mobile', ($this->userInfo['mobile']));
  80. return $this->fetch();
  81. }
  82. /**
  83. * 解除手机绑定
  84. * @throws \app\api\controller\HttpResponseException
  85. */
  86. public function unBindMobile()
  87. {
  88. if ($this->request->isPost()) {
  89. $data = $this->request->post();
  90. //1.验证数据合法性
  91. if (true !== ($res = $this->validate($data, [
  92. 'sms_code|短信验证码' => 'require|isEmpty',
  93. 'mobile|手机号' => 'require|isEmpty|mobile',
  94. ]))) {
  95. return $this->json([], 0, $res);
  96. }
  97. //2.验证对应的手机号和验证码是否匹配
  98. $res = (new Sms())->checkCode($data['mobile'], $data['sms_code']);
  99. if ($res['status'] == false) {
  100. return $this->json([], 0, $res['msg']);
  101. }
  102. //3.验证是否重复绑定
  103. if (isset($this->userInfo['mobile']) && ($data['mobile'] == $this->userInfo['mobile'])) {
  104. return $this->json([], 0, '手机号跟原绑定手机号一样,请重新填写');
  105. }
  106. //4.更新数据 记录日志
  107. if ($res = Members::where(['username' => $this->username])->update(['mobile' => $data['mobile'],'update_time'=>time()])) { // 插入玩家历史记录
  108. Db::table('cy_member_history')->insert([
  109. 'userid' => $this->userInfo['id'], //todo
  110. 'password' => '',
  111. 'mobile' => $data['mobile'],
  112. 'ip' => request()->ip(),
  113. 'create_time' => time(),
  114. ]);
  115. Session::delete('mobileVelidate');
  116. return $this->json(['url' => url('v1.UserCenter/account')], 1, '手机号换绑成功');
  117. //TODO 手动失效短信验证码
  118. }
  119. return $this->json([], 0, '手机号换绑失败');
  120. }
  121. }
  122. /**
  123. * 发送手机短信验证码
  124. * @throws \app\api\controller\HttpResponseException
  125. */
  126. public function sendSmsCode()
  127. {
  128. if ($this->request->isPost()) { // 手机号绑定页面,发送验证码
  129. $data = $this->request->put();
  130. $runnable = true;
  131. $message = '';
  132. $code = 0;
  133. //1.判断验证码防止被刷
  134. if (true !== ($res = $this->validate($data,
  135. [
  136. ['img_code', 'require|captcha', '请输入图形验证码|图形验证码错误'],
  137. ]
  138. ))) {
  139. $runnable = false;
  140. $message = $res;
  141. }
  142. //3.发送短信验证码
  143. if ($runnable) {
  144. $result = (new Sms())->sendCode($data['mobile']);
  145. if (!$result['status']) {
  146. $message = $result['msg'];
  147. }
  148. $code = $result['status'];
  149. }
  150. return $this->json('', $code, $message);
  151. }
  152. }
  153. /**
  154. * 发送手机短信验证码
  155. * @throws \app\api\controller\HttpResponseException
  156. */
  157. public function sendNewSmsCode()
  158. {
  159. if ($this->request->isPost()) { // 手机号绑定页面,发送验证码
  160. $data = $this->request->put();
  161. $runnable = true;
  162. $message = '';
  163. $code = 0;
  164. //1.判断验证码防止被刷
  165. if (true !== ($res = $this->validate($data,
  166. [
  167. ['img_code', 'require|captcha', '请输入图形验证码|图形验证码错误'],
  168. ]
  169. ))) {
  170. $runnable = false;
  171. $message = $res;
  172. }
  173. //2.检查换绑手机是否跟原绑定手机一致
  174. if (isset($this->userInfo['mobile']) && ($data['mobile'] == $this->userInfo['mobile'])) {
  175. $runnable = false;
  176. $message = '手机号跟原绑定手机号一样,请重新填写';
  177. }
  178. //3.发送短信验证码
  179. if ($runnable) {
  180. $result = (new Sms())->sendCode($data['mobile']);
  181. if (!$result['status']) {
  182. $message = $result['msg'];
  183. }
  184. $code = $result['status'];
  185. }
  186. return $this->json('', $code, $message);
  187. }
  188. }
  189. /**
  190. * 手机绑定操作
  191. */
  192. public function bindMobile()
  193. {
  194. if ($this->request->isPost()) {
  195. $data = $this->request->post();
  196. //1.验证数据合法性
  197. if (true !== ($res = $this->validate($data, [
  198. 'sms_code|短信验证码' => 'require|isEmpty',
  199. 'mobile|手机号' => 'require|isEmpty|mobile',
  200. ]))) {
  201. return $this->json([], 0, $res);
  202. }
  203. //2.验证对应的手机号和验证码是否匹配
  204. $res = (new Sms())->checkCode($data['mobile'], $data['sms_code']);
  205. if ($res['status'] == false) {
  206. return $this->json([], 0, $res['msg']);
  207. }
  208. //3.验证是否重复绑定
  209. if (isset($this->userInfo['mobile']) && ($data['mobile'] == $this->userInfo['mobile'])) {
  210. return $this->json([], 0, '手机号跟原绑定手机号一样,请重新填写');
  211. }
  212. //4.更新数据 记录日志
  213. if ($res = Members::where(['username' => $this->username])
  214. ->update(['mobile' => $data['mobile']])) { // 插入玩家历史记录
  215. Db::table('cy_member_history')->insert([
  216. 'userid' => $this->userInfo['id'], //todo
  217. 'password' => '',
  218. 'mobile' => $data['mobile'],
  219. 'ip' => request()->ip(),
  220. 'create_time' => time(),
  221. ]);
  222. return $this->json([], 1, '手机号绑定成功');
  223. //TODO 手动失效短信验证码
  224. }
  225. return $this->json([], 0, '手机号绑定失败');
  226. }
  227. }
  228. }