123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?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');
- $pid = input('pid');
- $goods_id = input('goods_id');
- if(empty($code)) $this->error('参数错误1');
- $res = http_get('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('app.official_account')['appid'].'&secret='.config('app.official_account')['secret'].'&code='.$code.'&grant_type=authorization_code');
- $res = json_decode($res,true);
- $member = Db::name('store_member')->field('id,phone')->where('openid', $res['openid'])->find();
- if (empty($member)) {
- $user_info = http_get('https://api.weixin.qq.com/sns/userinfo?access_token='.$res['access_token'].'&openid='.$res['openid']);
- $user_info = json_decode($user_info,true);
- $member_data = array(
- 'openid' => $user_info['openid'],
- 'headimg' => $user_info['headimgurl'],
- 'name' => $user_info['nickname'],
- 'pid' => $pid,
- 'create_at' => date("Y-m-d H:i:s")
- );
- Db::table('store_member')->insert($member_data);
- $uid = Db::getLastInsID();
-
- if ($pid && $goods_id) {
- $invite_info = [
- 'user_id' => $uid,
- 'pid' => $pid,
- 'goods_id' => $goods_id,
- ];
- Db::table('invite_info')->insert($invite_info);
- }
- } else {
- $uid = $member['id'];
- }
- if (empty($uid)) $this->error('数据有误');
- $token = self::create_jwt($uid);
- $this->success('登录成功', ['token' => $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;
- }
-
- public function programLogin()
- {
- $code = input('post.code');
- $encryptedData = input('post.encrypted');
- $iv = input('post.iv');
- $pid = input('post.pid',0);
- $goods_id = input('post.goods_id',0);
- if (empty($code)) $this->error('参数错误');
- $app = Factory::miniProgram(config('app.mini_program'));
- $data = $app->auth->session($code);
-
- if (empty($data['openid'])) $this->error('微信登录失败,未获取到openid');
- $member_id = Db::name('store_member')->field('id')->where('openid', $data['openid'])->value('id');
- if ($member_id) $this->success('登录成功',['token'=>self::create_jwt($member_id)]);
- require_once env('root_path') . 'vendor/zoujingli/wechat-developer/WeMini/crypt/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('微信登录失败2');
- $info = json_decode($info, true);
- $phone = $info['purePhoneNumber'];
- if (empty($phone)) $this->error('微信登录失败3');
- $member = Db::name('store_member')->field('id,phone,openid')->where('phone', $phone)->find();
- if(!empty($member)) $this->error('该手机号已绑定账号');
- $member_data = [
- 'phone' => $phone,
- 'headimg' =>'',
- 'openid' => $data['openid'],
- 'pid' => $pid,
- 'create_at' => date("Y-m-d H:i:s")
- ];
- Db::table('store_member')->insert($member_data);
- $uid = Db::getLastInsID();
- Db::table('store_member')->where('id',$uid)->update(['name'=>'HG'.$uid]);
-
- if ($pid && $goods_id) {
- $invite_info = [
- 'user_id' => $uid,
- 'pid' => $pid,
- 'goods_id' => $goods_id
- ];
- Db::table('invite_info')->insert($invite_info);
- }
- $this->success('登录成功',['token'=>self::create_jwt($uid)]);
- }
- }
|