Oauth.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. /**
  3. * Oauth.php UTF-8
  4. *
  5. *
  6. * @date : 2018/4/24 13:39
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\member;
  13. use huo\controller\request\Channel;
  14. use huo\controller\request\Device;
  15. use huo\controller\request\Game;
  16. use huo\controller\request\Mem;
  17. use huo\model\member\MemoauthModel;
  18. use huolib\constant\FromConst;
  19. use huolib\constant\MemConst;
  20. use huolib\constant\OauthConst;
  21. use huolib\oauth\OAuth as OAuthLib;
  22. use huolib\status\MemberStatus;
  23. use huolib\tool\StrUtils;
  24. use huomp\controller\game\GameMini;
  25. use huomp\model\weixin\MpConfModel;
  26. use think\Db;
  27. use think\Exception;
  28. use think\Log;
  29. class Oauth extends Member {
  30. protected $domain = MPAPISITE;
  31. protected $oauth_data
  32. = [
  33. 'openid' => '',/* openid */
  34. 'unionid' => '',/* 用户ID */
  35. 'channel' => '',/* 通道 qq*/
  36. 'nickname' => '', /* 昵称 */
  37. 'gender' => '3',/* 性别 */
  38. 'avatar' => ''/* 头像 */
  39. ];
  40. /**
  41. * 获取配置文件
  42. */
  43. public function getOauthConf() {
  44. $_data_wx = [
  45. 'type' => 2,
  46. 'app_id' => '',
  47. 'app_secret' => '',
  48. 'redirect_url' => ''
  49. ];
  50. $_rdata[] = $_data_wx;
  51. return $_rdata;
  52. }
  53. /**
  54. * @param string $type 第三方登陆类型
  55. * @param string $from
  56. *
  57. * @param string $url 需要跳转回的地址
  58. *
  59. * @param string $wx_app_id
  60. *
  61. * @return mixed
  62. */
  63. public function getRequestCodeUrl($type, $from = 'pc', $url = '', $wx_app_id = '') {
  64. $_check_type = $this->checkType($type);
  65. if (MemberStatus::NO_ERROR != $_check_type) {
  66. return $this->retErrMsg($_check_type);
  67. }
  68. try {
  69. $_type = $type;
  70. if (FromConst::FROM_WEIXIN != $from && OauthConst::OAUTH_WEIXIN == $type) {
  71. $_type = OauthConst::OAUTH_WXQRCODE;
  72. }
  73. $_config = [];
  74. $_wx_app_secret = (new MpConfModel())->getSecretByMpId($wx_app_id);
  75. if (!empty($_wx_app_secret)) {
  76. $_config['APP_KEY'] = $wx_app_id;
  77. $_config['APP_SECRET'] = $_wx_app_secret;
  78. $_config['CALLBACK'] = '';
  79. }
  80. $_oauth_class = OAuthLib::ins($_type, $_config);
  81. $_back_url = $this->getCallbackUrl($type);
  82. if (!empty($wx_app_id)) {
  83. $_back_url .= '/'.$wx_app_id;
  84. }
  85. $_callback = StrUtils::getUrl($_back_url);
  86. $_callback .= 'back_url='.urlencode($url).'&token='.session_id();
  87. $_oauth_class->setCallback($_callback);
  88. $_oauth_class->setState();
  89. $_url = $_oauth_class->getRequestCodeUrl($from);
  90. $_rdata['url'] = $_url;
  91. return $this->retSucMsg(MemberStatus::NO_ERROR, $_rdata);
  92. } catch (Exception $_e) {
  93. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&type='.$type
  94. .'&from='.$from.'&url'.$url.'&wx_app_id='.$wx_app_id.'&Exception'
  95. .$_e->getMessage();
  96. Log::write($_err_msg, Log::ERROR);
  97. return $this->retErrMsg(MemberStatus::UNKNOWN_ERROR);
  98. }
  99. }
  100. /**
  101. * @param $type
  102. *
  103. * @return bool
  104. */
  105. public function checkType($type) {
  106. switch ($type) {
  107. case OauthConst::OAUTH_QQ:
  108. case OauthConst::OAUTH_WEIBO:
  109. case OauthConst::OAUTH_WEIXIN:
  110. return MemberStatus::NO_ERROR;
  111. default:
  112. return MemberStatus::OAUTH_TYPE_ERROR;
  113. }
  114. }
  115. /**
  116. * @param $type
  117. *
  118. * @return string
  119. */
  120. public function getCallbackUrl($type) {
  121. switch ($type) {
  122. case OauthConst::OAUTH_QQ:
  123. $_cb_url = url('sdk/Oauth/callbackQq', '', false, $this->domain);
  124. break;
  125. case OauthConst::OAUTH_WEIBO:
  126. $_cb_url = url('sdk/Oauth/callbackWeibo', '', false, $this->domain);
  127. break;
  128. case OauthConst::OAUTH_WEIXIN:
  129. $_cb_url = url('sdk/Oauth/callbackWeixin', '', false, $this->domain);
  130. break;
  131. default:
  132. $_cb_url = url('sdk/Oauth/callbackWeixin', '', false, $this->domain);
  133. }
  134. return $_cb_url;
  135. }
  136. /**
  137. * 通过code获取登陆信息
  138. *
  139. * @param $type
  140. * @param $code
  141. * @param $from
  142. * @param Game $game_rq
  143. * @param Channel $channel
  144. * @param Device $device
  145. * @param Mem $member
  146. *
  147. * @param array $_conf
  148. *
  149. * @return array|mixed
  150. */
  151. public function oauthLoginByCode(
  152. $type, $code, $from,
  153. Game $game_rq, Channel $channel, Device $device, Mem $member, $_conf = []
  154. ) {
  155. $_oauth_data = $this->getOauthDataByCode($type, $code, $from, $game_rq->getHAppId(), $_conf);
  156. if (is_numeric($_oauth_data)) {
  157. $_mem_id = $_oauth_data;
  158. $_mo_model = new MemoauthModel();
  159. $_mo_data = $_mo_model->getInfoByOpenId($type, $this->oauth_data['openid']);
  160. if (empty($_mo_data)) {
  161. $this->addOauth($_mem_id, $type, $this->oauth_data, $game_rq->getHAppId(), $_conf);
  162. }
  163. /* 登陆成功 */
  164. $_mem_data = MemCache::ins()->getInfoById($_mem_id);
  165. $_mem_data['guided_agent_id'] = $channel->getAgentId();
  166. $_rdata = $this->getReturnData($_mem_data, $game_rq->getHAppId(), $device->getDeviceType());
  167. $this->insertLoginLog($game_rq, $channel, $device, $member, $_rdata);
  168. return $this->retSucMsg(MemberStatus::NO_ERROR, $_rdata);
  169. } elseif (is_array($_oauth_data)) {
  170. /* 用户信息 查询openid是否对应玩家ID */
  171. $_mo_model = new MemoauthModel();
  172. $_mo_data = $_mo_model->getInfoByOpenId($type, $_oauth_data['openid']);
  173. $_mem_id = isset($_mo_data['mem_id']) ? $_mo_data['mem_id'] : 0;
  174. if (empty($_mo_data['id']) && empty($_mem_id)) {
  175. /* 玩家注册 */
  176. $_mem_data = $this->oAuthReg($type, $_oauth_data, $game_rq, $channel, $device, $member, $_conf);
  177. $member->setIsReg(MemConst::MEM_IS_REG);
  178. $_mem_data['is_reg'] = MemConst::MEM_IS_REG;
  179. /* LTV统计 */
  180. $_ltv_class = new \ltv\Ltv();
  181. $_ltv_class->reg($_mem_data['app_id'], $_mem_data['agent_id'], $_mem_data['create_time']);
  182. /* 生成支付小程序码 */
  183. } elseif (empty($_mo_data['id']) && !empty($_mem_id)) {
  184. /* 登陆成功 */
  185. $this->addOauth($_mem_id, $type, $_oauth_data, $game_rq->getHAppId(), $_conf);
  186. $_mem_data = MemCache::ins()->getInfoById($_mem_id);
  187. } else {
  188. /* 有第三方信息 未绑定玩家 */
  189. $_mem_id = $this->memReg($type, $_oauth_data, $game_rq, $channel, $device, $member);
  190. $_mo_data['mem_id'] = $_mem_id;
  191. $_mo_model->updateOauth($type, $_oauth_data['openid'], $_mo_data);
  192. $_mem_data = MemCache::ins()->getInfoById($_mem_id);
  193. }
  194. $_mem_data['guided_agent_id'] = $channel->getAgentId();
  195. $_rdata = $this->getReturnData($_mem_data, $game_rq->getHAppId(), $device->getDeviceType());
  196. $this->insertLoginLog($game_rq, $channel, $device, $member, $_rdata);
  197. return $this->retSucMsg(MemberStatus::NO_ERROR, $_rdata);
  198. }
  199. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&oauth_data='
  200. .json_encode($_oauth_data).'&data='.json_encode(
  201. array($type, $code, $from, $game_rq->getHAppId(), $_conf)
  202. );
  203. Log::write($_err_msg, Log::ERROR);
  204. return $this->retErrMsg(MemberStatus::UNKNOWN_ERROR);
  205. }
  206. /**
  207. * @param string $type 登陆类型
  208. * @param string $code
  209. * @param string $from
  210. *
  211. * @param int $app_id
  212. *
  213. * @param array $_conf
  214. *
  215. * @return array|bool|string array 表示用户所有信息 false 表示获取失败 string 为玩家ID
  216. */
  217. public function getOauthDataByCode($type, $code, $from, $app_id = 0, $_conf = []) {
  218. //Log::error('$type$type$type'.json_encode($type));
  219. try {
  220. if (OauthConst::OAUTH_MP == $type) {
  221. $_conf = (new GameMini())->getLoginConf($app_id);
  222. if (empty($_conf)) {
  223. return false;
  224. }
  225. }
  226. $_oauth_class = OAuthLib::ins($type, $_conf);
  227. $_oauth_class->setCallback($this->getCallbackUrl($type));
  228. $_token = $_oauth_class->getAccessToken($code, $from);
  229. /* Session 设置 设置第三方登陆Session */
  230. // (new HuoSession())->setOauthSession($_token);
  231. $_open_id = $_token['openid'];
  232. $_oauth_data = $_oauth_class->getUserInfo();
  233. $this->oauth_data = $_oauth_data;
  234. //Log::error('$_oauth_data111'.json_encode($_oauth_data));
  235. $_mo_model = new MemoauthModel();
  236. $_mo_data = $_mo_model->getInfoByOpenId($type, $_open_id);
  237. if (!empty($_mo_data['id'])) { // 老玩家
  238. $_mem_id = $_mo_data['mem_id'];
  239. /* 更新信息 */
  240. if (($_mo_data['unionid'] == $_mo_data['openid'] && $_oauth_data['unionid'] != $_oauth_data['openid'])
  241. || 0 == $_mem_id) {
  242. $_mem_id = $this->getMemIdByUnionId($type, $_oauth_data['unionid']);
  243. /* 插入更多信息 */
  244. $_mo_data['unionid'] = $_oauth_data['unionid'];
  245. $_mo_data['nickname'] = $_oauth_data['nickname'];
  246. $_mo_data['gender'] = $_oauth_data['gender'];
  247. $_mo_data['avatar'] = $_oauth_data['avatar'];
  248. $_mo_data['status'] = MemConst::STATUS_NORMAL;
  249. $_mo_data['expires_in'] = $_token['expires_in'];
  250. $_mo_data['mem_id'] = $_mem_id;
  251. $_mo_data['last_login_ip'] = get_client_ip(0, true);
  252. $_mem_data['avatar'] = $_mo_data['avatar'];
  253. $_mem_data['nickname'] = $_mo_data['nickname'];
  254. $_mem_data['status'] = MemConst::STATUS_NORMAL;
  255. MemCache::ins()->updateMem($_mem_id, $_mem_data);
  256. }
  257. $_mo_data['access_token'] = $_token['access_token'];
  258. $_mo_data['expires_in'] = $_token['expires_in'];
  259. $_mo_data['last_login_ip'] = get_client_ip(0, true);
  260. $_mo_data['last_login_time'] = time();
  261. if(empty($_mo_data['conf_id'])){
  262. if (!empty($app_id)) {
  263. $_mp_id = (new GameMini())->getMiniIdByAppId($app_id);
  264. $_mo_data['conf_id'] = (new MpConfModel())->getIdByMpId($_mp_id);
  265. }
  266. }
  267. $_mo_model->updateOauth($type, $_open_id, $_mo_data);
  268. if (empty($_mem_id)) {
  269. return $_oauth_data;
  270. }
  271. return $_mem_id;
  272. }
  273. return $_oauth_data;
  274. } catch (Exception $_e) {
  275. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&Exception'
  276. .$_e->getMessage().'&code'.$_e->getCode();
  277. Log::write($_err_msg, Log::ERROR);
  278. return false;
  279. }
  280. }
  281. /**
  282. * @param $type
  283. * @param $oauth_data
  284. * @param Game $game_rq
  285. * @param Channel $channel
  286. * @param Device $device
  287. * @param Mem $member
  288. * @param array $conf
  289. *
  290. * @return array|bool|mixed
  291. */
  292. public function oAuthReg(
  293. $type, $oauth_data, Game $game_rq, Channel $channel, Device $device, Mem $member, $conf = []
  294. ) {
  295. $_mem_id = $this->memReg($type, $oauth_data, $game_rq, $channel, $device, $member);
  296. $_rs = $this->addOauth($_mem_id, $type, $oauth_data, $game_rq->getHAppId(), $conf);
  297. if (false != $_rs) {
  298. return MemCache::ins()->getInfoById($_mem_id);
  299. }
  300. return false;
  301. }
  302. /**
  303. * 玩家主表注册
  304. *
  305. * @param $type
  306. * @param $oauth_data
  307. * @param Game $game_rq
  308. * @param Channel $channel
  309. * @param Device $device
  310. * @param Mem $member
  311. *
  312. * @return string
  313. */
  314. public function memReg($type, $oauth_data, Game $game_rq, Channel $channel, Device $device, Mem $member) {
  315. $_username = $this->genUsername($type);
  316. $_password = StrUtils::getRandChars(8);
  317. $member->setUsername($_username);
  318. $member->setPassword($_password);
  319. $member->setAvatar($oauth_data['avatar']);
  320. $member->setNickname($oauth_data['nickname']);
  321. /* Modified by ouzhongfu BEGIN 2020/4/9 ISSUES:11805 第三方注册设置为试玩账号,修改密码时不需输入原密码 */
  322. /*if (empty($oauth_data['nickname'])) {
  323. $member->setStatus(MemConst::STATUS_TRY);
  324. }*/
  325. $member->setStatus(MemConst::STATUS_TRY);
  326. /* END 2020/4/9 ISSUES:11805 */
  327. $_mem_id = $this->getMemIdByUnionId($type, $oauth_data['unionid']);
  328. if (empty($_mem_id)) {
  329. $_mem_data = (new Register())->reg($game_rq, $channel, $device, $member);
  330. $_mem_id = $_mem_data['id'];
  331. }
  332. return $_mem_id;
  333. }
  334. private function addOauth($_mem_id, $type, $oauth_data, $app_id = 0, $conf = []) {
  335. $_data['openid'] = $oauth_data['openid'];
  336. $_data['from'] = $type;
  337. $_data['access_token'] = $oauth_data['token']['access_token'];
  338. $_data['unionid'] = $oauth_data['unionid'];
  339. $_data['nickname'] = $oauth_data['nickname'];
  340. $_data['country'] = !empty($oauth_data['country']) ? $oauth_data['country'] : '';
  341. $_data['province'] = !empty($oauth_data['province']) ? $oauth_data['province'] : '';
  342. $_data['city'] = !empty($oauth_data['city']) ? $oauth_data['city'] : '';
  343. $_data['gender'] = $oauth_data['gender'];
  344. $_data['avatar'] = $oauth_data['avatar'];
  345. $_data['mem_id'] = $_mem_id;
  346. $_data['status'] = empty($oauth_data['nickname']) ? MemConst::STATUS_TRY : MemConst::STATUS_NORMAL;
  347. $_data['expires_in'] = $oauth_data['token']['expires_in'];
  348. if (!empty($app_id)) {
  349. $_mp_id = (new GameMini())->getMiniIdByAppId($app_id);
  350. $_data['conf_id'] = (new MpConfModel())->getIdByMpId($_mp_id);
  351. }
  352. if (OauthConst::OAUTH_WEIXIN == $type) { //微信
  353. $_conf_id = get_val($conf, 'APP_KEY', '');
  354. if (empty($_conf_id)) {
  355. $_conf_id = get_val($oauth_data, 'app_key', '');
  356. }
  357. $_data['conf_id'] = (new MpConfModel())->getIdByMpId($_conf_id);
  358. }
  359. //扩展信息
  360. $_more = [
  361. 'refresh_token' => !empty($oauth_data['token']['refresh_token']) ? $oauth_data['token']['refresh_token']
  362. : '',
  363. ];
  364. $_data['more'] = json_encode($_more);
  365. $_rs = (new MemoauthModel())->addOauth($_data);
  366. if (false === $_rs) {
  367. return false;
  368. }
  369. return MemCache::ins()->saveOpenIdCache($type, $_data['openid'], $_mem_id);
  370. }
  371. /**
  372. * 通过OpenId 获取玩家ID
  373. *
  374. *
  375. * @param string $type
  376. * @param string $union_id
  377. *
  378. * @return string
  379. */
  380. public function getMemIdByUnionId($type, $union_id) {
  381. $_mem_id = (new MemoauthModel())->getMemIdByUnionId($type, $union_id);
  382. return $_mem_id;
  383. }
  384. /**
  385. * 通过Token获取第三方信息
  386. *
  387. * @param $type
  388. * @param $openid
  389. * @param $access_token
  390. *
  391. * @return bool
  392. */
  393. public function getOauthDataByToken($type, $openid, $access_token) {
  394. try {
  395. $token['openid'] = $openid;
  396. $token['access_token'] = $access_token;
  397. $_oauth_class = OAuthLib::ins($type, [], $token);
  398. return $_oauth_class->getUserInfo();
  399. } catch (Exception $_e) {
  400. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&Exception'
  401. .$_e->getMessage().'&code'.$_e->getCode();
  402. Log::write($_err_msg, Log::ERROR);
  403. return false;
  404. }
  405. }
  406. /**
  407. * @param string $domain
  408. */
  409. public function setDomain($domain) {
  410. $this->domain = $domain;
  411. }
  412. /**
  413. * 通过code获取登陆信息
  414. *
  415. * @param $type
  416. * @param $access_token
  417. * @param $openid
  418. * @param $from
  419. * @param Game $game_rq
  420. * @param Channel $channel
  421. * @param Device $device
  422. * @param Mem $member
  423. *
  424. * @param array $_conf
  425. * @param $oauth_app_key
  426. *
  427. * @return array|mixed
  428. */
  429. public function oauthLoginByAccessToken(
  430. $type, $access_token, $openid, $from,
  431. Game $game_rq, Channel $channel, Device $device, Mem $member, $_conf = [], $oauth_app_key = ''
  432. ) {
  433. $_oauth_data = $this->getOauthDataByAccessToken(
  434. $type, $access_token, $openid, $from, $game_rq->getHAppId(), $_conf, $oauth_app_key
  435. );
  436. if (is_numeric($_oauth_data)) {
  437. $_mem_id = $_oauth_data;
  438. /* 登陆成功 */
  439. $_mem_data = MemCache::ins()->getInfoById($_mem_id);
  440. $_rdata = $this->getReturnData($_mem_data, $game_rq->getHAppId(), $device->getDeviceType());
  441. $this->insertLoginLog($game_rq, $channel, $device, $member, $_rdata);
  442. return $this->retSucMsg(MemberStatus::NO_ERROR, $_rdata);
  443. } elseif (is_array($_oauth_data)) {
  444. /* 用户信息 查询openid是否对应玩家ID */
  445. $_mo_model = new MemoauthModel();
  446. $_mo_data = $_mo_model->getInfoByOpenId($type, $_oauth_data['openid']);
  447. $_mem_id = isset($_mo_data['mem_id']) ? $_mo_data['mem_id'] : 0;
  448. if (empty($_mo_data['id']) && empty($_mem_id)) {
  449. /* 玩家注册 */
  450. $_mem_data = $this->oAuthReg($type, $_oauth_data, $game_rq, $channel, $device, $member, $_conf);
  451. $member->setIsReg(MemConst::MEM_IS_REG);
  452. $_mem_data['is_reg'] = MemConst::MEM_IS_REG;
  453. /* 生成支付小程序码 */
  454. } elseif (empty($_mo_data['id']) && !empty($_mem_id)) {
  455. /* 登陆成功 */
  456. $this->addOauth($_mem_id, $type, $_oauth_data, $game_rq->getHAppId(), $_conf);
  457. $_mem_data = MemCache::ins()->getInfoById($_mem_id);
  458. } else {
  459. /* 有第三方信息 未绑定玩家 */
  460. $_mem_id = $this->memReg($type, $_oauth_data, $game_rq, $channel, $device, $member);
  461. $_mo_data['mem_id'] = $_mem_id;
  462. $_mo_model->updateOauth($type, $_oauth_data['openid'], $_mo_data);
  463. $_mem_data = MemCache::ins()->getInfoById($_mem_id);
  464. }
  465. $_rdata = $this->getReturnData($_mem_data, $game_rq->getHAppId(), $device->getDeviceType());
  466. $this->insertLoginLog($game_rq, $channel, $device, $member, $_rdata);
  467. return $this->retSucMsg(MemberStatus::NO_ERROR, $_rdata);
  468. }
  469. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&oauth_data='
  470. .json_encode($_oauth_data).'&data='.json_encode(
  471. array($type, $access_token, $openid, $from, $game_rq->getHAppId(), $_conf)
  472. );
  473. Log::write($_err_msg, Log::ERROR);
  474. return $this->retErrMsg(MemberStatus::UNKNOWN_ERROR);
  475. }
  476. /**通过accesstoken获取信息
  477. *
  478. * @param $type
  479. * @param $access_token
  480. * @param $openid
  481. * @param $from
  482. * @param int $app_id
  483. * @param array $_conf
  484. * @param $oauth_app_key
  485. *
  486. * @return bool|string
  487. */
  488. public function getOauthDataByAccessToken($type, $access_token, $openid, $from, $app_id = 0, $_conf = [], $oauth_app_key = '') {
  489. try {
  490. if (OauthConst::OAUTH_MP == $type) {
  491. $_conf = (new GameMini())->getLoginConf($app_id);
  492. if (empty($_conf)) {
  493. return false;
  494. }
  495. }
  496. $_token = [
  497. 'access_token' => $access_token,
  498. 'openid' => $openid,
  499. 'oauth_app_key'=> $oauth_app_key,
  500. 'expires_in' => '7200'
  501. ];
  502. $_oauth_class = OAuthLib::ins($type, $_conf, $_token);
  503. $_oauth_class->setCallback($this->getCallbackUrl($type));
  504. /* Session 设置 设置第三方登陆Session */
  505. $_open_id = $openid;
  506. $_oauth_data = $_oauth_class->getUserInfo();
  507. $_mo_model = new MemoauthModel();
  508. $_mo_data = $_mo_model->getInfoByOpenId($type, $_open_id);
  509. if (!empty($_mo_data['id'])) {
  510. $_mem_id = $_mo_data['mem_id'];
  511. /* 更新信息 */
  512. if (($_mo_data['unionid'] == $_mo_data['openid'] && $_oauth_data['unionid'] != $_oauth_data['openid'])
  513. || 0 == $_mem_id) {
  514. $_mem_id = $this->getMemIdByUnionId($type, $_oauth_data['unionid']);
  515. /* 插入更多信息 */
  516. $_mo_data['unionid'] = $_oauth_data['unionid'];
  517. $_mo_data['nickname'] = $_oauth_data['nickname'];
  518. $_mo_data['gender'] = $_oauth_data['gender'];
  519. $_mo_data['avatar'] = $_oauth_data['avatar'];
  520. $_mo_data['status'] = MemConst::STATUS_NORMAL;
  521. $_mo_data['expires_in'] = $_token['expires_in'];
  522. $_mo_data['mem_id'] = $_mem_id;
  523. $_mo_data['last_login_ip'] = get_client_ip(0, true);
  524. $_mem_data['avatar'] = $_mo_data['avatar'];
  525. $_mem_data['nickname'] = $_mo_data['nickname'];
  526. $_mem_data['status'] = MemConst::STATUS_NORMAL;
  527. MemCache::ins()->updateMem($_mem_id, $_mem_data);
  528. }
  529. $_mo_data['access_token'] = $_token['access_token'];
  530. $_mo_data['expires_in'] = $_token['expires_in'];
  531. $_mo_data['last_login_ip'] = get_client_ip(0, true);
  532. $_mo_data['last_login_time'] = time();
  533. $_mo_model->updateOauth($type, $_open_id, $_mo_data);
  534. if (empty($_mem_id)) {
  535. return $_oauth_data;
  536. }
  537. return $_mem_id;
  538. }
  539. return $_oauth_data;
  540. } catch (Exception $_e) {
  541. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&Exception'
  542. .$_e->getMessage().'&code'.$_e->getCode();
  543. Log::write($_err_msg, Log::ERROR);
  544. return false;
  545. }
  546. }
  547. }