123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\service\handler;
- use app\service\service\WechatService as WechatLogic;
- use think\Db;
- class WechatHandler
- {
-
- protected $appid;
-
- protected $config;
-
- protected $message;
-
- public function __construct($config = [])
- {
- $this->config = $config;
- $this->appid = isset($config['authorizer_appid']) ? $config['authorizer_appid'] : '';
- }
-
- private function checkInit()
- {
- if (!empty($this->config)) return true;
- throw new \think\Exception('Wechat Please bind Wechat first');
- }
-
- public function getConfig()
- {
- $this->checkInit();
- $info = Db::name('WechatServiceConfig')->where(['authorizer_appid' => $this->appid])->find();
- if (empty($info)) return false;
- if (isset($info['id'])) unset($info['id']);
- return $info;
- }
-
- public function setApiNotifyUri($notifyUri)
- {
- $this->checkInit();
- if (empty($notifyUri)) throw new \think\Exception('请传入微信通知URL');
- list($where, $data) = [['authorizer_appid' => $this->appid], ['appuri' => $notifyUri]];
- return Db::name('WechatServiceConfig')->where($where)->update($data) !== false;
- }
-
- public function updateApiAppkey()
- {
- $this->checkInit();
- $data = ['appkey' => md5(uniqid())];
- Db::name('WechatServiceConfig')->where(['authorizer_appid' => $this->appid])->update($data);
- return $data['appkey'];
- }
-
- public function config($name = null)
- {
- $this->checkInit();
- return WechatLogic::WeChatScript($this->appid)->config->get($name);
- }
-
- public function oauth($sessid, $selfUrl, $fullMode = 0)
- {
- $this->checkInit();
- $fans = cache("{$this->appid}_{$sessid}_fans");
- $openid = cache("{$this->appid}_{$sessid}_openid");
- if (!empty($openid) && (empty($fullMode) || !empty($fans))) {
- return ['openid' => $openid, 'fans' => $fans, 'url' => ''];
- }
- $service = WechatLogic::service();
- $mode = empty($fullMode) ? 'snsapi_base' : 'snsapi_userinfo';
- $url = url('@service/api.push/oauth', '', true, true);
- $params = ['mode' => $fullMode, 'sessid' => $sessid, 'enurl' => encode($selfUrl)];
- $authurl = $service->getOauthRedirect($this->appid, $url . '?' . http_build_query($params), $mode);
- return ['openid' => $openid, 'fans' => $fans, 'url' => $authurl];
- }
-
- public function jsSign($url)
- {
- $this->checkInit();
- return WechatLogic::WeChatScript($this->appid)->getJsSign($url);
- }
- }
|