CustomMessage.php
4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace domain\system\message;
use Yii;
use common\components\jqueue\base\Job;
use common\helpers\UserMPHelper;
use common\helpers\WxHelper;
use common\models\SysSetting as SysSettingModel;
use common\exts\wechat\PHPSDK as WxPHPSDK;
use common\services\queue\job\CustomMessageJob;
use function json_encode;
/**
* 客服消息业务接口
* Class CustomMessage
* @package domain\system\message
*/
class CustomMessage
{
const DEBUG = true; // 调试开关(开启日志)
/**
* 统一控制日志输出
* @param $level
* @param $msg
*/
private static function Log($msg) {
if (self::DEBUG == false || YII_ENV_PROD) { // 正式服屏蔽消息日志
return ;
}
Yii::$app->dlog->debug($msg);
}
/**
* 发送客服文本消息接口
*
* @param $touser 当前关注公众号的微信用户openid
*/
public static function sendTextMessage(WxPHPSDK $wechat, $touser, $content)
{
$data = [
'touser' => $touser,
'msgtype' => WxPHPSDK::MSGTYPE_TEXT,
'text' => [
'content' => $content
]
];
self::send($wechat, $data);
}
/**
* 发送客服图片消息接口
*
* @param $touser 当前关注公众号的微信用户openid
*/
public static function sendImageMessage(WxPHPSDK $wechat, $touser, $mediaId)
{
$data = [
'touser' => $touser,
'msgtype' => WxPHPSDK::MSGTYPE_IMAGE,
'image' => [
'media_id' => $mediaId
]
];
self::send($wechat, $data);
}
/**
* 发送客服图文消息接口
*
* @param $touser 当前关注公众号的微信用户openid
*/
public static function sendNewsMessage(WxPHPSDK $wechat, $touser, $message)
{
$data = array(
'touser' => $touser,
'msgtype' => WxPHPSDK::MSGTYPE_NEWS,
'news' => [
'articles' => $message
]
);
self::send($wechat, $data);
}
/**
* 发送客服消息接口
* @param WxPHPSDK $wechat
* @param array $data
*/
protected static function send(WxPHPSDK $wechat, array $data)
{
// 发送模板消息: 如果是异步模式,那么加入队列;非异步模式的话,直接发。
$messagePushMode = SysSettingModel::getMessagePushMode();
if (SysSettingModel::MESSAGE_PUSH_ASYNC == $messagePushMode) {
$data['appid'] = $wechat->getAppID();
CustomMessageJob::dispatch($data);
} else {
$wechat->sendCustomMessage($data);
}
}
/**
* 执行客服消息发送任务
* 说明: 消息队列任务类将回调该[任务处理]方法
* @param $job
* @param $payload
* @return bool
*/
public static function handle(Job $job, $payload)
{
$e = array();
$e['success'] = false;
if (empty($payload)) {
self::Log("[执行[客服通知]异步发送任务]: 无效payload数据");
$e['errcode'] = '-1';
$e['errmsg'] = "无效payload数据";
return $e;
}
// 判断是否用户公众号的消息
$userMpSettings = UserMPHelper::getMpSetting();
if ($payload['appid'] == $userMpSettings->appid) {
$wechat = UserMPHelper::getWxPHPSDK();
}else {
$wechat = WxHelper::getWxPHPSDK();
}
unset($payload['appid']);
// 返回true的情况,会自动删除成功的任务。
self::Log("[执行[客服通知]异步发送任务]: " . json_encode($payload));
$result = $wechat->sendCustomMessage($payload);
// 存在错误码
if (isset($result['errcode'])) {
$e['success'] = false;
$e['errcode'] = $result['errcode'];
$e['errmsg'] = $result['errmsg'];
} else {
$e['success'] = true;
}
return $e;
}
}