Client.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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->initSystem();
  48. $this->initSourceType();
  49. $this->initSystemSource();
  50. $this->initRefer();
  51. $this->initConfig();
  52. }
  53. private function initPassport()
  54. {
  55. $this->passport = Dever::load('passport/lib/base');
  56. }
  57. private function initSave()
  58. {
  59. $this->session = new Save(DEVER_PROJECT, 'session');
  60. }
  61. private function initRequest()
  62. {
  63. $this->request = Dever::input();
  64. }
  65. private function initAccount()
  66. {
  67. $this->account = (isset($this->request['account']) && $this->request['account']) ? $this->request['account'] : $this->session->get($this->prefix . 'oauth_account');
  68. $this->session->add($this->prefix . 'oauth_account', $this->account);
  69. }
  70. private function initSystem()
  71. {
  72. $this->system = (isset($this->request['system']) && $this->request['system']) ? $this->request['system'] : $this->session->get($this->prefix . 'oauth_system');
  73. $this->session->add($this->prefix . 'oauth_system', $this->system);
  74. }
  75. private function initSourceType()
  76. {
  77. $this->source_type = (isset($this->request['source_type']) && $this->request['source_type']) ? $this->request['source_type'] : $this->session->get($this->prefix . 'oauth_source_type');
  78. $this->session->add($this->prefix . 'oauth_source_type', $this->source_type);
  79. }
  80. private function initSystemSource()
  81. {
  82. $this->system_source = (isset($this->request['system_source']) && $this->request['system_source']) ? $this->request['system_source'] : $this->session->get($this->prefix . 'oauth_system_source');
  83. $this->session->add($this->prefix . 'oauth_system_source', $this->system_source);
  84. }
  85. private function initSource()
  86. {
  87. $this->source = (isset($this->request['source']) && $this->request['source']) ? $this->request['source'] : $this->session->get($this->prefix . 'oauth_source');
  88. $this->session->add($this->prefix . 'oauth_source', $this->source);
  89. }
  90. private function initRefer()
  91. {
  92. $this->refer = (isset($this->request['refer']) && $this->request['refer']) ? $this->request['refer'] : $this->session->get($this->prefix . 'oauth_refer');
  93. $this->session->add($this->prefix . 'oauth_refer', $this->refer);
  94. }
  95. private function initConfig()
  96. {
  97. $this->config = Dever::db('oauth/account')->one($this->account);
  98. if (!$this->config) {
  99. Dever::alert('账户错误');
  100. }
  101. $this->config += Dever::config($this->config['type'])->cAll;
  102. }
  103. /**
  104. * @desc oauth请求
  105. */
  106. public function auth()
  107. {
  108. $info = $this->info();
  109. if ($info) {
  110. if ($this->refer) {
  111. $refer = urldecode($this->refer);
  112. Dever::location($refer);
  113. } else {
  114. return true;
  115. }
  116. }
  117. $id = Dever::id();
  118. $this->session->add($this->prefix . 'oauth_id', $id);
  119. $this->param('auth', 'appid', $this->config['appid']);
  120. $this->param('auth', 'redirect_uri', Dever::url('request.callback?account=' . $this->account, 'oauth'));
  121. $this->param('auth', 'state', $id);
  122. $this->param('auth', 'response_type');
  123. $this->param('auth', 'scope');
  124. //print_r($this->config['auth']);die;
  125. $url = $this->config['auth']['url'] . '?' . http_build_query($this->config['auth']['param']);
  126. Dever::location($url);
  127. }
  128. /**
  129. * @desc oauth请求 callback
  130. */
  131. public function callback($url = '')
  132. {
  133. $this->js = true;
  134. if ((isset($this->request['js']) && $this->request['js'])) {
  135. $this->js = false;
  136. }
  137. if ($this->js == true && $url) {
  138. return $this->output($url);
  139. } else {
  140. $id = $this->session->get($this->prefix . 'oauth_id');
  141. if (isset($this->config['token']['param'])) {
  142. if (!$this->session->get($this->prefix . 'oauth_refresh')) {
  143. $this->param('token', 'code');
  144. $this->param('token', 'appid', $this->config['appid']);
  145. $this->param('token', 'secret', $this->config['appsecret']);
  146. $this->param('token', 'grant_type');
  147. $result = Dever::curl($this->config['token']['url'], $this->config['token']['param']);
  148. $result = Dever::json_decode($result);
  149. if (isset($result['errmsg'])) {
  150. Dever::alert($result['errmsg']);
  151. }
  152. parse_str(http_build_query($result), $this->request);
  153. $this->response('token', 'access_token');
  154. $this->response('token', 'expires_in');
  155. $this->response('token', 'refresh_token');
  156. $this->response('token', 'openid');
  157. $this->response('token', 'unionid');
  158. $this->response('token', 'scope');
  159. $this->response('token', 'errcode');
  160. $this->response('token', 'errmsg');
  161. } else {
  162. # 由于refresh token是长期有效的,所以这里无需再次获取了。之后通过这个refresh获取access token就行了
  163. return;
  164. }
  165. }
  166. if (isset($this->config['token']['response']) && $this->config['token']['response']) {
  167. # 进入绑定流程吧
  168. return $this->bind();
  169. }
  170. }
  171. Dever::alert('登录失败');
  172. }
  173. /**
  174. * @desc 重新获取token 暂时不用
  175. * @author leo(suwi.bin)
  176. * @date 2012-08-27
  177. */
  178. protected function refresh()
  179. {
  180. $data = $this->request;
  181. $state = false;
  182. if(isset($data['token_refresh']) && $data['token_refresh'])
  183. {
  184. $this->param('refresh', 'refresh_token', $data['token_refresh']);
  185. $this->param('refresh', 'client_id', $this->_config['id']);
  186. $this->param('refresh', 'client_secret', $this->_config['key']);
  187. $return = json_decode($this->_curl('post', $this->param['refresh']), true);
  188. if(isset($return['error']))
  189. {
  190. Dever::alert('参数错误');
  191. }
  192. if(isset($return['access_token']) && $return['access_token'])
  193. {
  194. $update['token_code'] = $return['access_token'];
  195. $update['token_type'] = $return['token_type'];
  196. $update['token_time'] = $return['expires_in'];
  197. $state = $this->update($update, $data['id']);
  198. }
  199. }
  200. return $state;
  201. }
  202. /**
  203. * @desc 绑定数据
  204. */
  205. private function bind()
  206. {
  207. $data = $this->config['token']['response'];
  208. if (!$data) {
  209. Dever::alert('错误的数据');
  210. }
  211. $get = $this->request;
  212. $id = false;
  213. if (isset($get['id']) && $get['id'] > 0) {
  214. $id = $get['id'];
  215. }
  216. $user = $this->update($data, $id);
  217. # 跳转吧,从哪来去哪吧
  218. if ($this->refer) {
  219. $refer = urldecode($this->refer);
  220. Dever::location($refer);
  221. } else {
  222. return $user;
  223. }
  224. }
  225. /**
  226. * @desc 绑定数据
  227. */
  228. private function update($data, $id)
  229. {
  230. $this->param('user', 'access_token', $data['access_token']);
  231. $this->param('user', 'openid', $data['openid']);
  232. $userinfo = Dever::json_decode(Dever::curl($this->config['user']['url'], $this->config['user']['param']));
  233. if (!$userinfo) {
  234. return;
  235. }
  236. //$user['bind'] = 1;
  237. $user['temp'] = 2;
  238. //$user['username'] = Dever::emoji($userinfo['nickname']);
  239. $user['username'] = $userinfo['nickname'];
  240. if ($userinfo['headimgurl']) {
  241. //$update['set_avatar'] = $this->sessionAvatar($pic);
  242. $user['avatar'] = $userinfo['headimgurl'];
  243. }
  244. if ($userinfo['city']) {
  245. $user['city'] = $userinfo['city'];
  246. }
  247. if ($userinfo['province']) {
  248. $user['province'] = $userinfo['province'];
  249. }
  250. if ($userinfo['country']) {
  251. $user['country'] = $userinfo['country'];
  252. }
  253. $user = $this->passport->wechat($data, $user, $this->account, $this->system, $this->source_type, $this->system_source, $this->source);
  254. return $user;
  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. }