| xqd
@@ -577,24 +577,28 @@ if (!function_exists('send_wechat_message')) {
|
|
|
}
|
|
|
$json = vsprintf($json, $official_arr);
|
|
|
$data = json_decode($json, true);
|
|
|
- $ret = $app->uniform_message->send($data);
|
|
|
- if (empty($subscribe_arr) && (!isset($ret['errcode']) || $ret['errcode'] != 0)) {
|
|
|
- trace(['发送微信模板消息失败,请求参数' => $data, '返回数据' => $ret], 'error');
|
|
|
- }
|
|
|
- //如果模板消息发送失败(可能因用户没有关注公众号而失败)再发送订阅消息
|
|
|
- if (!empty($subscribe_arr) && isset($ret['errcode']) && $ret['errcode'] != 0) {
|
|
|
- $data = config('config.wechat_subscribe_message_template')[$type];
|
|
|
- $json = json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
|
- foreach ($subscribe_arr as $k => &$v) {
|
|
|
- $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
|
|
|
- $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t","\\f", "\\b");
|
|
|
- $v = str_replace($escapers, $replacements, $v);
|
|
|
+ if (empty($data) || !is_array($data)) {
|
|
|
+ $ret = $app->uniform_message->send($data);
|
|
|
+ if (empty($subscribe_arr) && (!isset($ret['errcode']) || $ret['errcode'] != 0)) {
|
|
|
+ trace(['发送微信模板消息失败,请求参数' => $data, '返回数据' => $ret], 'error');
|
|
|
}
|
|
|
- $json = vsprintf($json, $subscribe_arr);
|
|
|
- $data = json_decode($json, true);
|
|
|
- $ret = $app->subscribe_message->send($data);
|
|
|
- if (isset($ret['errcode']) && $ret['errcode'] != 0) {
|
|
|
- trace(['发送微信订阅消息失败,请求参数' => $data ?? '', '返回数据' => $ret ?? ''], 'error');
|
|
|
+ //如果模板消息发送失败(可能因用户没有关注公众号而失败)再发送订阅消息
|
|
|
+ if (!empty($subscribe_arr) && isset($ret['errcode']) && $ret['errcode'] != 0) {
|
|
|
+ $data = config('config.wechat_subscribe_message_template')[$type];
|
|
|
+ $json = json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
|
+ foreach ($subscribe_arr as $k => &$v) {
|
|
|
+ $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
|
|
|
+ $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t","\\f", "\\b");
|
|
|
+ $v = str_replace($escapers, $replacements, $v);
|
|
|
+ }
|
|
|
+ $json = vsprintf($json, $subscribe_arr);
|
|
|
+ $data = json_decode($json, true);
|
|
|
+ if (!empty($data) && is_array($data)) {
|
|
|
+ $ret = $app->subscribe_message->send($data);
|
|
|
+ if (isset($ret['errcode']) && $ret['errcode'] != 0) {
|
|
|
+ trace(['发送微信订阅消息失败,请求参数' => $data ?? '', '返回数据' => $ret ?? ''], 'error');
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception $e) {
|