Client.php 8.6 KB

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