mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
[更新]让接口兼容小程序模式
This commit is contained in:
parent
99b3b8ae80
commit
a10a47a817
@ -47,15 +47,41 @@ use think\Exception;
|
|||||||
class WechatService
|
class WechatService
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接口类型模式
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public static $type = 'WeChat';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 切换接口为服务号模式
|
||||||
|
*/
|
||||||
|
public static function setWeChatState()
|
||||||
|
{
|
||||||
|
self::$type = 'WeChat';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 切换接口为小程序模式
|
||||||
|
*/
|
||||||
|
public function setWeMiniState()
|
||||||
|
{
|
||||||
|
self::$type = 'WeMini';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取微信实例ID
|
* 获取微信实例ID
|
||||||
* @param string $name 实例对象名称
|
* @param string $name 实例对象名称
|
||||||
|
* @param string $type 接口实例类型
|
||||||
* @return SoapService|string
|
* @return SoapService|string
|
||||||
* @throws \think\Exception
|
* @throws Exception
|
||||||
* @throws \think\exception\PDOException
|
* @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'))) {
|
switch (strtolower(sysconf('wechat_type'))) {
|
||||||
case 'api':
|
case 'api':
|
||||||
$config = [
|
$config = [
|
||||||
@ -69,14 +95,14 @@ class WechatService
|
|||||||
'ssl_key' => sysconf('wechat_cert_key'),
|
'ssl_key' => sysconf('wechat_cert_key'),
|
||||||
'cachepath' => env('cache_path') . 'wechat' . DIRECTORY_SEPARATOR,
|
'cachepath' => env('cache_path') . 'wechat' . DIRECTORY_SEPARATOR,
|
||||||
];
|
];
|
||||||
$class = '\\WeChat\\' . ucfirst(strtolower($name));
|
$class = "\\{$type}\\" . ucfirst(strtolower($name));
|
||||||
if (class_exists($class)) {
|
if (class_exists($class)) {
|
||||||
return new $class($config);
|
return new $class($config);
|
||||||
}
|
}
|
||||||
throw new Exception("Class '{$class}' not found");
|
throw new Exception("Class '{$class}' not found");
|
||||||
case 'thr':
|
case 'thr':
|
||||||
list($appid, $appkey) = [sysconf('wechat_thr_appid'), sysconf('wechat_thr_appkey')];
|
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";
|
$location = config('wechat.service_url') . "/wechat/api.client/soap/{$token}.html";
|
||||||
$params = ['uri' => strtolower($name), 'location' => $location, 'trace' => true];
|
$params = ['uri' => strtolower($name), 'location' => $location, 'trace' => true];
|
||||||
return new SoapService(null, $params);
|
return new SoapService(null, $params);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user