mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
[更新]微信消息模式测试
This commit is contained in:
parent
3ad452957e
commit
3d7adf6c7f
@ -54,7 +54,15 @@ class ReceiveHandler
|
|||||||
if (isset($data['EventKey']) && is_object($data['EventKey'])) $data['EventKey'] = (array)$data['EventKey'];
|
if (isset($data['EventKey']) && is_object($data['EventKey'])) $data['EventKey'] = (array)$data['EventKey'];
|
||||||
$input = ['openid' => $openid, 'appid' => $appid, 'receive' => serialize($data), 'encrypt' => intval($service->isEncrypt())];
|
$input = ['openid' => $openid, 'appid' => $appid, 'receive' => serialize($data), 'encrypt' => intval($service->isEncrypt())];
|
||||||
if (is_string($result = http_post($config['appuri'], $input, ['timeout' => 30]))) {
|
if (is_string($result = http_post($config['appuri'], $input, ['timeout' => 30]))) {
|
||||||
return $result;
|
p('---- Service 收到 Client 的http返回 ---- ');
|
||||||
|
p($result);
|
||||||
|
if (is_array($json = json_decode($result, true))) {
|
||||||
|
p('===== 已返回xml加密内容 =====');
|
||||||
|
return $service->reply($json, true, true);
|
||||||
|
} else {
|
||||||
|
p('===== 直接回复返回的内容 =====');
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
\think\facade\Log::error("微信{$appid}接口调用异常,{$e->getMessage()}");
|
\think\facade\Log::error("微信{$appid}接口调用异常,{$e->getMessage()}");
|
||||||
|
@ -72,6 +72,12 @@ class Push extends Controller
|
|||||||
*/
|
*/
|
||||||
protected $forceCustom = false;
|
protected $forceCustom = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 强制返回JSON回复
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
|
protected $forceJson = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取网络出口IP
|
* 获取网络出口IP
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@ -90,7 +96,8 @@ class Push extends Controller
|
|||||||
try {
|
try {
|
||||||
$this->wechat = WechatService::WeChatReceive();
|
$this->wechat = WechatService::WeChatReceive();
|
||||||
if ($this->request->has('receive', 'post') && WechatService::getType() === 'thr') {
|
if ($this->request->has('receive', 'post') && WechatService::getType() === 'thr') {
|
||||||
$this->forceCustom = true;
|
$this->forceJson = true; // 强制返回JSON到服务端再转发
|
||||||
|
$this->forceCustom = false; // 暂时使用客户消息模式
|
||||||
$this->appid = $this->request->post('appid', '', null);
|
$this->appid = $this->request->post('appid', '', null);
|
||||||
$this->openid = $this->request->post('openid', '', null);
|
$this->openid = $this->request->post('openid', '', null);
|
||||||
$this->encrypt = boolval($this->request->post('encrypt', 0));
|
$this->encrypt = boolval($this->request->post('encrypt', 0));
|
||||||
@ -99,7 +106,8 @@ class Push extends Controller
|
|||||||
throw new \think\Exception('微信API实例缺失必要参数[appid,openid,receive]');
|
throw new \think\Exception('微信API实例缺失必要参数[appid,openid,receive]');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->forceCustom = false;
|
$this->forceJson = false; // 强制返回JSON仅对第三方有效
|
||||||
|
$this->forceCustom = false; // 暂时使用客户消息模式
|
||||||
$this->appid = WechatService::getAppid();
|
$this->appid = WechatService::getAppid();
|
||||||
$this->openid = $this->wechat->getOpenid();
|
$this->openid = $this->wechat->getOpenid();
|
||||||
$this->encrypt = $this->wechat->isEncrypt();
|
$this->encrypt = $this->wechat->isEncrypt();
|
||||||
@ -266,7 +274,8 @@ class Push extends Controller
|
|||||||
WechatService::WeChatCustom()->send(['touser' => $this->openid, 'msgtype' => $type, "{$type}" => $data]);
|
WechatService::WeChatCustom()->send(['touser' => $this->openid, 'msgtype' => $type, "{$type}" => $data]);
|
||||||
} else switch (strtolower($type)) {
|
} else switch (strtolower($type)) {
|
||||||
case 'text': // 发送文本消息
|
case 'text': // 发送文本消息
|
||||||
return $this->wechat->reply(['CreateTime' => time(), 'MsgType' => 'text', 'ToUserName' => $this->openid, 'FromUserName' => $this->fromOpenid, 'Content' => $data['content']], true, $this->encrypt);
|
$reply = ['CreateTime' => time(), 'MsgType' => 'text', 'ToUserName' => $this->openid, 'FromUserName' => $this->fromOpenid, 'Content' => $data['content']];
|
||||||
|
return $this->forceJson ? json_encode($reply, JSON_UNESCAPED_UNICODE) : $this->wechat->reply($reply, true, $this->encrypt);
|
||||||
case 'image': // 发送图片消息
|
case 'image': // 发送图片消息
|
||||||
return $this->buildMessage($type, ['MediaId' => $data['media_id']]);
|
return $this->buildMessage($type, ['MediaId' => $data['media_id']]);
|
||||||
case 'voice': // 发送语言消息
|
case 'voice': // 发送语言消息
|
||||||
@ -281,7 +290,8 @@ class Push extends Controller
|
|||||||
case 'news': // 发送图文消息
|
case 'news': // 发送图文消息
|
||||||
$articles = [];
|
$articles = [];
|
||||||
foreach ($data['articles'] as $article) array_push($articles, ['PicUrl' => $article['picurl'], 'Title' => $article['title'], 'Description' => $article['description'], 'Url' => $article['url']]);
|
foreach ($data['articles'] as $article) array_push($articles, ['PicUrl' => $article['picurl'], 'Title' => $article['title'], 'Description' => $article['description'], 'Url' => $article['url']]);
|
||||||
return $this->wechat->reply(['CreateTime' => time(), 'MsgType' => 'news', 'ToUserName' => $this->openid, 'FromUserName' => $this->fromOpenid, 'Articles' => $articles, 'ArticleCount' => count($articles)], true, $this->encrypt);
|
$reply = ['CreateTime' => time(), 'MsgType' => 'news', 'ToUserName' => $this->openid, 'FromUserName' => $this->fromOpenid, 'Articles' => $articles, 'ArticleCount' => count($articles)];
|
||||||
|
return $this->forceJson ? json_encode($reply, JSON_UNESCAPED_UNICODE) : $this->wechat->reply($reply, true, $this->encrypt);
|
||||||
default:
|
default:
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
@ -289,8 +299,8 @@ class Push extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息数据生成
|
* 消息数据生成
|
||||||
* @param string $type
|
* @param string $type 消息类型
|
||||||
* @param string|array $data
|
* @param string|array $data 消息数据
|
||||||
* @return string
|
* @return string
|
||||||
* @throws \WeChat\Exceptions\InvalidDecryptException
|
* @throws \WeChat\Exceptions\InvalidDecryptException
|
||||||
*/
|
*/
|
||||||
@ -298,7 +308,7 @@ class Push extends Controller
|
|||||||
{
|
{
|
||||||
$reply = ['CreateTime' => time(), 'MsgType' => strtolower($type), 'ToUserName' => $this->openid, 'FromUserName' => $this->fromOpenid];
|
$reply = ['CreateTime' => time(), 'MsgType' => strtolower($type), 'ToUserName' => $this->openid, 'FromUserName' => $this->fromOpenid];
|
||||||
if (!empty($data)) $reply[ucfirst(strtolower($type))] = $data;
|
if (!empty($data)) $reply[ucfirst(strtolower($type))] = $data;
|
||||||
return $this->wechat->reply($reply, true, $this->encrypt);
|
return $this->forceJson ? json_encode($reply, JSON_UNESCAPED_UNICODE) : $this->wechat->reply($reply, true, $this->encrypt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user