This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(kernel): 🦄 response * feat(kuaishou): ✨ app * feat(kuaishou): ✨ oauth * feat(kuaishou): ✨ user_info
- Loading branch information
Showing
12 changed files
with
310 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Waset\Kuaishou; | ||
|
||
use Waset\Kernel\BaseApi; | ||
|
||
/** | ||
* 快手应用入口 | ||
* | ||
* @method function Oauth Oauth() | ||
*/ | ||
class Application extends BaseApi | ||
{ | ||
/** | ||
* 快手接口地址 | ||
*/ | ||
const BaseUrl = "https://open.kuaishou.com"; | ||
|
||
/** | ||
* 应用唯一标识 | ||
* | ||
* @var array | ||
*/ | ||
private array $config; | ||
|
||
/** | ||
* 应用唯一标识 | ||
* | ||
* @var string | ||
*/ | ||
public string $app_id = ''; | ||
|
||
/** | ||
* 应用唯一标识对应的密钥 | ||
* | ||
* @var string | ||
*/ | ||
public string $app_secret = ''; | ||
|
||
/** | ||
* 用户唯一标志 | ||
* | ||
* @var string | ||
*/ | ||
public string $open_id = ''; | ||
|
||
/** | ||
* 构造函数 | ||
* | ||
* @param array $config | ||
*/ | ||
public function __construct(array $config) | ||
{ | ||
$this->config = $config; | ||
$this->app_id = $config['app_id']; | ||
$this->app_secret = $config['app_secret']; | ||
$this->open_id = $config['open_id'] ?? ''; | ||
} | ||
|
||
/** | ||
* 静态魔术加载方法 | ||
* @param $name | ||
* @param $arguments | ||
* @return class | ||
*/ | ||
public function __call($name, $arguments) | ||
{ | ||
$name = ucfirst(strtolower($name)); | ||
$class = "\\Waset\\Kuaishou\\{$name}"; | ||
|
||
if (!empty($class) && class_exists($class)) { | ||
return new $class(array_merge($this->config, $arguments)); | ||
} else { | ||
throw new \Exception("{$name} is not exists"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Waset\Kuaishou; | ||
|
||
class Oauth extends Application | ||
{ | ||
/** | ||
* 快手授权码(code) | ||
* | ||
* 该接口只适用于快手获取动态二维码 | ||
* | ||
* 该URL不是用来请求的, 需要展示给用户用于扫码,在快手APP支持端内唤醒的版本内打开的话会弹出客户端原生授权页面。 | ||
* @see https://open.kuaishou.com/platform/openApi?menu=12 | ||
* | ||
* @param array $scope 应用授权作用域,多个授权作用域以英文逗号(,)分隔 | ||
* @param string $redirect_uri 授权成功后的回调地址,必须以http/https开头。 | ||
* @param string $state 用于保持请求和回调的状态 | ||
* @param string $type 返回值类型,可选img和path,选择img会返回二维码图片,选择path会返回二维码内容的地址 | ||
* @param boolean $debug 是否开启调试模式,开启后,在用户授权失败时,会在快手APP内显示调试信息,可以根据调试信息来调试.默认false | ||
* @return BaseApi | ||
*/ | ||
public function code(array $scope, string $redirect_uri, string $state = "", string $type = "path", bool $debug = false) | ||
{ | ||
$api_url = self::BaseUrl . '/oauth2/qr_code'; | ||
$params = [ | ||
// 应用唯一标识 | ||
'app_id' => $this->app_id, | ||
// 写死为'code'即可 | ||
'response_type' => 'code', | ||
'scope' => implode(',', $scope), | ||
'redirect_uri' => $redirect_uri, | ||
'type' => $type, | ||
'debug' => $debug | ||
]; | ||
|
||
if ($state) { | ||
$params['state'] = $state; | ||
} | ||
|
||
return $this->https_url($api_url, $params); | ||
} | ||
|
||
/** | ||
* 获取access_token | ||
* | ||
* 该接口用于获取用户授权凭证access_token | ||
* | ||
* 该接口适用于快手授权 | ||
* @see https://open.kuaishou.com/platform/openApi?menu=13 | ||
* | ||
* @param string $code | ||
* @return BaseApi | ||
*/ | ||
public function token(string $code) | ||
{ | ||
$api_url = self::BaseUrl . '/oauth2/access_token'; | ||
$params = [ | ||
'app_id' => $this->app_id, | ||
'app_secret' => $this->app_secret, | ||
'code' => $code, | ||
'grant_type' => 'authorization_code' | ||
]; | ||
|
||
$res = $this->https_get($api_url, $params)->toArray(); | ||
|
||
return $res; | ||
} | ||
|
||
/** | ||
* 刷新refresh_token | ||
* | ||
* 该接口用于刷新refresh_token的有效期 | ||
* | ||
* 该接口适用于快手授权 | ||
* | ||
* @see https://open.snssdk.com/platform/doc/6848806519174154248 | ||
* @param string $refresh_token | ||
* | ||
* @return BaseApi | ||
*/ | ||
public function renew_refresh_token(string $refresh_token) | ||
{ | ||
$api_url = self::BaseUrl . '/platform/oauth/renew_refresh_token/'; | ||
$params = [ | ||
'client_key' => $this->client_key, | ||
'client_secret' => $this->client_secret, | ||
'refresh_token' => $refresh_token | ||
]; | ||
|
||
$res = $this->https_post($api_url, $params)->toArray(); | ||
|
||
return $res; | ||
} | ||
|
||
/** | ||
* 刷新access_token | ||
* | ||
* 该接口用于刷新access_token的有效期 | ||
* | ||
* 该接口适用于快手授权 | ||
* | ||
* @see https://open.snssdk.com/platform/doc/6848806519174154248 | ||
* @param string $refresh_token | ||
* | ||
* @return BaseApi | ||
*/ | ||
public function refresh_token(string $refresh_token) | ||
{ | ||
$api_url = self::BaseUrl . '/oauth2/refresh_token'; | ||
$params = [ | ||
'app_id' => $this->app_id, | ||
'app_secret' => $this->app_secret, | ||
'refresh_token' => $refresh_token, | ||
'grant_type' => "refresh_token" | ||
]; | ||
|
||
$res = $this->https_post($api_url, $params)->toArray(); | ||
|
||
return $res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Waset\Kuaishou; | ||
|
||
class User extends Application | ||
{ | ||
/** | ||
* 获取用户信息 | ||
* @param string $access_token | ||
* @param string $openid | ||
* @return User | ||
*/ | ||
public function info($access_token, $openid) | ||
{ | ||
$api_url = self::BaseUrl . '/oauth/userinfo/'; | ||
$params = [ | ||
'access_token' => $access_token, | ||
'open_id' => $openid | ||
]; | ||
|
||
$res = $this->https_post($api_url, $params)->toArray(); | ||
|
||
return $res['user_info'] ?? throw new \Exception("快手获取用户失败", 1);; | ||
} | ||
} |
Oops, something went wrong.