BasicWechat.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~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\FansService;
  15. use think\Controller;
  16. class BasicWechat extends Controller {
  17. /**
  18. * 当前粉丝用户OPENID
  19. * @var string
  20. */
  21. protected $openid;
  22. /**
  23. * 当前粉丝信息
  24. * @var array
  25. */
  26. protected $fansinfo;
  27. /**
  28. * 当前访问网址
  29. * @var string
  30. */
  31. protected $current;
  32. /**
  33. * 是否默认开启网页授权
  34. * @var bool
  35. */
  36. protected $check_auth = true;
  37. /**
  38. * 初始化方法
  39. */
  40. public function _initialize() {
  41. parent::_initialize();
  42. $this->current = ($this->request->isSsl() ? 'https' : 'http') . '://' . $this->request->host() . $this->request->url();
  43. /* 网页授权,并获粉丝信息 */
  44. if ($this->check_auth && $this->oAuth()) {
  45. if ($this->request->isGet()) {
  46. $this->assign('js_sign', load_wechat('script')->getJsSign($this->current));
  47. $this->assign('fansinfo', $this->fansinfo);
  48. }
  49. }
  50. }
  51. /**
  52. * 微信网页授权函数
  53. * @param bool $isfull
  54. * @return string
  55. */
  56. protected function oAuth($isfull = true) {
  57. $host = $this->request->host();
  58. # 本地开发调试用户OPENID
  59. if (in_array($host, ['127.0.0.1', 'localhost'])) {
  60. session('openid', 'o38gps1Unf64JOTdxNdd424lsEmM');
  61. }
  62. # 检查缓存中openid信息是否完整
  63. if (!!($this->openid = session('openid'))) {
  64. if (!!($this->fansinfo = FansService::get($this->openid)) || !$isfull) {
  65. return $this->openid;
  66. }
  67. }
  68. # 发起微信网页授权
  69. $wxoauth_url = $this->current;
  70. if (!($redirect_url = $this->request->get('redirecturl', false, 'decode'))) {
  71. $params = $this->request->param();
  72. $params['redirecturl'] = encode($wxoauth_url);
  73. $wxoauth_url = url($this->request->baseUrl(), '', false, true) . '?' . http_build_query($params);
  74. }
  75. $wechat = &load_wechat('Oauth');
  76. # 微信网页授权处理
  77. if (!$this->request->get('code', false)) {
  78. exit(redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_base'))->send());
  79. }
  80. if (FALSE === ($result = $wechat->getOauthAccessToken()) || empty($result['openid'])) {
  81. Log::error("微信授权失败 [ {$wechat->errMsg} ]");
  82. exit('网页授权失败,请稍候再试!');
  83. }
  84. session('openid', $this->openid = $result['openid']);
  85. $this->fansinfo = FansService::get($this->openid);
  86. # 微信粉丝信息处理
  87. if (empty($this->fansinfo['expires_in']) || $this->fansinfo['expires_in'] < time()) {
  88. switch ($result['scope']) {
  89. case 'snsapi_base': /* 普通授权,获取用户资料;未关注时重新使用高级授权 */
  90. $user = load_wechat('User')->getUserInfo($this->openid);
  91. if ($isfull && empty($user['subscribe'])) {
  92. exit(redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_userinfo'))->send());
  93. }
  94. break;
  95. case 'snsapi_userinfo': /* 高级授权,获取用户资料 */
  96. $user = $wechat->getOauthUserinfo($result['access_token'], $this->openid);
  97. break;
  98. }
  99. if ($isfull && (empty($user) || !array_key_exists('nickname', $user))) {
  100. exit("微信授权失败 [{$wechat->errMsg}]!");
  101. }
  102. /* 更新粉丝信息 */
  103. $user['expires_in'] = $result['expires_in'] + time() - 100;
  104. $user['refresh_token'] = $result['refresh_token'];
  105. $user['access_token'] = $result['access_token'];
  106. !FansService::set($user) && exit('微信授权失败 [ save userinfo faild ]');
  107. $this->fansinfo = FansService::get($this->openid);
  108. }
  109. empty($this->fansinfo) && exit('获取微信用户信息失败!');
  110. !!$redirect_url && exit(redirect($redirect_url)->send());
  111. return $this->openid;
  112. }
  113. }