Client.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. $result = $user;
  211. $result['uid'] = $user['id'];
  212. $result['signature'] = Dever::login($user['id']);
  213. return $result;
  214. }
  215. }
  216. /**
  217. * @desc 绑定数据
  218. */
  219. private function update($data, $id)
  220. {
  221. $uid = false;
  222. $info = Dever::load('passport/wechat-one', array('option_openid' => $data['openid']));
  223. $this->param('user', 'access_token', $data['access_token']);
  224. $this->param('user', 'openid', $data['openid']);
  225. $userinfo = Dever::json_decode(Dever::curl($this->config['user']['url'], $this->config['user']['param']));
  226. if (!$userinfo) {
  227. return;
  228. }
  229. //$user['bind'] = 1;
  230. $user['temp'] = 2;
  231. $user['username'] = Dever::emoji($userinfo['nickname']);
  232. if ($userinfo['headimgurl']) {
  233. //$update['set_avatar'] = $this->sessionAvatar($pic);
  234. $user['avatar'] = $userinfo['headimgurl'];
  235. }
  236. if ($userinfo['city']) {
  237. $user['city'] = $userinfo['city'];
  238. }
  239. if ($userinfo['province']) {
  240. $user['province'] = $userinfo['province'];
  241. }
  242. if ($userinfo['country']) {
  243. $user['country'] = $userinfo['country'];
  244. }
  245. if ($userinfo['country'] && $userinfo['province'] && $userinfo['city']) {
  246. $user['area'] = $userinfo['country'] .','. $userinfo['province'] .','. $userinfo['city'];
  247. }
  248. if (!$info) {
  249. if (isset($data['unionid']) && $data['unionid']) {
  250. $info = Dever::load('passport/wechat-one', array('unionid' => $data['unionid']));
  251. if (!$info) {
  252. $uid = false;
  253. } else {
  254. $uid = $info['uid'];
  255. }
  256. $wechat['unionid'] = $data['unionid'];
  257. }
  258. if (!$uid) {
  259. $user['source_type'] = 'service';
  260. if ($this->system) {
  261. $user['system_id'] = $this->system;
  262. }
  263. if ($this->source_type) {
  264. $user['source_type'] = $this->source_type;
  265. }
  266. $uid = Dever::load('passport/user-insert', $user);
  267. } else {
  268. $user['where_id'] = $uid;
  269. Dever::load('passport/user-update', $user);
  270. }
  271. $wechat['access_token'] = $data['access_token'];
  272. $wechat['openid'] = $data['openid'];
  273. $wechat['expires_in'] = $data['expires_in'];
  274. $wechat['refresh_token'] = $data['refresh_token'];
  275. $wechat['account_id'] = $this->account;
  276. if ($this->system) {
  277. $wechat['system_id'] = $this->system;
  278. }
  279. $wechat['uid'] = $uid;
  280. $wechat['type'] = 2;
  281. if ($this->source_type == 'ios') {
  282. $wechat['type'] = 3;
  283. } elseif ($this->source_type == 'android') {
  284. $wechat['type'] = 4;
  285. }
  286. $id = Dever::load('passport/wechat-insert', $wechat);
  287. if (Dever::project('source') && isset($this->source) && $this->source && $this->source > 0) {
  288. Dever::load('source/lib/core')->saveUser($id, $uid, $this->source, 'oauth', $this->account);
  289. }
  290. } else {
  291. $uid = $info['uid'];
  292. if (isset($data['unionid']) && $data['unionid']) {
  293. $wechat['unionid'] = $data['unionid'];
  294. # 判断用户是否存在,是否需要合并
  295. $wechat['uid'] = $this->passport->combine($uid, $data['unionid']);
  296. }
  297. $wechat['access_token'] = $data['access_token'];
  298. $wechat['openid'] = $data['openid'];
  299. $wechat['expires_in'] = $data['expires_in'];
  300. $wechat['refresh_token'] = $data['refresh_token'];
  301. $wechat['where_id'] = $info['id'];
  302. $wechat['account_id'] = $this->account;
  303. if ($this->system) {
  304. $wechat['system_id'] = $this->system;
  305. }
  306. $id = $info['id'];
  307. Dever::load('passport/wechat-update', $wechat);
  308. $user['where_id'] = $uid;
  309. Dever::load('passport/user-update', $user);
  310. }
  311. $this->passport->createUsername($uid, $user['username']);
  312. $user = Dever::load('passport/user-one', $uid);
  313. $this->save($user);
  314. return $user;
  315. }
  316. /**
  317. * @desc 输出js内容
  318. */
  319. private function output($url)
  320. {
  321. $html =
  322. '<script>
  323. var params = {}, queryString = location.hash.substring(1),
  324. regex = /([^&=]+)=([^&]*)/g, m;
  325. while (m = regex.exec(queryString))
  326. {
  327. params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  328. }
  329. location.href="'.$url.'&js=false&" + queryString;
  330. </script>';
  331. echo $html;die;
  332. }
  333. /**
  334. * @desc 请求参数
  335. */
  336. private function param($type, $key, $value = false)
  337. {
  338. $this->compatible($this->config[$type]['param'], $key, $value);
  339. }
  340. /**
  341. * @desc 响应参数
  342. */
  343. private function response($type, $key, $value = false)
  344. {
  345. $this->compatible($this->config[$type]['response'], $key, $value);
  346. }
  347. /**
  348. * @desc 兼容处理
  349. */
  350. private function compatible(&$param, $key, $value = false)
  351. {
  352. $default = false;
  353. if (isset($param[$key]) && is_array($param[$key])) {
  354. $nkey = $param[$key][0];
  355. $default = $param[$key][1];
  356. unset($param[$key]);
  357. $key = $nkey;
  358. } else {
  359. $default = $param[$key];
  360. }
  361. return $param[$key] = ($value ? $value : (isset($this->request[$key]) ? $this->request[$key] : $default));
  362. }
  363. }