示例#1
0
 public function authenticate($url = NULL, $scope = 'snsapi_base', $bindUserRole = NULL)
 {
     $wechatUser = $this->getUser();
     if (!empty($wechatUser)) {
         return $wechatUser;
     }
     empty($url) && ($url = app('url')->full());
     $json = $this->api->getOauthAccessToken();
     if (empty($json)) {
         !empty($_GET['code']) && dd(app('url')->full(), $this->api->errCode, $this->getUser());
         $scope == 'hybrid' && ($scope = 'snsapi_base');
         //混杂模式下,第一次访问静默授权
         $oauth_url = $this->api->getOauthRedirect($url, $scope, $scope);
         throw new HttpResponseException(redirect($oauth_url));
         //\Illuminate\Http\RedirectResponse
     } else {
         $wechatUserTool = new WechatUserTool($this->api);
         $wechatUser = $wechatUserTool->updateWechatUser($json['openid'], $json['access_token'], $scope != 'hybrid');
         if ($scope == 'hybrid' && $_GET['state'] == 'snsapi_base' && empty($wechatUser['nickname'])) {
             $oauth_url = $this->api->getOauthRedirect($url, 'snsapi_userinfo', 'snsapi_userinfo');
             throw new HttpResponseException(redirect($oauth_url));
             //\Illuminate\Http\RedirectResponse
         }
         $this->setUser($wechatUser);
         if (!empty($bindUserRole)) {
             $user = $wechatUserTool->bindToUser($wechatUser, $bindUserRole, $scope != 'hybrid');
         }
     }
     return $this->getUser();
 }