Client.php 8.3 KB

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