ThinkOauth.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace login;
  3. abstract class ThinkOauth
  4. {
  5. /**
  6. * oauth版本
  7. * @var string
  8. */
  9. protected $Version = '3.0';
  10. /**
  11. * 申请应用时分配的app_key
  12. * @var string
  13. */
  14. protected $AppKey = '';
  15. /**
  16. * 申请应用时分配的 app_secret
  17. * @var string
  18. */
  19. protected $AppSecret = '';
  20. /**
  21. * 授权类型 response_type 目前只能为code
  22. * @var string
  23. */
  24. protected $ResponseType = 'code';
  25. /**
  26. * grant_type 目前只能为 authorization_code
  27. * @var string
  28. */
  29. protected $GrantType = 'authorization_code';
  30. /**
  31. * 回调页面URL 可以通过配置文件配置
  32. * @var string
  33. */
  34. protected $Callback = '';
  35. /**
  36. * 获取request_code的额外参数 URL查询字符串格式
  37. * @var srting
  38. */
  39. protected $Authorize = '';
  40. /**
  41. * 获取request_code请求的URL
  42. * @var string
  43. */
  44. protected $GetRequestCodeURL = '';
  45. /**
  46. * 获取access_token请求的URL
  47. * @var string
  48. */
  49. protected $GetAccessTokenURL = '';
  50. /**
  51. * API根路径
  52. * @var string
  53. */
  54. protected $ApiBase = '';
  55. /**
  56. * 授权后获取到的TOKEN信息
  57. * @var array
  58. */
  59. protected $Token = null;
  60. /**
  61. * 调用接口类型
  62. * @var string
  63. */
  64. private $Type = '';
  65. /**
  66. * 构造方法,配置应用信息
  67. * @param array $token
  68. */
  69. public function __construct($token = null)
  70. {
  71. //设置SDK类型
  72. $class = get_class($this);
  73. $this->Type = explode('\\', strtolower(substr($class, 0, strlen($class) - 3)))[2];
  74. //获取应用配置
  75. $connect = config('maccms.connect');
  76. $tmp = $connect["{$this->Type}"];
  77. unset($config);
  78. $config['APP_KEY'] = $tmp['key'];
  79. $config['APP_SECRET'] = $tmp['secret'];
  80. if (empty($config['APP_KEY']) || empty($config['APP_SECRET'])) {
  81. throw new \think\Exception('请配置您申请的APP_KEY和APP_SECRET', 100001);
  82. } else {
  83. $this->AppKey = $config['APP_KEY'];
  84. $this->AppSecret = $config['APP_SECRET'];
  85. $this->Token = $token; //设置获取到的TOKEN
  86. }
  87. }
  88. /**
  89. * 取得Oauth实例
  90. * @static
  91. * @return mixed 返回Oauth
  92. */
  93. public static function getInstance($type, $token = null)
  94. {
  95. $name = ucfirst(strtolower($type)) . 'SDK';
  96. if (class_exists("login\sdk\\{$name}")) {
  97. $class_name = "\\login\\sdk\\{$name}";
  98. return new $class_name($token);
  99. } else {
  100. throw new \think\Exception('CLASS_NOT_EXIST:' . $name, 100002);
  101. }
  102. }
  103. /**
  104. * 请求code
  105. */
  106. public function getRequestCodeURL()
  107. {
  108. $this->config();
  109. //Oauth 标准参数
  110. $params = array(
  111. 'client_id' => $this->AppKey,
  112. 'redirect_uri' => $this->Callback,
  113. 'response_type' => $this->ResponseType,
  114. );
  115. //获取额外参数
  116. if ($this->Authorize) {
  117. parse_str($this->Authorize, $_param);
  118. if (is_array($_param)) {
  119. $params = array_merge($params, $_param);
  120. } else {
  121. throw new \think\Exception('AUTHORIZE配置不正确!',100003);
  122. }
  123. }
  124. return $this->GetRequestCodeURL . '?' . http_build_query($params);
  125. }
  126. /**
  127. * 初始化配置
  128. */
  129. public function config()
  130. {
  131. $this->Callback = THIRD_LOGIN_CALLBACK . "{$this->Type}";
  132. if(empty($this->Callback)) {
  133. throw new \think\Exception('请配置回调页面地址', 100004);
  134. }
  135. }
  136. /**
  137. * 获取access_token
  138. * @param string $code 上一步请求到的code
  139. * $code = $_GET['code']
  140. */
  141. public function getAccessToken($code, $extend = null)
  142. {
  143. $this->config();
  144. $params = array(
  145. 'client_id' => $this->AppKey,
  146. 'client_secret' => $this->AppSecret,
  147. 'grant_type' => $this->GrantType,
  148. 'code' => $code,
  149. 'redirect_uri' => $this->Callback,
  150. );
  151. $data = $this->http($this->GetAccessTokenURL, $params, 'POST');
  152. $this->Token = $this->parseToken($data, $extend);
  153. return $this->Token;
  154. }
  155. /**
  156. * 发送HTTP请求方法,目前只支持CURL发送请求
  157. * @param string $url 请求URL
  158. * @param array $params 请求参数
  159. * @param string $method 请求方法GET/POST
  160. * @return array $data 响应数据
  161. */
  162. protected function http($url, $params, $method = 'GET', $header = array(), $multi = false)
  163. {
  164. $opts = array(
  165. CURLOPT_TIMEOUT => 30,
  166. CURLOPT_RETURNTRANSFER => 1,
  167. CURLOPT_SSL_VERIFYPEER => false,
  168. CURLOPT_SSL_VERIFYHOST => false,
  169. CURLOPT_HTTPHEADER => $header
  170. );
  171. /* 根据请求类型设置特定参数 */
  172. switch (strtoupper($method)) {
  173. case 'GET':
  174. $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
  175. break;
  176. case 'POST':
  177. //判断是否传输文件
  178. $params = $multi ? $params : http_build_query($params);
  179. $opts[CURLOPT_URL] = $url;
  180. $opts[CURLOPT_POST] = 1;
  181. $opts[CURLOPT_POSTFIELDS] = $params;
  182. break;
  183. default:
  184. throw new \think\Exception('不支持的请求方式!',100005);
  185. }
  186. /* 初始化并执行curl请求 */
  187. $ch = curl_init();
  188. curl_setopt_array($ch, $opts);
  189. $data = curl_exec($ch);
  190. $error = curl_error($ch);
  191. curl_close($ch);
  192. if ($error)
  193. throw new \think\Exception('请求发生错误:' . $error,100006);
  194. return $data;
  195. }
  196. /**
  197. * 抽象方法,在SNSSDK中实现
  198. * 解析access_token方法请求后的返回值
  199. */
  200. abstract protected function parseToken($result, $extend);
  201. /**
  202. * 抽象方法,在SNSSDK中实现
  203. * 获取当前授权用户的SNS标识
  204. */
  205. abstract public function openid();
  206. /**
  207. * 合并默认参数和额外参数
  208. * @param array $params 默认参数
  209. * @param array /string $param 额外参数
  210. * @return array:
  211. */
  212. protected function param($params, $param)
  213. {
  214. if (is_string($param))
  215. parse_str($param, $param);
  216. return array_merge($params, $param);
  217. }
  218. /**
  219. * 获取指定API请求的URL
  220. * @param string $api API名称
  221. * @param string $fix api后缀
  222. * @return string 请求的完整URL
  223. */
  224. protected function url($api, $fix = '')
  225. {
  226. return $this->ApiBase . $api . $fix;
  227. }
  228. /**
  229. * 抽象方法,在SNSSDK中实现
  230. * 组装接口调用参数 并调用接口
  231. */
  232. abstract protected function call($api, $param = '', $method = 'GET', $multi = false);
  233. }