123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\api\controller;
- use think\cache\driver\Redis;
- use think\Db;
- use think\facade\Validate;
- /**
- * @title 用户登录
- * @controller Login
- * @group worker
- */
- class Login extends Base
- {
- /**
- * @title 注册
- * @desc 注册
- * @url /api/Login/Register
- * @method POST
- * @tag 基础
- * @header
- * @param name:phone type:string require:1 desc:手机号
- * @param name:ver_code type:string require:1 desc:验证码
- * @param name:password type:string require:1 desc:密码
- * @param name:confirm_password type:string require:1 desc:确认密码
- * @param name:second_password type:string require:1 desc:二级密码
- * @param name:confirm_second_password type:string require:1 desc:二级确认密码
- * @param name:invite_code type:string require:0 desc:邀请码
- *
- */
- public function Register(){
- $phone = input('phone');
- $ver_code = input('ver_code');
- $password = input('password');
- $confirm_password = input('confirm_password');
- $invite_code = input('invite_code');
- if (!$phone || !$ver_code || !$password || !$confirm_password){
- $this->error('参数错误');
- }
- if (!Validate::regex($phone, "^1\d{10}$")) {
- $this->error('手机号格式错误');
- }
- //验证短信验证码
- $time = time()-60;
- $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'register'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- // if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
- $user = Db::name('store_member')
- ->where('is_deleted',0)
- ->where('phone',$phone)
- ->find();
- if ($user) $this->error('手机号已注册');
- if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
- if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
- $data = [
- 'phone'=>$phone,
- 'username'=>$phone,
- 'password'=>md5($password),
- 'reg_ip'=>request()->ip()
- ];
- $member_id = Db::name('store_member')->insertGetId($data);
- if ($member_id){
- $this->success('注册成功');
- }
- $this->error('注册失败');
- }
- /**
- * @title 登录
- * @desc 登录
- * @url /api/Login/passwordLogin
- * @method POST
- * @tag 基础
- * @header
- * @param name:phone type:int require:1 default:-- desc:手机号
- * @param name:password type:string require:1 default:-- desc:密码
- * @param name:ver_code type:string require:1 desc:验证码
- * @return name:token type:string default:-- desc:用户登录成功后的token值
- */
- public function passwordLogin()
- {
- $phone = input('phone');
- $password = input('password');
- if (empty($password) || empty($phone)) {
- $this->error('参数错误');
- }
- $member = Db::name('store_member')
- ->where('phone', $phone)
- ->where('is_deleted',0)
- ->find();
- if (!$member) $this->error('手机号未注册');
- //验证短信验证码
- $token = self::create_jwt($member['id']);
- Db::name('store_member')->where('id',$member['id'])->update(['ip'=>request()->ip()]);
- $this->success('登录成功', $token);
- }
- //token加密
- public function create_jwt($uid)
- {
- $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
- $time = time(); //签发时间
- $expire = $time + config('app.jwt_time'); //过期时间
- $token = array(
- "uid" => $uid,
- "iss" => "https://zain.com",//签发组织
- "aud" => "https://zain.com", //签发作者
- "iat" => $time,
- "nbf" => $time,
- "exp" => $expire
- );
- $jwtmodel = require_once '../vendor/firebase/php-jwt/src/JWT.php';
- $jwt = $jwtmodel::encode($token, $key);
- return $jwt;
- }
- /**
- * @title 找回密码
- * @desc 找回密码
- * @url /api/Login/ForgetPassword
- * @method POST
- * @tag 基础
- * @header
- * @param name:phone type:int require:1 default:-- desc:手机号
- * @param name:ver_code type:string require:1 desc:验证码
- * @param name:password type:string require:1 default:-- desc:密码
- * @param name:confirm_password type:string require:1 desc:确认密码
- */
- public function ForgetPassword(){
- $phone = input('phone');
- $ver_code = input('ver_code');
- $password = input('password');
- $confirm_password = input('confirm_password');
- if (!$phone || !$ver_code || !$password || !$confirm_password) $this->error('参数错误');
- $member = Db::name('store_member')
- ->where('phone', $phone)
- ->where('is_deleted',0)
- ->find();
- if (!$member) $this->error('手机号未注册');
- //验证短信验证码
- $time = time()-60;
- $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'forgetpwd'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
- if (!preg_match('/^[0-9a-z]{6,12}$/i',$password)) $this->error('密码格式错误,请输入6-12位数字+字母');
- if ($password!=$confirm_password) $this->error('密码与确认密码不一致');
- $data = [
- 'password'=>md5($password),
- 'update_at'=>date('Y-m-d H:i:s')
- ];
- if (Db::name('store_member')->where('id',$member['id'])->update($data)) $this->success('修改成功');
- $this->error('修改失败');
- }
- /**
- * @title 更换手机号
- * @desc 更换手机号
- * @url /api/Login/ForgetPassword
- * @method POST
- * @tag 基础
- * @header
- * @param name:phone type:int require:1 default:-- desc:手机号
- * @param name:ver_code type:string require:1 desc:验证码
- * @param name:password type:string require:1 default:-- desc:密码
- * @param name:confirm_password type:string require:1 desc:确认密码
- */
- public function ForgetPhone(){
- $uid = $this->get_uid();
- $phone = input('phone');
- $ver_code = input('ver_code');
- //验证短信验证码
- $time = time()-60;
- $sms = Db::name('store_sms')->where(['mobile' => $phone, 'event' => 'forgetpwd'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- if (!$sms || $sms['code'] != $ver_code) $this->error('短信验证码不正确!');
- $data = [
- 'phone'=>$phone,
- 'update_at'=>date('Y-m-d H:i:s')
- ];
- if (Db::name('store_member')->where('id',$uid)->update($data)) $this->success('修改成功');
- $this->error('修改失败');
- }
- }
|