Client.php 9.4 KB

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