Login.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Firebase\JWT\JWT;
  17. use think\Db;
  18. use think\facade\Validate;
  19. /**
  20. * @title 用户登录
  21. * @controller Login
  22. * @group worker
  23. */
  24. class Login extends Base
  25. {
  26. /**
  27. * @title 注册
  28. * @desc 注册
  29. * @url /api/Login/Register
  30. * @method POST
  31. * @tag 基础
  32. * @header
  33. * @param name:phone type:string require:1 desc:手机号
  34. * @param name:ver_code type:string require:1 desc:验证码
  35. * @param name:password type:string require:1 desc:密码
  36. * @param name:confirm_password type:string require:1 desc:确认密码
  37. * @param name:second_password type:string require:1 desc:二级密码
  38. * @param name:confirm_second_password type:string require:1 desc:二级确认密码
  39. * @param name:invite_code type:string require:0 desc:邀请码
  40. *
  41. */
  42. public function Register(){
  43. $phone = input('phone');
  44. $ver_code = input('ver_code');
  45. $password = input('password');
  46. $confirm_password = input('confirm_password');
  47. $invite_code = input('invite_code');
  48. if (!$phone || !$ver_code || !$password || !$confirm_password){
  49. $this->error('参数错误');
  50. }
  51. if (!Validate::regex($phone, "^1\d{10}$")) {
  52. $this->error('手机号格式错误');
  53. }
  54. //验证短信验证码
  55. $time = time()-60;
  56. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'register'])
  57. ->where('createtime','>',$time)
  58. ->order('id', 'DESC')
  59. ->find();
  60. if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  61. $user = Db::name('store_member')
  62. ->where('is_deleted',0)
  63. ->where('phone',$phone)
  64. ->find();
  65. if ($user) $this->error('手机号已注册');
  66. if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
  67. if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
  68. $data = [
  69. 'phone'=>$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. $jwt = JWT::encode($token, $key);
  123. return $jwt;
  124. }
  125. /**
  126. * @title 找回密码
  127. * @desc 找回密码
  128. * @url /api/Login/ForgetPassword
  129. * @method POST
  130. * @tag 基础
  131. * @header
  132. * @param name:phone type:int require:1 default:-- desc:手机号
  133. * @param name:ver_code type:string require:1 desc:验证码
  134. * @param name:password type:string require:1 default:-- desc:密码
  135. * @param name:confirm_password type:string require:1 desc:确认密码
  136. */
  137. public function ForgetPassword(){
  138. $phone = input('phone');
  139. $ver_code = input('ver_code');
  140. $password = input('password');
  141. $confirm_password = input('confirm_password');
  142. if (!$phone || !$ver_code || !$password || !$confirm_password) $this->error('参数错误');
  143. $member = Db::name('store_member')
  144. ->where('phone', $phone)
  145. ->where('is_deleted',0)
  146. ->find();
  147. if (!$member) $this->error('手机号未注册');
  148. //验证短信验证码
  149. $time = time()-60;
  150. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'forgetpwd'])
  151. ->where('createtime','>',$time)
  152. ->order('id', 'DESC')
  153. ->find();
  154. if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  155. if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
  156. if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
  157. $data = [
  158. 'password'=>md5($password),
  159. 'update_at'=>date('Y-m-d H:i:s')
  160. ];
  161. if (Db::name('store_member')->where('id',$member['id'])->update($data)) $this->success('修改成功');
  162. $this->error('修改失败');
  163. }
  164. }