Login.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use think\cache\driver\Redis;
  16. use think\Db;
  17. use think\facade\Validate;
  18. /**
  19. * @title 用户登录
  20. * @controller Login
  21. * @group worker
  22. */
  23. class Login extends Base
  24. {
  25. /**
  26. * @title 注册
  27. * @desc 注册
  28. * @url /api/Login/Register
  29. * @method POST
  30. * @tag 基础
  31. * @header
  32. * @param name:phone type:string require:1 desc:手机号
  33. * @param name:ver_code type:string require:1 desc:验证码
  34. * @param name:password type:string require:1 desc:密码
  35. * @param name:confirm_password type:string require:1 desc:确认密码
  36. * @param name:second_password type:string require:1 desc:二级密码
  37. * @param name:confirm_second_password type:string require:1 desc:二级确认密码
  38. * @param name:invite_code type:string require:0 desc:邀请码
  39. *
  40. */
  41. public function Register(){
  42. $phone = input('phone');
  43. $ver_code = input('ver_code');
  44. $password = input('password');
  45. $confirm_password = input('confirm_password');
  46. $invite_code = input('invite_code');
  47. if (!$phone || !$ver_code || !$password || !$confirm_password){
  48. $this->error('参数错误');
  49. }
  50. if (!Validate::regex($phone, "^1\d{10}$")) {
  51. $this->error('手机号格式错误');
  52. }
  53. //验证短信验证码
  54. $time = time()-60;
  55. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'register'])
  56. ->where('createtime','>',$time)
  57. ->order('id', 'DESC')
  58. ->find();
  59. // if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  60. $user = Db::name('store_member')
  61. ->where('is_deleted',0)
  62. ->where('phone',$phone)
  63. ->find();
  64. if ($user) $this->error('手机号已注册');
  65. if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
  66. if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
  67. $data = [
  68. 'phone'=>$phone,
  69. 'username'=>$phone,
  70. 'password'=>md5($password),
  71. 'reg_ip'=>request()->ip()
  72. ];
  73. $member_id = Db::name('store_member')->insertGetId($data);
  74. if ($member_id){
  75. $this->success('注册成功');
  76. }
  77. $this->error('注册失败');
  78. }
  79. /**
  80. * @title 登录
  81. * @desc 登录
  82. * @url /api/Login/passwordLogin
  83. * @method POST
  84. * @tag 基础
  85. * @header
  86. * @param name:phone type:int require:1 default:-- desc:手机号
  87. * @param name:password type:string require:1 default:-- desc:密码
  88. * @param name:ver_code type:string require:1 desc:验证码
  89. * @return name:token type:string default:-- desc:用户登录成功后的token值
  90. */
  91. public function passwordLogin()
  92. {
  93. $phone = input('phone');
  94. $password = input('password');
  95. if (empty($password) || empty($phone)) {
  96. $this->error('参数错误');
  97. }
  98. $member = Db::name('store_member')
  99. ->where('phone', $phone)
  100. ->where('is_deleted',0)
  101. ->find();
  102. if (!$member) $this->error('手机号未注册');
  103. //验证短信验证码
  104. $token = self::create_jwt($member['id']);
  105. Db::name('store_member')->where('id',$member['id'])->update(['ip'=>request()->ip()]);
  106. $this->success('登录成功', $token);
  107. }
  108. //token加密
  109. public function create_jwt($uid)
  110. {
  111. $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
  112. $time = time(); //签发时间
  113. $expire = $time + config('app.jwt_time'); //过期时间
  114. $token = array(
  115. "uid" => $uid,
  116. "iss" => "https://zain.com",//签发组织
  117. "aud" => "https://zain.com", //签发作者
  118. "iat" => $time,
  119. "nbf" => $time,
  120. "exp" => $expire
  121. );
  122. $jwtmodel = require_once '../vendor/firebase/php-jwt/src/JWT.php';
  123. $jwt = $jwtmodel::encode($token, $key);
  124. return $jwt;
  125. }
  126. /**
  127. * @title 找回密码
  128. * @desc 找回密码
  129. * @url /api/Login/ForgetPassword
  130. * @method POST
  131. * @tag 基础
  132. * @header
  133. * @param name:phone type:int require:1 default:-- desc:手机号
  134. * @param name:ver_code type:string require:1 desc:验证码
  135. * @param name:password type:string require:1 default:-- desc:密码
  136. * @param name:confirm_password type:string require:1 desc:确认密码
  137. */
  138. public function ForgetPassword(){
  139. $phone = input('phone');
  140. $ver_code = input('ver_code');
  141. $password = input('password');
  142. $confirm_password = input('confirm_password');
  143. if (!$phone || !$ver_code || !$password || !$confirm_password) $this->error('参数错误');
  144. $member = Db::name('store_member')
  145. ->where('phone', $phone)
  146. ->where('is_deleted',0)
  147. ->find();
  148. if (!$member) $this->error('手机号未注册');
  149. //验证短信验证码
  150. $time = time()-60;
  151. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'forgetpwd'])
  152. ->where('createtime','>',$time)
  153. ->order('id', 'DESC')
  154. ->find();
  155. if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  156. if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
  157. if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
  158. $data = [
  159. 'password'=>md5($password),
  160. 'update_at'=>date('Y-m-d H:i:s')
  161. ];
  162. if (Db::name('store_member')->where('id',$member['id'])->update($data)) $this->success('修改成功');
  163. $this->error('修改失败');
  164. }
  165. /**
  166. * @title 更换手机号
  167. * @desc 更换手机号
  168. * @url /api/Login/ForgetPassword
  169. * @method POST
  170. * @tag 基础
  171. * @header
  172. * @param name:phone type:int require:1 default:-- desc:手机号
  173. * @param name:ver_code type:string require:1 desc:验证码
  174. * @param name:password type:string require:1 default:-- desc:密码
  175. * @param name:confirm_password type:string require:1 desc:确认密码
  176. */
  177. public function ForgetPhone(){
  178. $uid = $this->get_uid();
  179. $phone = input('phone');
  180. $ver_code = input('ver_code');
  181. //验证短信验证码
  182. $time = time()-60;
  183. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'forgetpwd'])
  184. ->where('createtime','>',$time)
  185. ->order('id', 'DESC')
  186. ->find();
  187. if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  188. $data = [
  189. 'phone'=>$phone,
  190. 'update_at'=>date('Y-m-d H:i:s')
  191. ];
  192. if (Db::name('store_member')->where('id',$uid)->update($data)) $this->success('修改成功');
  193. $this->error('修改失败');
  194. }
  195. }