BasicWechat.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 think\Controller;
  15. /**
  16. * 微信基础控制器
  17. * Class BasicWechat
  18. * @package controller
  19. */
  20. class BasicWechat extends Controller
  21. {
  22. /**
  23. * 当前粉丝用户OPENID
  24. * @var string
  25. */
  26. protected $openid;
  27. /**
  28. * 当前粉丝信息
  29. * @var array
  30. */
  31. protected $fansinfo;
  32. /**
  33. * 微信网页授权
  34. * @param bool $mode 获取完整
  35. * @return string
  36. */
  37. protected function oauth($mode = true)
  38. {
  39. $this->openid = session('openid');
  40. if (!empty($this->openid) && empty($mode)) {
  41. return $this->openid;
  42. } elseif (!empty($this->openid) && session('fansinfo')) {
  43. $this->fansinfo = session('fansinfo');
  44. return $this->openid;
  45. }
  46. list($sessionid, $location) = [session_id(), $this->request->url(true)];
  47. $result = load_wechat('wechat')->oauth($sessionid, $location, intval($mode));
  48. !empty($result['url']) && $this->redirect($result['url']);
  49. if (!empty($result['openid'])) {
  50. list($this->openid, $this->fansinfo) = [$result['openid'], $result['fans']];
  51. session('openid', $this->openid);
  52. session('fansinfo', $this->fansinfo);
  53. return $this->openid;
  54. }
  55. $this->error('网页授权失败,请稍候再试!');
  56. }
  57. }