Login.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Db;
  16. use Firebase\JWT\JWT;
  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. $second_password = input('second_password');
  47. $confirm_second_password = input('confirm_second_password');
  48. $invite_code = input('invite_code');
  49. if (!$phone || !$ver_code || !$password || !$confirm_password || !$second_password || !$confirm_second_password){
  50. $this->error('参数错误');
  51. }
  52. if (!Validate::regex($phone, "^1\d{10}$")) {
  53. $this->error('手机号格式错误');
  54. }
  55. //验证短信验证码
  56. $time = time()-60;
  57. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'register'])
  58. ->where('createtime','>',$time)
  59. ->order('id', 'DESC')
  60. ->find();
  61. if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  62. $user = Db::name('store_member')
  63. ->where('is_deleted',0)
  64. ->where('phone',$phone)
  65. ->find();
  66. if ($user) $this->error('手机号已注册');
  67. if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
  68. if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
  69. if (!preg_match('/^[0-9]{6}$/i',$second_password)) $this->error('二级密码格式错误,请输入6位纯数字');
  70. if ($second_password!=$confirm_second_password) $this->error('二级密码与确认密码不一致');
  71. if ($invite_code){
  72. $isset = Db::name('store_member')->where('is_deleted',0)->where('id',$invite_code)->find();
  73. if (!$isset) $this->error('邀请码不存在');
  74. }else{
  75. $invite_code = 0;
  76. }
  77. $wallet_address = getWalletAddress($phone);
  78. if (!$wallet_address){
  79. $wallet_address = '';
  80. $accountName = '';
  81. }else{
  82. $accountName = $phone;
  83. }
  84. $data = [
  85. 'phone'=>$phone,
  86. 'pid'=>$invite_code,
  87. 'password'=>md5($password),
  88. 'second_password'=>md5($second_password),
  89. 'wallet_address'=>$wallet_address,
  90. 'accountName'=>$accountName
  91. ];
  92. $member_id = Db::name('store_member')->insertGetId($data);
  93. if ($member_id){
  94. $invite_img = setintivecode($member_id);
  95. $invite_address = getintiveaddress($member_id);
  96. Db::name('store_member')->where('id',$member_id)->update(['name'=>'收藏家'.$member_id,'invite_img'=>$invite_img,'invite_address'=>$invite_address]);
  97. //邀请好友送积分
  98. if ($invite_code>0){
  99. $invite_friends_integral = getConfigValue('invite_friends_integral');
  100. memberMoneyChange($invite_friends_integral,1,$invite_code,'邀请好友',1,$member_id);
  101. }
  102. $this->success('注册成功');
  103. }
  104. $this->error('注册失败');
  105. }
  106. /**
  107. * @title 登录
  108. * @desc 登录
  109. * @url /api/Login/passwordLogin
  110. * @method POST
  111. * @tag 基础
  112. * @header
  113. * @param name:phone type:int require:1 default:-- desc:手机号
  114. * @param name:password type:string require:1 default:-- desc:密码
  115. * @return name:token type:string default:-- desc:用户登录成功后的token值
  116. */
  117. public function passwordLogin()
  118. {
  119. $phone = input('phone');
  120. $password = input('password');
  121. if (empty($password) || empty($phone)) {
  122. $this->error('参数错误');
  123. }
  124. $member = Db::name('store_member')
  125. ->where('phone', $phone)
  126. ->where('is_deleted',0)
  127. ->find();
  128. if (!$member) $this->error('手机号未注册');
  129. if ($member['password']!=md5($password)) $this->error('密码错误');
  130. $token = self::create_jwt($member['id']);
  131. setMemberInfoHash($member['id']);
  132. $this->success('登录成功', $token);
  133. }
  134. //token加密
  135. public function create_jwt($uid)
  136. {
  137. $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
  138. $time = time(); //签发时间
  139. $expire = $time + config('app.jwt_time'); //过期时间
  140. $token = array(
  141. "uid" => $uid,
  142. "iss" => "https://zain.com",//签发组织
  143. "aud" => "https://zain.com", //签发作者
  144. "iat" => $time,
  145. "nbf" => $time,
  146. "exp" => $expire
  147. );
  148. $jwt = JWT::encode($token, $key);
  149. return $jwt;
  150. }
  151. /**
  152. * @title 找回密码
  153. * @desc 找回密码
  154. * @url /api/Login/ForgetPassword
  155. * @method POST
  156. * @tag 基础
  157. * @header
  158. * @param name:phone type:int require:1 default:-- desc:手机号
  159. * @param name:ver_code type:string require:1 desc:验证码
  160. * @param name:password type:string require:1 default:-- desc:密码
  161. * @param name:confirm_password type:string require:1 desc:确认密码
  162. */
  163. public function ForgetPassword(){
  164. $phone = input('phone');
  165. $ver_code = input('ver_code');
  166. $password = input('password');
  167. $confirm_password = input('confirm_password');
  168. if (!$phone || !$ver_code || !$password || !$confirm_password) $this->error('参数错误');
  169. $member = Db::name('store_member')
  170. ->where('phone', $phone)
  171. ->where('is_deleted',0)
  172. ->find();
  173. if (!$member) $this->error('手机号未注册');
  174. //验证短信验证码
  175. $time = time()-60;
  176. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'forgetpwd'])
  177. ->where('createtime','>',$time)
  178. ->order('id', 'DESC')
  179. ->find();
  180. if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  181. if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
  182. if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
  183. $data = [
  184. 'password'=>md5($password),
  185. 'update_at'=>date('Y-m-d H:i:s')
  186. ];
  187. if (Db::name('store_member')->where('id',$member['id'])->update($data)) $this->success('修改成功');
  188. $this->error('修改失败');
  189. }
  190. public function test(){
  191. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa6980453638c9c78&secret=de7267bf7616bd73d867535642196fed';
  192. $res=curlRequest($url);
  193. $res = json_decode($res,true);
  194. $url2 ="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$res['access_token']."&type=jsapi";
  195. $res2=curlRequest($url2);
  196. $res2 = json_decode($res2,true);
  197. $timestamp = time();
  198. $noncestr = get32Str(15);
  199. $url = 'http://jybl.hdlkeji.com/web/h5/';
  200. $string = "jsapi_ticket=".$res2['ticket']."&noncestr=$noncestr&timestamp=$timestamp&url=".$url;
  201. $result = sha1($string);
  202. echo $result;die;
  203. }
  204. }