BasicWechat.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace controller;
  14. use service\WechatService;
  15. use think\Controller;
  16. use think\Log;
  17. /**
  18. * 微信基础控制器
  19. * Class BasicWechat
  20. * @package controller
  21. */
  22. class BasicWechat extends Controller {
  23. /**
  24. * 当前粉丝用户OPENID
  25. * @var string
  26. */
  27. protected $openid;
  28. /**
  29. * 当前粉丝信息
  30. * @var array
  31. */
  32. protected $fansinfo;
  33. /**
  34. * 当前访问网址
  35. * @var string
  36. */
  37. protected $url;
  38. /**
  39. * 是否默认开启网页授权
  40. * @var bool
  41. */
  42. protected $checkAuth = true;
  43. /**
  44. * 初始化方法
  45. */
  46. public function _initialize() {
  47. // 当前完整URL地址
  48. $this->url = $this->request->url(true);
  49. // 网页授权,并获粉丝信息
  50. $this->assign('jsSign', load_wechat('script')->getJsSign($this->url));
  51. // 检查启用网页授权
  52. $this->checkAuth && $this->oAuth();
  53. }
  54. /**
  55. * 微信网页授权
  56. * @param bool $fullMode 获取完整
  57. * @return string
  58. */
  59. protected function oAuth($fullMode = true) {
  60. // 本地开发调试用户 openid
  61. if (in_array($this->request->host(), ['127.0.0.1', 'localhost'])) {
  62. session('openid', 'oBWB3wWVNujb-PJlmPmxC5CBTNF0');
  63. }
  64. // 检查缓存中 openid 信息是否完整
  65. if (($this->openid = session('openid'))) {
  66. if (!$fullMode) {
  67. return $this->openid;
  68. }
  69. $this->fansinfo = WechatService::getFansInfo($this->openid);
  70. if (!empty($this->fansinfo) && $this->fansinfo['expires_in'] > time()) {
  71. $this->assign('fansinfo', $this->fansinfo);
  72. return $this->openid;
  73. }
  74. }
  75. // 发起微信网页授权
  76. $wxoauth_url = $this->url;
  77. if (!($redirect_url = $this->request->get('redirectcode', false, 'decode'))) {
  78. $split = stripos($this->url, '?') === false ? '?' : '&';
  79. $wxoauth_url = "{$this->url}{$split}redirectcode=" . encode($this->url);
  80. }
  81. // 微信网页授权处理
  82. $wechat = &load_wechat('Oauth');
  83. if (!$this->request->get('code', false)) {
  84. $this->redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_base'));
  85. }
  86. if (FALSE === ($result = $wechat->getOauthAccessToken()) || empty($result['openid'])) {
  87. Log::error("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]");
  88. $this->error("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]");
  89. }
  90. session('openid', $this->openid = $result['openid']);
  91. empty($fullMode) && $this->redirect($redirect_url);
  92. // 微信粉丝信息处理
  93. $this->fansinfo = WechatService::getFansInfo($this->openid);
  94. if (empty($this->fansinfo['expires_in']) || intval($this->fansinfo['expires_in']) < time()) {
  95. /* 使用普通授权, 获取用户资料; 未关注时重新使用高级授权 */
  96. if ($result['scope'] === 'snsapi_base') :
  97. $user = load_wechat('User')->getUserInfo($this->openid);
  98. empty($user['subscribe']) && $this->redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_userinfo'));
  99. /* 使用高级授权, 获取完整用户资料 */
  100. elseif ($result['scope'] === 'snsapi_userinfo') :
  101. $user = $wechat->getOauthUserinfo($result['access_token'], $this->openid);
  102. endif;
  103. /* 授权结果处理, 更新粉丝信息 */
  104. if ((empty($user) || !array_key_exists('nickname', $user))) :
  105. Log::error("微信网页授权获取用户信息失败, {$wechat->errMsg}[{$wechat->errCode}]");
  106. $this->error("微信网页授权获取用户信息失败, {$wechat->errMsg}[{$wechat->errCode}]");
  107. endif;
  108. $user['expires_in'] = $result['expires_in'] + time() - 100;
  109. $user['refresh_token'] = $result['refresh_token'];
  110. $user['access_token'] = $result['access_token'];
  111. WechatService::setFansInfo($user, $wechat->appid) or $this->error('微信网页授权用户保存失败!');
  112. }
  113. $this->redirect($redirect_url);
  114. }
  115. }