Client.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. parse_str(http_build_query($result), $this->request);
  138. $this->response('token', 'access_token');
  139. $this->response('token', 'expires_in');
  140. $this->response('token', 'refresh_token');
  141. $this->response('token', 'openid');
  142. $this->response('token', 'unionid');
  143. $this->response('token', 'scope');
  144. $this->response('token', 'errcode');
  145. $this->response('token', 'errmsg');
  146. } else {
  147. # 由于refresh token是长期有效的,所以这里无需再次获取了。之后通过这个refresh获取access token就行了
  148. return;
  149. }
  150. }
  151. if (isset($this->config['token']['response'])) {
  152. # 进入绑定流程吧
  153. return $this->bind();
  154. }
  155. }
  156. Dever::alert('登录失败');
  157. }
  158. /**
  159. * @desc 重新获取token 暂时不用
  160. * @author leo(suwi.bin)
  161. * @date 2012-08-27
  162. */
  163. protected function refresh()
  164. {
  165. $data = $this->request;
  166. $state = false;
  167. if(isset($data['token_refresh']) && $data['token_refresh'])
  168. {
  169. $this->param('refresh', 'refresh_token', $data['token_refresh']);
  170. $this->param('refresh', 'client_id', $this->_config['id']);
  171. $this->param('refresh', 'client_secret', $this->_config['key']);
  172. $return = json_decode($this->_curl('post', $this->param['refresh']), true);
  173. if(isset($return['error']))
  174. {
  175. Dever::alert('参数错误');
  176. }
  177. if(isset($return['access_token']) && $return['access_token'])
  178. {
  179. $update['token_code'] = $return['access_token'];
  180. $update['token_type'] = $return['token_type'];
  181. $update['token_time'] = $return['expires_in'];
  182. $state = $this->update($update, $data['id']);
  183. }
  184. }
  185. return $state;
  186. }
  187. /**
  188. * @desc 绑定数据
  189. */
  190. private function bind()
  191. {
  192. $data = $this->config['token']['response'];
  193. $get = $this->request;
  194. $id = false;
  195. if (isset($get['id']) && $get['id'] > 0) {
  196. $id = $get['id'];
  197. }
  198. $user = $this->update($data, $id);
  199. # 跳转吧,从哪来去哪吧
  200. if ($this->refer) {
  201. $refer = urldecode($this->refer);
  202. Dever::location($refer);
  203. } else {
  204. $result = $user;
  205. $result['uid'] = $user['id'];
  206. $result['signature'] = Dever::login($user['id']);
  207. return $result;
  208. }
  209. }
  210. /**
  211. * @desc 绑定数据
  212. */
  213. private function update($data, $id)
  214. {
  215. $uid = false;
  216. $info = Dever::load('passport/wechat-one', array('option_openid' => $data['openid']));
  217. $this->param('user', 'access_token', $data['access_token']);
  218. $this->param('user', 'openid', $data['openid']);
  219. $userinfo = Dever::json_decode(Dever::curl($this->config['user']['url'], $this->config['user']['param']));
  220. if (!$userinfo) {
  221. return;
  222. }
  223. //$user['bind'] = 1;
  224. $user['temp'] = 2;
  225. $user['username'] = Dever::emoji($userinfo['nickname']);
  226. if ($userinfo['headimgurl']) {
  227. //$update['set_avatar'] = $this->sessionAvatar($pic);
  228. $user['avatar'] = $userinfo['headimgurl'];
  229. }
  230. if ($userinfo['city']) {
  231. $user['city'] = $userinfo['city'];
  232. }
  233. if ($userinfo['province']) {
  234. $user['province'] = $userinfo['province'];
  235. }
  236. if ($userinfo['country']) {
  237. $user['country'] = $userinfo['country'];
  238. }
  239. if ($userinfo['country'] && $userinfo['province'] && $userinfo['city']) {
  240. $user['area'] = $userinfo['country'] .','. $userinfo['province'] .','. $userinfo['city'];
  241. }
  242. if (!$info) {
  243. if (isset($data['unionid']) && $data['unionid']) {
  244. $info = Dever::load('passport/wechat-one', array('unionid' => $data['unionid']));
  245. if (!$info) {
  246. $uid = false;
  247. } else {
  248. $uid = $info['uid'];
  249. }
  250. $wechat['unionid'] = $data['unionid'];
  251. }
  252. if (!$uid) {
  253. $user['source_type'] = 'service';
  254. if ($this->system) {
  255. $user['system_id'] = $this->system;
  256. }
  257. if ($this->source_type) {
  258. $user['source_type'] = $this->source_type;
  259. }
  260. $uid = Dever::load('passport/user-insert', $user);
  261. } else {
  262. $user['where_id'] = $uid;
  263. Dever::load('passport/user-update', $user);
  264. }
  265. $wechat['access_token'] = $data['access_token'];
  266. $wechat['openid'] = $data['openid'];
  267. $wechat['expires_in'] = $data['expires_in'];
  268. $wechat['refresh_token'] = $data['refresh_token'];
  269. $wechat['account_id'] = $this->account;
  270. if ($this->system) {
  271. $wechat['system_id'] = $this->system;
  272. }
  273. $wechat['uid'] = $uid;
  274. $wechat['type'] = 2;
  275. if ($this->source_type == 'ios') {
  276. $wechat['type'] = 3;
  277. } elseif ($this->source_type == 'android') {
  278. $wechat['type'] = 4;
  279. }
  280. $id = Dever::load('passport/wechat-insert', $wechat);
  281. if (Dever::project('source') && isset($this->source) && $this->source && $this->source > 0) {
  282. Dever::load('source/lib/core')->saveUser($id, $uid, $this->source, 'oauth', $this->account);
  283. }
  284. } else {
  285. $uid = $info['uid'];
  286. if (isset($data['unionid']) && $data['unionid']) {
  287. $wechat['unionid'] = $data['unionid'];
  288. # 判断用户是否存在,是否需要合并
  289. $wechat['uid'] = $this->passport->combine($uid, $data['unionid']);
  290. }
  291. $wechat['access_token'] = $data['access_token'];
  292. $wechat['openid'] = $data['openid'];
  293. $wechat['expires_in'] = $data['expires_in'];
  294. $wechat['refresh_token'] = $data['refresh_token'];
  295. $wechat['where_id'] = $info['id'];
  296. $wechat['account_id'] = $this->account;
  297. if ($this->system) {
  298. $wechat['system_id'] = $this->system;
  299. }
  300. $id = $info['id'];
  301. Dever::load('passport/wechat-update', $wechat);
  302. $user['where_id'] = $uid;
  303. Dever::load('passport/user-update', $user);
  304. }
  305. $this->passport->createUsername($uid, $user['username']);
  306. $user = Dever::load('passport/user-one', $uid);
  307. $this->save($user);
  308. return $user;
  309. }
  310. /**
  311. * @desc 输出js内容
  312. */
  313. private function output($url)
  314. {
  315. $html =
  316. '<script>
  317. var params = {}, queryString = location.hash.substring(1),
  318. regex = /([^&=]+)=([^&]*)/g, m;
  319. while (m = regex.exec(queryString))
  320. {
  321. params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  322. }
  323. location.href="'.$url.'&js=false&" + queryString;
  324. </script>';
  325. echo $html;die;
  326. }
  327. /**
  328. * @desc 请求参数
  329. */
  330. private function param($type, $key, $value = false)
  331. {
  332. $this->compatible($this->config[$type]['param'], $key, $value);
  333. }
  334. /**
  335. * @desc 响应参数
  336. */
  337. private function response($type, $key, $value = false)
  338. {
  339. $this->compatible($this->config[$type]['response'], $key, $value);
  340. }
  341. /**
  342. * @desc 兼容处理
  343. */
  344. private function compatible(&$param, $key, $value = false)
  345. {
  346. $default = false;
  347. if (isset($param[$key]) && is_array($param[$key])) {
  348. $nkey = $param[$key][0];
  349. $default = $param[$key][1];
  350. unset($param[$key]);
  351. $key = $nkey;
  352. } else {
  353. $default = $param[$key];
  354. }
  355. return $param[$key] = ($value ? $value : (isset($this->request[$key]) ? $this->request[$key] : $default));
  356. }
  357. }