Client.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. # Oauth Client
  3. namespace Oauth\Lib;
  4. use Dever;
  5. use Dever\Session\Oper as Save;
  6. use Passport\Lib\Base;
  7. class Client extends Base
  8. {
  9. /**
  10. * @desc account
  11. * @var int
  12. */
  13. private $account = 1;
  14. /**
  15. * @desc save
  16. * @var object
  17. */
  18. protected $session = null;
  19. /**
  20. * @desc passport
  21. * @var object
  22. */
  23. protected $passport = null;
  24. /**
  25. * @desc request
  26. * @var array
  27. */
  28. private $request = null;
  29. /**
  30. * @desc config
  31. * @var array
  32. */
  33. private $config = null;
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. $this->initPassport();
  38. $this->initSave();
  39. $this->initRequest();
  40. $this->initAccount();
  41. $this->initSource();
  42. $this->initSourceType();
  43. $this->initRefer();
  44. $this->initConfig();
  45. }
  46. private function initPassport()
  47. {
  48. $this->passport = Dever::load('passport/lib/base');
  49. }
  50. private function initSave()
  51. {
  52. $this->session = new Save(DEVER_PROJECT, 'session');
  53. }
  54. private function initRequest()
  55. {
  56. $this->request = Dever::input();
  57. }
  58. private function initAccount()
  59. {
  60. $this->account = (isset($this->request['account']) && $this->request['account']) ? $this->request['account'] : $this->session->get('oauth_account');
  61. $this->session->add('oauth_account', $this->account);
  62. }
  63. private function initSystem()
  64. {
  65. $this->system = (isset($this->request['system']) && $this->request['system']) ? $this->request['system'] : $this->session->get('oauth_system');
  66. $this->session->add('oauth_system', $this->system);
  67. }
  68. private function initSourceType()
  69. {
  70. $this->source_type = (isset($this->request['source_type']) && $this->request['source_type']) ? $this->request['source_type'] : $this->session->get('oauth_source_type');
  71. $this->session->add('oauth_source_type', $this->source_type);
  72. }
  73. private function initSource()
  74. {
  75. $this->source = (isset($this->request['source']) && $this->request['source']) ? $this->request['source'] : $this->session->get('oauth_source');
  76. $this->session->add('oauth_source', $this->source);
  77. }
  78. private function initRefer()
  79. {
  80. $this->refer = (isset($this->request['refer']) && $this->request['refer']) ? $this->request['refer'] : $this->session->get('oauth_refer');
  81. $this->session->add('oauth_refer', $this->refer);
  82. }
  83. private function initConfig()
  84. {
  85. $this->config = Dever::db('oauth/account')->one($this->account);
  86. if (!$this->config) {
  87. Dever::alert('账户错误');
  88. }
  89. $this->config += Dever::config($this->config['type'])->cAll;
  90. }
  91. /**
  92. * @desc oauth请求
  93. */
  94. public function auth()
  95. {
  96. $info = $this->info();
  97. if ($info) {
  98. if ($this->refer) {
  99. $refer = urldecode($this->refer);
  100. Dever::location($refer);
  101. } else {
  102. return true;
  103. }
  104. }
  105. $id = Dever::id();
  106. $this->session->add('oauth_id', $id);
  107. $this->param('auth', 'appid', $this->config['appid']);
  108. $this->param('auth', 'redirect_uri', Dever::url('request.callback?account=' . $this->account, 'oauth'));
  109. $this->param('auth', 'state', $id);
  110. $this->param('auth', 'response_type');
  111. $this->param('auth', 'scope');
  112. //print_r($this->config['auth']);die;
  113. $url = $this->config['auth']['url'] . '?' . http_build_query($this->config['auth']['param']);
  114. Dever::location($url);
  115. }
  116. /**
  117. * @desc oauth请求 callback
  118. */
  119. public function callback($url = '')
  120. {
  121. $this->js = true;
  122. if ((isset($this->request['js']) && $this->request['js'])) {
  123. $this->js = false;
  124. }
  125. if ($this->js == true && $url) {
  126. return $this->output($url);
  127. } else {
  128. $id = $this->session->get('oauth_id');
  129. if (isset($this->config['token']['param'])) {
  130. if (!$this->session->get('oauth_refresh')) {
  131. $this->param('token', 'code');
  132. $this->param('token', 'appid', $this->config['appid']);
  133. $this->param('token', 'secret', $this->config['appsecret']);
  134. $this->param('token', 'grant_type');
  135. $result = Dever::curl($this->config['token']['url'], $this->config['token']['param']);
  136. $result = Dever::json_decode($result);
  137. if (isset($result['errmsg'])) {
  138. Dever::alert($result['errmsg']);
  139. }
  140. parse_str(http_build_query($result), $this->request);
  141. $this->response('token', 'access_token');
  142. $this->response('token', 'expires_in');
  143. $this->response('token', 'refresh_token');
  144. $this->response('token', 'openid');
  145. $this->response('token', 'unionid');
  146. $this->response('token', 'scope');
  147. $this->response('token', 'errcode');
  148. $this->response('token', 'errmsg');
  149. } else {
  150. # 由于refresh token是长期有效的,所以这里无需再次获取了。之后通过这个refresh获取access token就行了
  151. return;
  152. }
  153. }
  154. if (isset($this->config['token']['response']) && $this->config['token']['response']) {
  155. # 进入绑定流程吧
  156. return $this->bind();
  157. }
  158. }
  159. Dever::alert('登录失败');
  160. }
  161. /**
  162. * @desc 重新获取token 暂时不用
  163. * @author leo(suwi.bin)
  164. * @date 2012-08-27
  165. */
  166. protected function refresh()
  167. {
  168. $data = $this->request;
  169. $state = false;
  170. if(isset($data['token_refresh']) && $data['token_refresh'])
  171. {
  172. $this->param('refresh', 'refresh_token', $data['token_refresh']);
  173. $this->param('refresh', 'client_id', $this->_config['id']);
  174. $this->param('refresh', 'client_secret', $this->_config['key']);
  175. $return = json_decode($this->_curl('post', $this->param['refresh']), true);
  176. if(isset($return['error']))
  177. {
  178. Dever::alert('参数错误');
  179. }
  180. if(isset($return['access_token']) && $return['access_token'])
  181. {
  182. $update['token_code'] = $return['access_token'];
  183. $update['token_type'] = $return['token_type'];
  184. $update['token_time'] = $return['expires_in'];
  185. $state = $this->update($update, $data['id']);
  186. }
  187. }
  188. return $state;
  189. }
  190. /**
  191. * @desc 绑定数据
  192. */
  193. private function bind()
  194. {
  195. $data = $this->config['token']['response'];
  196. if (!$data) {
  197. Dever::alert('错误的数据');
  198. }
  199. $get = $this->request;
  200. $id = false;
  201. if (isset($get['id']) && $get['id'] > 0) {
  202. $id = $get['id'];
  203. }
  204. $user = $this->update($data, $id);
  205. # 跳转吧,从哪来去哪吧
  206. if ($this->refer) {
  207. $refer = urldecode($this->refer);
  208. Dever::location($refer);
  209. } else {
  210. return $user;
  211. }
  212. }
  213. /**
  214. * @desc 绑定数据
  215. */
  216. private function update($data, $id)
  217. {
  218. $this->param('user', 'access_token', $data['access_token']);
  219. $this->param('user', 'openid', $data['openid']);
  220. $userinfo = Dever::json_decode(Dever::curl($this->config['user']['url'], $this->config['user']['param']));
  221. if (!$userinfo) {
  222. return;
  223. }
  224. //$user['bind'] = 1;
  225. $user['temp'] = 2;
  226. $user['username'] = Dever::emoji($userinfo['nickname']);
  227. if ($userinfo['headimgurl']) {
  228. //$update['set_avatar'] = $this->sessionAvatar($pic);
  229. $user['avatar'] = $userinfo['headimgurl'];
  230. }
  231. if ($userinfo['city']) {
  232. $user['city'] = $userinfo['city'];
  233. }
  234. if ($userinfo['province']) {
  235. $user['province'] = $userinfo['province'];
  236. }
  237. if ($userinfo['country']) {
  238. $user['country'] = $userinfo['country'];
  239. }
  240. $user = $this->passport->wechat($data, $user, $this->account, $this->system, $this->source_type, $this->source);
  241. return $user;
  242. }
  243. /**
  244. * @desc 输出js内容
  245. */
  246. private function output($url)
  247. {
  248. $html =
  249. '<script>
  250. var params = {}, queryString = location.hash.substring(1),
  251. regex = /([^&=]+)=([^&]*)/g, m;
  252. while (m = regex.exec(queryString))
  253. {
  254. params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  255. }
  256. location.href="'.$url.'&js=false&" + queryString;
  257. </script>';
  258. echo $html;die;
  259. }
  260. /**
  261. * @desc 请求参数
  262. */
  263. private function param($type, $key, $value = false)
  264. {
  265. $this->compatible($this->config[$type]['param'], $key, $value);
  266. }
  267. /**
  268. * @desc 响应参数
  269. */
  270. private function response($type, $key, $value = false)
  271. {
  272. $this->compatible($this->config[$type]['response'], $key, $value);
  273. }
  274. /**
  275. * @desc 兼容处理
  276. */
  277. private function compatible(&$param, $key, $value = false)
  278. {
  279. $default = false;
  280. if (isset($param[$key]) && is_array($param[$key])) {
  281. $nkey = $param[$key][0];
  282. $default = $param[$key][1];
  283. unset($param[$key]);
  284. $key = $nkey;
  285. } else {
  286. $default = $param[$key];
  287. }
  288. return $param[$key] = ($value ? $value : (isset($this->request[$key]) ? $this->request[$key] : $default));
  289. }
  290. }