Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

188 linhas
4.0 KiB

  1. <?php
  2. namespace Kuxin\Oauth;
  3. use Kuxin\Config;
  4. use Kuxin\Helper\Http;
  5. use Kuxin\Loader;
  6. abstract class Oauth
  7. {
  8. /**
  9. * 申请应用时分配的app_key
  10. *
  11. * @var string
  12. */
  13. protected $appid = '';
  14. /**
  15. * 申请应用时分配的 app_secret
  16. *
  17. * @var string
  18. */
  19. protected $appsecret = '';
  20. /**
  21. * 授权类型 response_type 目前只能为code
  22. *
  23. * @var string
  24. */
  25. protected $responseType = 'code';
  26. /**
  27. * grant_type 目前只能为 authorization_code
  28. *
  29. * @var string
  30. */
  31. protected $grantType = 'authorization_code';
  32. /**
  33. * 获取request_code的额外参数
  34. *
  35. * @var array
  36. */
  37. protected $authorizeParam = [];
  38. /**
  39. * 获取accesstoekn时候的附加参数
  40. *
  41. * @var array
  42. */
  43. protected $getTokenParam = [];
  44. /**
  45. * 获取request_code请求的URL
  46. *
  47. * @var string
  48. */
  49. protected $getRequestCodeURL = '';
  50. /**
  51. * 获取access_token请求的URL
  52. *
  53. * @var string
  54. */
  55. protected $getAccessTokenURL = '';
  56. /**
  57. * API根路径
  58. *
  59. * @var string
  60. */
  61. protected $apiBase = '';
  62. /**
  63. * 授权后获取到的TOKEN信息
  64. *
  65. * @var array
  66. */
  67. protected $token = null;
  68. /**
  69. * 授权后的用户id
  70. *
  71. * @var null
  72. */
  73. protected $openid = null;
  74. /**
  75. * 构造函数
  76. *
  77. * @param array $config
  78. * @param null $token
  79. */
  80. public function __construct(array $config, $token = null)
  81. {
  82. $this->appid = $config['appid'];
  83. $this->appsecret = $config['appsecret'];
  84. $this->token = $token;
  85. Config::set('user_agent', '');
  86. }
  87. /**
  88. * @param $type
  89. * @param null $token
  90. * @return static
  91. */
  92. public static function getInstance($type, $token = null)
  93. {
  94. $config = Config::get("oauth.{$type}");
  95. $classname = '\\Kuxin\\Oauth\\' . $type;
  96. return Loader::instance($classname, [ $config, $token ]);
  97. }
  98. /**
  99. * 前往认证页
  100. *
  101. * @param $url
  102. * @return string
  103. */
  104. public function authorize($url)
  105. {
  106. $param = [
  107. "response_type" => $this->responseType,
  108. "client_id" => $this->appid,
  109. "redirect_uri" => $url,
  110. "state" => time(),
  111. ];
  112. $param = array_merge($param, $this->authorizeParam);
  113. if (strpos($this->getRequestCodeURL, '?') === false) {
  114. return $this->getRequestCodeURL . '?' . http_build_query($param);
  115. } else {
  116. return $this->getRequestCodeURL . '&' . http_build_query($param);
  117. }
  118. }
  119. /**
  120. * 获取access token
  121. *
  122. * @param $code
  123. * @param $url
  124. * @return string
  125. */
  126. public function getAccessToken($code, $url)
  127. {
  128. $param = [
  129. "grant_type" => $this->grantType,
  130. "client_id" => $this->appid,
  131. "redirect_uri" => $url,
  132. "client_secret" => $this->appsecret,
  133. "code" => $code,
  134. ];
  135. $param = array_merge($param, $this->getTokenParam);
  136. $response = Http::post($this->getAccessTokenURL, http_build_query($param));
  137. return $this->parseToken($response);
  138. }
  139. /**
  140. * @param string $api 接口名
  141. * @param string $param 参数
  142. * @param string $method 是否POST
  143. * @param bool $multi 是否上传文件
  144. * @return array
  145. */
  146. abstract protected function call($api, $param = '', $method = 'GET', $multi = false);
  147. /**
  148. * 抽象方法 解析access_token方法请求后的返回值
  149. *
  150. * @param 待处理内容
  151. * @return string
  152. */
  153. abstract protected function parseToken($result);
  154. /**
  155. * 抽象方法 获取当前授权用户的标识
  156. *
  157. * @return mixed
  158. */
  159. abstract public function getOpenId();
  160. /**
  161. * 获取用户信息
  162. *
  163. * @return mixed
  164. */
  165. abstract public function getInfo();
  166. }