Client.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. # Oauth Client
  3. namespace Oauth\Lib;
  4. use Dever;
  5. use Dever\Session\Oper as Save;
  6. class Client
  7. {
  8. /**
  9. * @desc account
  10. * @var int
  11. */
  12. private $account = 1;
  13. /**
  14. * @desc save
  15. * @var object
  16. */
  17. private $save = null;
  18. /**
  19. * @desc request
  20. * @var array
  21. */
  22. private $request = null;
  23. /**
  24. * @desc config
  25. * @var array
  26. */
  27. private $config = null;
  28. public function __construct()
  29. {
  30. $this->initSave();
  31. $this->initRequest();
  32. $this->initAccount();
  33. $this->initRefer();
  34. $this->initConfig();
  35. }
  36. private function initSave()
  37. {
  38. $this->save = new Save(DEVER_PROJECT, 'session');
  39. }
  40. private function initRequest()
  41. {
  42. $this->request = Dever::input();
  43. }
  44. private function initAccount()
  45. {
  46. $this->account = (isset($this->request['account']) && $this->request['account']) ? $this->request['account'] : $this->save->get('oauth_account');
  47. $this->save->add('oauth_account', $this->account);
  48. }
  49. private function initRefer()
  50. {
  51. $this->refer = (isset($this->request['refer']) && $this->request['refer']) ? $this->request['refer'] : $this->save->get('oauth_refer');
  52. $this->save->add('oauth_refer', $this->refer);
  53. }
  54. private function initConfig()
  55. {
  56. $this->config = Dever::db('oauth/account')->one($this->account);
  57. if (!$this->config) {
  58. Dever::alert('账户错误');
  59. }
  60. $this->config += Dever::config($this->config['type'])->cAll;
  61. }
  62. /**
  63. * @desc oauth请求
  64. */
  65. public function auth()
  66. {
  67. $id = Dever::id();
  68. $this->save->add('oauth_id', $id);
  69. $this->param('auth', 'appid', $this->config['appid']);
  70. $this->param('auth', 'redirect_uri', Dever::url('request/callback?account=' . $this->account, 'oauth'));
  71. $this->param('auth', 'state', $id);
  72. $this->param('auth', 'response_type');
  73. $this->param('auth', 'scope');
  74. print_r($this->config['auth']);die;
  75. $url = $this->config['auth']['url'] . '?' . http_build_query($this->config['auth']['param']);
  76. Dever::location($url);
  77. }
  78. /**
  79. * @desc oauth请求 callback
  80. */
  81. public function callback($url = '')
  82. {
  83. if ((isset($this->request['js']) && $this->request['js'])) {
  84. $this->_js = false;
  85. }
  86. if ($this->_js == true && $url) {
  87. return $this->output($url);
  88. } else {
  89. $id = $this->save->get('oauth_id');
  90. if (isset($this->config['token']['param'])) {
  91. if (!$this->save->get('oauth_refresh')) {
  92. $this->param('token', 'code');
  93. $this->param('token', 'appid', $this->config['appid']);
  94. $this->param('token', 'secret', $this->config['appsecret']);
  95. $this->param('token', 'grant_type');
  96. $result = Dever::curl($this->config['token']['url'], $this->config['token']['param']);
  97. $result = Dever::json_decode($result);
  98. parse_str(http_build_query($result), $this->request);
  99. $this->response('token', 'access_token');
  100. $this->response('token', 'expires_in');
  101. $this->response('token', 'refresh_token');
  102. $this->response('token', 'openid');
  103. $this->response('token', 'unionid');
  104. $this->response('token', 'scope');
  105. $this->response('token', 'errcode');
  106. $this->response('token', 'errmsg');
  107. } else {
  108. # 由于refresh token是长期有效的,所以这里无需再次获取了。之后通过这个refresh获取access token就行了
  109. return;
  110. }
  111. }
  112. # 进入绑定流程吧
  113. $this->bind($data);
  114. }
  115. }
  116. /**
  117. * @desc 重新获取token
  118. * @author leo(suwi.bin)
  119. * @date 2012-08-27
  120. */
  121. protected function refresh()
  122. {
  123. $data = $this->request();
  124. $state = false;
  125. if(isset($data['token_refresh']) && $data['token_refresh'])
  126. {
  127. $this->param('refresh', 'refresh_token', $data['token_refresh']);
  128. $this->param('refresh', 'client_id', $this->_config['id']);
  129. $this->param('refresh', 'client_secret', $this->_config['key']);
  130. $return = json_decode($this->_curl('post', $this->param['refresh']), true);
  131. if(isset($return['error']))
  132. {
  133. Dever::alert('参数错误');
  134. }
  135. if(isset($return['access_token']) && $return['access_token'])
  136. {
  137. $update['token_code'] = $return['access_token'];
  138. $update['token_type'] = $return['token_type'];
  139. $update['token_time'] = $return['expires_in'];
  140. $state = $this->_update($update, $data['id']);
  141. }
  142. }
  143. return $state;
  144. }
  145. /**
  146. * @desc 绑定数据
  147. */
  148. private function bind()
  149. {
  150. $data = $this->config['token']['response'];
  151. $get = $this->request();
  152. $id = false;
  153. if(isset($get['id']) && $get['id'] > 0)
  154. {
  155. $id = $get['id'];
  156. }
  157. $this->_update($data, $id);
  158. # 跳转吧,从哪来去哪吧
  159. if($this->refer)
  160. {
  161. $refer = base64_decode($this->refer);
  162. Dever::location($refer);
  163. }
  164. }
  165. /**
  166. * @desc 输出js内容
  167. */
  168. private function output($url)
  169. {
  170. $html =
  171. '<script>
  172. var params = {}, queryString = location.hash.substring(1),
  173. regex = /([^&=]+)=([^&]*)/g, m;
  174. while (m = regex.exec(queryString))
  175. {
  176. params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  177. }
  178. location.href="'.$url.'&js=false&" + queryString;
  179. </script>';
  180. echo $html;die;
  181. }
  182. /**
  183. * @desc 请求参数
  184. */
  185. private function param($type, $key, $value = false)
  186. {
  187. $this->compatible($this->config[$type]['param'], $key, $value);
  188. }
  189. /**
  190. * @desc 响应参数
  191. */
  192. private function response($type, $key, $value = false)
  193. {
  194. $this->compatible($this->config[$type]['response'], $key, $value);
  195. }
  196. /**
  197. * @desc 兼容处理
  198. */
  199. private function compatible(&$param, $key, $value = false)
  200. {
  201. $default = false;
  202. if (isset($param[$key]) && is_array($param[$key])) {
  203. $nkey = $param[$key][0];
  204. $default = $param[$key][1];
  205. unset($param[$key]);
  206. $key = $nkey;
  207. } else {
  208. $default = $param[$key];
  209. }
  210. return $param[$key] = ($value ? $value : (isset($this->request[$key]) ? $this->request[$key] : $default));
  211. }
  212. }