<?php namespace app\api\controller; use app\common\library\Common; use app\common\model\Config; use app\common\model\LoginLog; use app\common\model\User; use app\common\controller\Api; use EasyWeChat\Factory; use Firebase\JWT\JWT; use think\Db; use think\facade\Validate; use app\common\library\WXBizDataCrypt; /** * @title 登录注册 * @controller Login * @group user */ class Login extends Api { /** * @title 授权登录 * @desc 授权登录 * @url /api/Login/wechat_login * @method POST * @tag 基础 * @header * @param name:code type:string require:1 desc:code * @param name:rawData type:string require:1 desc:rawData */ public function wechat_login(){ $code = input('code'); $rawData = input('rawData'); //用户信息 if (!$code) $this->error('code为空'); $result = User::wechatLogin($code,$rawData); if ($result['code']){ $this->success($result['msg'],$result['data']); }else{ $this->error($result['msg']); } } public function sav(){ $data['headimg']=input('avatarUrl'); $data['nickname']=input('nickName'); Db::table('q_user')->where('openid',input('openid'))->update($data); $this->success('操作成功'); } /** * @title 绑定手机号 * @desc 绑定手机号 * @url /api/Login/bind_phone * @method POST * @tag 基础 * @header name:Authorization require:1 default: desc:验证token * * * @param name:code type:string require:1 desc:code * @param name:iv type:string require:1 desc:iv * @param name:encryptedData type:string require:1 desc:encryptedData * */ public function bind_phone(){ $user_id = $this->check_login(); $code = input('code'); $appid = Config::get_values('small_wechat_id'); $secret = '01161e8b3dcee65960d0350131e14105'; $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code"; $session_key = input('session_key'); /*if (!empty($session_key['session_key'])) { $session_key = $session_key['session_key']; }else{ $this->error('获取session_key失败!'); }*/ $iv = input('iv'); $encryptedData = input('encryptedData'); $pc = new WXBizDataCrypt($appid, $session_key); $pc->decryptData($encryptedData, $iv, $data ); $array=json_decode($data, true); $result = User::bindPhone($user_id,$array); if ($result['code']){ $this->success($result['msg']); }else{ $this->error($result['msg']); } } /** * 小程序手机号授权登陆 */ public function wx_app_login(){ $config = [ 'app_id' => 'wx23528ccb517d264b', 'secret' => '01161e8b3dcee65960d0350131e14105', 'response_type' => 'array', ]; $app = Factory::miniProgram($config); if(app()->request->header('Authorization')){ $user_id = $this->check_login(); $avatar=input('avatarUrl'); $nickname=input('nickname'); dump($avatar); dump($nickname);die(); $res=Db::table('q_user')->where('id',$user_id)->update(['headimg'=>$avatar,'nickname'=>$nickname]); if(empty($res)){ $this->success('授权信息失败,请重新授权'); }else{ $this->success('登陆成功'); } } $code=input('code'); if(empty($code)){ $this->error('code参数错误'); } $data=$app->auth->session($code); $decryptedData = $app->encryptor->decryptData($data['session_key'], input('iv'), input('encryptedData')); $user=Db::table('q_user')->where('openid',$data['openid'])->find(); if(empty($user)){ $id=Db::table('q_user')->insertGetId(['openid'=>$data['openid'],'phone'=>$decryptedData['phoneNumber']]); $user=Db::table('q_user')->where('id',$id)->find(); } $token = JWT::encode($user,config('jwt.key')); $this->success('登陆成功',['token'=>$token]); } public function login(){ $config = [ 'app_id' => 'wx23528ccb517d264b', 'secret' => '01161e8b3dcee65960d0350131e14105', 'response_type' => 'array', ]; $app = Factory::miniProgram($config); $code=input('code'); if(empty($code)){ $this->error('code参数错误'); } $data=$app->auth->session($code); $decryptedData = $app->encryptor->decryptData($data['session_key'], input('iv'), input('encryptedData')); $user=Db::table('q_user')->where('openid',$data['openid'])->find(); if(empty($user)){ $id=Db::table('q_user')->insertGetId(['openid'=>$data['openid'],'phone'=>$decryptedData['phoneNumber']]); $user=Db::table('q_user')->where('id',$id)->find(); } $token = JWT::encode($user,config('jwt.key')); $this->success('登陆成功',['token'=>$token]); } }