Client.php 9.5 KB

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