Login.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 Firebase\JWT\JWT;
  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. $second_password = input('second_password');
  48. $confirm_second_password = input('confirm_second_password');
  49. $invite_code = input('invite_code');
  50. if (!$phone || !$ver_code || !$password || !$confirm_password || !$second_password || !$confirm_second_password){
  51. // $this->error('参数错误');
  52. }
  53. if (!Validate::regex($phone, "^1\d{10}$")) {
  54. $this->error('手机号格式错误');
  55. }
  56. //验证短信验证码
  57. $time = time()-60;
  58. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'register'])
  59. ->where('createtime','>',$time)
  60. ->order('id', 'DESC')
  61. ->find();
  62. if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  63. $user = Db::name('store_member')
  64. ->where('is_deleted',0)
  65. ->where('phone',$phone)
  66. ->find();
  67. if ($user) $this->error('手机号已注册');
  68. if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
  69. if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
  70. if (!preg_match('/^[0-9]{6}$/i',$second_password)) $this->error('支付密码格式错误,请输入6位纯数字');
  71. if ($second_password!=$confirm_second_password) $this->error('支付密码与确认密码不一致');
  72. if ($invite_code){
  73. $isset = Db::name('store_member')->where('is_deleted',0)->where('invite_code',$invite_code)->find();
  74. if (!$isset) $this->error('邀请码不存在');
  75. $invitecode = $isset['id'];
  76. }else{
  77. $invitecode = 0;
  78. }
  79. $accountName = $phone;
  80. $data = [
  81. 'phone'=>$phone,
  82. 'pid'=>$invitecode,
  83. 'password'=>md5($password),
  84. 'second_password'=>md5($second_password),
  85. 'wallet_address'=>'',
  86. 'accountName'=>$accountName
  87. ];
  88. $member_id = Db::name('store_member')->insertGetId($data);
  89. if ($member_id){
  90. $code = get32Str(8);
  91. $invite_img = setintivecode($code);
  92. $invite_address = getintiveaddress($code);
  93. Db::name('store_member')->where('id',$member_id)->update(['name'=>'收藏家'.$member_id,'invite_img'=>$invite_img,'invite_address'=>$invite_address,'invite_code'=>$code]);
  94. //邀请好友送积分
  95. if ($invite_code>0){
  96. $invite_friends_integral = getConfigValue('invite_friends_integral');
  97. //memberMoneyChange($invite_friends_integral,1,$invite_code,'邀请好友',1,$member_id);
  98. }
  99. $this->success('注册成功');
  100. }
  101. $this->error('注册失败');
  102. }
  103. /**
  104. * @title 登录
  105. * @desc 登录
  106. * @url /api/Login/passwordLogin
  107. * @method POST
  108. * @tag 基础
  109. * @header
  110. * @param name:phone type:int require:1 default:-- desc:手机号
  111. * @param name:password type:string require:1 default:-- desc:密码
  112. * @return name:token type:string default:-- desc:用户登录成功后的token值
  113. */
  114. public function passwordLogin()
  115. {
  116. $phone = input('phone');
  117. $password = input('password');
  118. if (empty($password) || empty($phone)) {
  119. $this->error('参数错误');
  120. }
  121. $member = Db::name('store_member')
  122. ->where('phone', $phone)
  123. ->where('is_deleted',0)
  124. ->find();
  125. if (!$member) $this->error('手机号未注册');
  126. if ($member['password']!=md5($password)) $this->error('密码错误');
  127. $token = self::create_jwt($member['id']);
  128. setMemberInfoHash($member['id']);
  129. $this->success('登录成功', $token);
  130. }
  131. //token加密
  132. public function create_jwt($uid)
  133. {
  134. $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
  135. $time = time(); //签发时间
  136. $expire = $time + config('app.jwt_time'); //过期时间
  137. $token = array(
  138. "uid" => $uid,
  139. "iss" => "https://zain.com",//签发组织
  140. "aud" => "https://zain.com", //签发作者
  141. "iat" => $time,
  142. "nbf" => $time,
  143. "exp" => $expire
  144. );
  145. $jwt = JWT::encode($token, $key);
  146. return $jwt;
  147. }
  148. /**
  149. * @title 找回密码
  150. * @desc 找回密码
  151. * @url /api/Login/ForgetPassword
  152. * @method POST
  153. * @tag 基础
  154. * @header
  155. * @param name:phone type:int require:1 default:-- desc:手机号
  156. * @param name:ver_code type:string require:1 desc:验证码
  157. * @param name:password type:string require:1 default:-- desc:密码
  158. * @param name:confirm_password type:string require:1 desc:确认密码
  159. */
  160. public function ForgetPassword(){
  161. $phone = input('phone');
  162. $ver_code = input('ver_code');
  163. $password = input('password');
  164. $confirm_password = input('confirm_password');
  165. if (!$phone || !$ver_code || !$password || !$confirm_password) $this->error('参数错误');
  166. $member = Db::name('store_member')
  167. ->where('phone', $phone)
  168. ->where('is_deleted',0)
  169. ->find();
  170. if (!$member) $this->error('手机号未注册');
  171. //验证短信验证码
  172. $time = time()-60;
  173. $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'forgetpwd'])
  174. ->where('createtime','>',$time)
  175. ->order('id', 'DESC')
  176. ->find();
  177. if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
  178. if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
  179. if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
  180. $data = [
  181. 'password'=>md5($password),
  182. 'update_at'=>date('Y-m-d H:i:s')
  183. ];
  184. if (Db::name('store_member')->where('id',$member['id'])->update($data)) $this->success('修改成功');
  185. $this->error('修改失败');
  186. }
  187. public function test(){
  188. $list = Db::name('store_collection')->select();
  189. foreach ($list as &$v){
  190. $cover = str_replace('https://fanyi.fanyiys.com/','https://fanyisc.oss-cn-hangzhou.aliyuncs.com/',$v['auth_img']);
  191. echo $cover."<br />";
  192. Db::name('store_collection')->where('id',$v['id'])->update(['auth_img'=>$cover]);
  193. }
  194. // $list = Db::name('store_order_info')->select();
  195. // foreach ($list as &$v){
  196. // $data = [];
  197. // $info = Db::name('store_collection')->where('id',$v['c_id'])->find();
  198. // $data['cover'] = $info['cover'];
  199. // $data['pro_info'] = json_encode($info,true);
  200. // Db::name('store_order_info')->where('id',$v['id'])->update($data);
  201. // echo $v['id']."<br />";
  202. //
  203. // }
  204. // $ids = Db::name('store_order_info')->group('c_id')->column('c_id');
  205. // $redis = new Redis();
  206. // foreach ($ids as &$v){
  207. // $max = Db::name('store_order_info')->where('c_id',$v)->order('id desc')->field('id,tag,c_id')->limit(1)->find();
  208. // $tag = explode('#',$max['tag']);
  209. // $num = explode('/',$tag[1]);
  210. // $number = (int)$num[0];
  211. // $redis->set('ranking'.$v,$number);
  212. // dump($max)."<br />";
  213. // }
  214. // $redis = new Redis();
  215. // $list = Db::name('hash')->where('status',0)->select();
  216. // foreach ($list as &$v){
  217. // //存入reids list
  218. // $redis_data = ['hash'=>$v['hash'],'tokenid'=>$v['tokenid'],'create_at'=>$v['create_at']];
  219. // $redis->rPush('collectionHash_'.$v['goods_id'],json_encode($redis_data));
  220. // echo $v['hash']."<br />";
  221. // }
  222. $redis = new Redis();
  223. $list = Db::name('store_collection')->select();
  224. foreach ($list as &$v){
  225. $buy_count = Db::name('store_order_info')
  226. ->whereIn('status','1,3')
  227. ->where('c_id',$v['id'])
  228. ->count();
  229. $now = $v['inventory']-$buy_count;
  230. $redis->set('collection_count_'.$v['id'],$now);
  231. echo $v['id']."<br />";
  232. }
  233. }
  234. }