WechatHandler.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\service\handler;
  14. use app\service\service\WechatService as WechatLogic;
  15. use think\Db;
  16. /**
  17. * 微信网页授权接口
  18. * Class WechatHandler
  19. * @package app\wechat\handler
  20. * @author Anyon <zoujingli@qq.com>
  21. */
  22. class WechatHandler
  23. {
  24. /**
  25. * 当前微信APPID
  26. * @var string
  27. */
  28. protected $appid;
  29. /**
  30. * 当前微信配置
  31. * @var array
  32. */
  33. protected $config;
  34. /**
  35. * 错误消息
  36. * @var string
  37. */
  38. protected $message;
  39. /**
  40. * Wechat constructor.
  41. * @param array $config
  42. */
  43. public function __construct($config = [])
  44. {
  45. $this->config = $config;
  46. $this->appid = isset($config['authorizer_appid']) ? $config['authorizer_appid'] : '';
  47. }
  48. /**
  49. * 检查微信配置服务初始化状态
  50. * @return boolean
  51. * @throws \think\Exception
  52. */
  53. private function checkInit()
  54. {
  55. if (!empty($this->config)) return true;
  56. throw new \think\Exception('Wechat Please bind Wechat first');
  57. }
  58. /**
  59. * 获取当前公众号配置
  60. * @return array|boolean
  61. * @throws \think\Exception
  62. */
  63. public function getConfig()
  64. {
  65. $this->checkInit();
  66. $info = Db::name('WechatServiceConfig')->where(['authorizer_appid' => $this->appid])->find();
  67. if (empty($info)) return false;
  68. if (isset($info['id'])) unset($info['id']);
  69. return $info;
  70. }
  71. /**
  72. * 设置微信接口通知URL地址
  73. * @param string $notifyUri 接口通知URL地址
  74. * @return boolean
  75. * @throws \think\Exception
  76. * @throws \think\exception\PDOException
  77. */
  78. public function setApiNotifyUri($notifyUri)
  79. {
  80. $this->checkInit();
  81. if (empty($notifyUri)) throw new \think\Exception('请传入微信通知URL');
  82. list($where, $data) = [['authorizer_appid' => $this->appid], ['appuri' => $notifyUri]];
  83. return Db::name('WechatServiceConfig')->where($where)->update($data) !== false;
  84. }
  85. /**
  86. * 更新接口Appkey(成功返回新的Appkey)
  87. * @return bool|string
  88. * @throws \think\Exception
  89. * @throws \think\exception\PDOException
  90. */
  91. public function updateApiAppkey()
  92. {
  93. $this->checkInit();
  94. $data = ['appkey' => md5(uniqid())];
  95. Db::name('WechatServiceConfig')->where(['authorizer_appid' => $this->appid])->update($data);
  96. return $data['appkey'];
  97. }
  98. /**
  99. * 获取公众号的配置参数
  100. * @param string $name 参数名称
  101. * @return array|string
  102. * @throws \think\Exception
  103. */
  104. public function config($name = null)
  105. {
  106. $this->checkInit();
  107. return WechatLogic::WeChatScript($this->appid)->config->get($name);
  108. }
  109. /**
  110. * 微信网页授权
  111. * @param string $sessid 当前会话id(可用session_id()获取)
  112. * @param string $selfUrl 当前会话URL地址(需包含域名的完整URL地址)
  113. * @param int $fullMode 网页授权模式(0静默模式,1高级授权)
  114. * @return array|bool
  115. * @throws \think\Exception
  116. */
  117. public function oauth($sessid, $selfUrl, $fullMode = 0)
  118. {
  119. $this->checkInit();
  120. $fans = cache("{$this->appid}_{$sessid}_fans");
  121. $openid = cache("{$this->appid}_{$sessid}_openid");
  122. if (!empty($openid) && (empty($fullMode) || !empty($fans))) {
  123. return ['openid' => $openid, 'fans' => $fans, 'url' => ''];
  124. }
  125. $service = WechatLogic::service();
  126. $mode = empty($fullMode) ? 'snsapi_base' : 'snsapi_userinfo';
  127. $url = url('@service/api.push/oauth', '', true, true);
  128. $params = ['mode' => $fullMode, 'sessid' => $sessid, 'enurl' => encode($selfUrl)];
  129. $authurl = $service->getOauthRedirect($this->appid, $url . '?' . http_build_query($params), $mode);
  130. return ['openid' => $openid, 'fans' => $fans, 'url' => $authurl];
  131. }
  132. /**
  133. * 微信网页JS签名
  134. * @param string $url 当前会话URL地址(需包含域名的完整URL地址)
  135. * @return array|boolean
  136. * @throws \WeChat\Exceptions\InvalidResponseException
  137. * @throws \WeChat\Exceptions\LocalCacheException
  138. * @throws \think\Exception
  139. */
  140. public function jsSign($url)
  141. {
  142. $this->checkInit();
  143. return WechatLogic::WeChatScript($this->appid)->getJsSign($url);
  144. }
  145. }