123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace app\api\controller;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use app\api\controller\Base;
- use think\Db;
- use Firebase\JWT\JWT;
- use EasyWeChat\Factory;
- class Login extends Base
- {
-
- public function weChatLogin(){
- $code = input('code');
- $headimg = input('headimg');
- $name = input('name');
- $pid = input('pid',0);
- $iv = input('iv');
- $encryptedData = input('encrypted');
- if(empty($code) || empty($headimg) || empty($name)){
- $this->error('参数错误');
- }
- $app = Factory::miniProgram(config('app.mini_program'));
- $data = $app->auth->session($code);
- if(empty($data['openid'])){
- $this->error($data['errmsg']);
- }
- $phone = '';
- if($iv && $encryptedData) {
- require_once env('root_path').'/vendor/program/wxBizDataCrypt.php';
- $sessionKey = $data['session_key'];
- $pc = new \WXBizDataCrypt(config('app.mini_program')['app_id'], $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $info);
- if($errCode != 0) $this->error('微信登录失败');
- $info = json_decode($info,true);
- $phone = $info['purePhoneNumber'];
- }
- $member = Db::name('store_member')->field('id,phone')->where('openid',$data['openid'])->find();
- if(empty($member)){
-
- if($phone){
- $member_id = Db::name('store_member')->where('phone','=',$phone)->value('id');
- if($member_id) $phone = '';
- }
- $member_data = array(
- 'openid' => $data['openid'],
- 'headimg' => $headimg,
- 'name' => $name,
- 'pid' =>$pid,
- 'phone' => $phone,
- 'integral' => 0,
- 'growth' => 0,
- 'create_at'=>date("Y-m-d H:i:s")
- );
- Db::table('store_member')->insert($member_data);
- $uid = Db::getLastInsID();
-
- if($pid){
- $check_integral = Db::table('integral_info')
- ->where('user_id','=',$pid)
- ->where('create_at','> time',date('Y-m-d 00:00:00'))
- ->where('type','=',1)
- ->sum('integral');
- if($check_integral < 100) {
- update_user_integral($pid,10,1,'恭喜亲通过邀请好友注册获得10积分',$uid);
- update_user_growth($pid,10,1,'恭喜亲通过邀请好友注册获得10成长值',['register_id'=>$uid]);
- }
- }
- }else{
- $uid = $member['id'];
- }
- if(empty($uid)) $this->error('数据有误');
- $token = self::create_jwt($uid);
- $this->success('登录成功',$token);
- }
-
- public function passwordLogin(){
- $phone = input('phone');
- $password = input('password');
- if(empty($password) || empty($phone)){
- $this->error('参数错误');
- }
- $member_id = Db::name('store_member')->where('phone',$phone)->where('password',md5($password))->value('id');
- if(empty($member_id)){
- $this->error('手机号或密码错误');
- }
- $token = self::create_jwt($member_id);
- $this->success('登录成功',$token);
- }
-
- public function create_jwt($uid)
- {
- $key = md5(config('app.jwt'));
- $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
- );
- $jwt = JWT::encode($token, $key);
- return $jwt;
- }
- }
|