appid = $config['appid']; $this->appsecret = $config['appsecret']; $this->token = $token; Config::set('user_agent', ''); } /** * @param $type * @param null $token * @return static */ public static function getInstance($type, $token = null) { $config = Config::get("oauth.{$type}"); $classname = '\\Kuxin\\Oauth\\' . $type; return Loader::instance($classname, [ $config, $token ]); } /** * 前往认证页 * * @param $url * @return string */ public function authorize($url) { $param = [ "response_type" => $this->responseType, "client_id" => $this->appid, "redirect_uri" => $url, "state" => time(), ]; $param = array_merge($param, $this->authorizeParam); if (strpos($this->getRequestCodeURL, '?') === false) { return $this->getRequestCodeURL . '?' . http_build_query($param); } else { return $this->getRequestCodeURL . '&' . http_build_query($param); } } /** * 获取access token * * @param $code * @param $url * @return string */ public function getAccessToken($code, $url) { $param = [ "grant_type" => $this->grantType, "client_id" => $this->appid, "redirect_uri" => $url, "client_secret" => $this->appsecret, "code" => $code, ]; $param = array_merge($param, $this->getTokenParam); $response = Http::post($this->getAccessTokenURL, http_build_query($param)); return $this->parseToken($response); } /** * @param string $api 接口名 * @param string $param 参数 * @param string $method 是否POST * @param bool $multi 是否上传文件 * @return array */ abstract protected function call($api, $param = '', $method = 'GET', $multi = false); /** * 抽象方法 解析access_token方法请求后的返回值 * * @param 待处理内容 * @return string */ abstract protected function parseToken($result); /** * 抽象方法 获取当前授权用户的标识 * * @return mixed */ abstract public function getOpenId(); /** * 获取用户信息 * * @return mixed */ abstract public function getInfo(); }