From a10a47a817e449a73b5e5fcd28678bf220eda83a Mon Sep 17 00:00:00 2001 From: Anyon Date: Tue, 15 May 2018 11:54:38 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E8=AE=A9=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/service/WechatService.php | 34 ++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/extend/service/WechatService.php b/extend/service/WechatService.php index 357ce0e26..1a1130a74 100644 --- a/extend/service/WechatService.php +++ b/extend/service/WechatService.php @@ -47,15 +47,41 @@ use think\Exception; class WechatService { + /** + * 接口类型模式 + * @var string + */ + public static $type = 'WeChat'; + + /** + * 切换接口为服务号模式 + */ + public static function setWeChatState() + { + self::$type = 'WeChat'; + } + + /** + * 切换接口为小程序模式 + */ + public function setWeMiniState() + { + self::$type = 'WeMini'; + } + /** * 获取微信实例ID * @param string $name 实例对象名称 + * @param string $type 接口实例类型 * @return SoapService|string - * @throws \think\Exception + * @throws Exception * @throws \think\exception\PDOException */ - public static function instance($name) + public static function instance($name, $type = null) { + if (!in_array($type, ['WeChat', 'WeMini'])) { + $type = self::$type; + } switch (strtolower(sysconf('wechat_type'))) { case 'api': $config = [ @@ -69,14 +95,14 @@ class WechatService 'ssl_key' => sysconf('wechat_cert_key'), 'cachepath' => env('cache_path') . 'wechat' . DIRECTORY_SEPARATOR, ]; - $class = '\\WeChat\\' . ucfirst(strtolower($name)); + $class = "\\{$type}\\" . ucfirst(strtolower($name)); if (class_exists($class)) { return new $class($config); } throw new Exception("Class '{$class}' not found"); case 'thr': list($appid, $appkey) = [sysconf('wechat_thr_appid'), sysconf('wechat_thr_appkey')]; - $token = strtolower("{$name}-{$appid}-{$appkey}"); + $token = strtolower("{$name}-{$appid}-{$appkey}-{$type}"); $location = config('wechat.service_url') . "/wechat/api.client/soap/{$token}.html"; $params = ['uri' => strtolower($name), 'location' => $location, 'trace' => true]; return new SoapService(null, $params);