网页授权

微信统一授权代码
    /**
     * 微信授权
     *
     * @param string $platform
     * @return ResponseInterface
     */
    public function weixin(string $platform): ResponseInterface
    {
        $code = $this->request->input('code');
        $callbackUrl = $this->request->input('callback_url');

        $app = $this->getApp('wxb240867e502343fe', '6fe79c22fbfdb7fb49484f027879d95f');
        $oauth = $app->oauth;

        if ($code === null || $code === '') {
            $authUrl = $oauth->scopes(['snsapi_userinfo'])->redirect($this->request->fullUrl())->getTargetUrl();

            return $this->response->redirect($authUrl);
        }

        $accessToken = $oauth->getAccessToken($code);
        $userInfo = $oauth->user($accessToken)->toArray()['original'];
        $userInfo['oauth_source'] = WechatUser::OAUTH_WEI_XIN;
        $userInfo['platform'] = $platform;
        $oauthToken = $this->encryptOAuthToken($userInfo);

        // 跳转回目标页,兼容带GET参数的url
        $link = isset(parse_url($callbackUrl)['query']) ? '&' : '?';
        $url = "{$callbackUrl}{$link}oauth_token={$oauthToken}";

        return $this->response->redirect($url);
    }
WEB应用入口
    /**
     * 获取用户信息
     * 
     * @return ResponseInterface
     */
    public function userInfo(): ResponseInterface
    {
        $input = $this->request->all();

        if (isset($input['oauth_token'])) {
            // 授权成功获取用户信息
            // 如果是其他服务调用,可使用http请求统一授权服务,进行oauth_token解密
            $input['original'] = $this->decryptOAuthToken($input['oauth_token']);
        }

        return $this->success($input);
    }

你可能感兴趣的:(网页授权)