thinkphp5.1下获取微信登录授权获取用户信息

后端代码

    public function index()
    {
        $code = input('param.code');
        $signature = input('param.signature');
        $rawData = input('param.rawData');
        $encryptedData = input('param.encryptedData');
        $iv = input('param.iv');
        $params = [
            'appid'=>config('app.program.appid'),
            'secret'=>config('app.program.appsecret'),
            'js_code'=>$code,
            'grant_type'=>'authorization_code'
        ];
        $url ="https://api.weixin.qq.com/sns/jscode2session?".http_build_query($params);
        $wx_result = getCurl($url);
        $result = json_decode(json_encode($wx_result),true);
        if (isset($result['errcode']) AND $result['errcode']) {
            return $this->apireturn('3000','用户登录信息异常');
        }
        $openid = $result['openid'];
        $session_key = $result['session_key'];
        $signature2 = sha1($rawData . $session_key);
        if ($signature != $signature2) {
            return $this->apireturn('3001','数据签名验证失败!');
        }
        $pc = new WXBizDataCrypt(config('app.program.appid'), $session_key);
        $errCode = $pc->decryptData($encryptedData, $iv, $data);
        if ($errCode == 0) {
            $data = json_decode($data,true);
            $CashierClient = new CashierClient;
            $check = $CashierClient->where('openId',$data['openId'])->find();
            if (!$check) {
                if (isset($data['watermark'])) {
                    unset($data['watermark']);
                }
                $CashierClient->save($data);
                // 添加登录状态
                $login_data = [
                    'user_id'=>$CashierClient->id
                ];
            }else{
                $login_data = [
                    'user_id'=>$check->id
                ];
            }
            Cache::store('redis')->set('session_key_'.$login_data['user_id'],$session_key,config('token_expire'));
            $token = $this->createToken($login_data);
            return $this->apireturn('1','登录成功',$token);
        } else {
            return $this->apireturn('3002','登录失败错误码:'+$errCode);
        }
    }

微信小程序代码

   登录请求 
    wx.login({
        success: res1 => {
          wx.getUserInfo({
            success: res2 => {
              console.log(res2.iv)
              that.setData({
                code: res1.code,//code
                rawData: res2.rawData,
                signature: res2.signature,
                encryptedData: res2.encryptedData,
                iv: res2.iv,
                authorizeHide: true,//授权窗
              })
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
              that.requireFun();//后台调用
            }
          })
        }
      });
后台数据请求
    wx.request({
      url: 'http://www.cashier.com/api/login',//后台url
      data: this.data,
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      success: res3 => {
        wx.hideLoading();
        wx.setStorageSync('tokens', res3.data.data)
      }
    })

 

你可能感兴趣的:(thinkphp)