Client.php 9.0 KB

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