WechatMessageHelper.php
3.77 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
<?php
namespace common\helpers;
use common\exts\wechat\Log as WxLog;
use common\exts\wechat\PHPSDK as WxPHPSDK;
/**
* 微信公众号消息发送助手类
* Class WechatMessageHelper
* @package common\helpers
*/
class WechatMessageHelper
{
/**
* 发送客服文本消息接口
* @param $touser 当前关注公众号的微信用户openid
* @param $content
* @param WxPHPSDK $wechat
*/
public static function sendCustomerTextMessage($touser, $content, WxPHPSDK $wechat)
{
WxLog::init();
$data = array(
'touser' => $touser,
'msgtype' => WxPHPSDK::MSGTYPE_TEXT,
'text' => array(
'content'=>$content
)
);
$wechat->sendCustomMessage($data);
if($wechat->errCode ){
WxLog::ERROR("sendTextMessage: {$wechat->errCode} {$wechat->errMsg} ".json_encode($data));
return;
}
WxLog::DEBUG("sendTextMessage: ".json_encode($data));
}
/**
* 发送小程序卡片客服消息
* @param $touser 当前关注公众号的微信用户openid
* @param $title
* @param $pagepath
* @param $thumb_media_id
* @param WxPHPSDK $wechat
* @return bool|void
* @throws \yii\base\ErrorException
*/
public static function sendCustomerMiniProgrampageMessage($touser, $title, $pagepath, $thumb_media_id, WxPHPSDK $wechat)
{
WxLog::init();
$minaAppId = MinaHelper::getMinaSetting();
if (empty($minaAppId->appid)) {
return false;
}
//$wechat = UserMPHelper::getWxPHPSDK();
$data = array(
'touser' => $touser,
'msgtype' => WxPHPSDK::MSGTYPE_MINIPROGRAMPAGE,
'miniprogrampage' => array(
"title" => $title,
"appid" => $minaAppId->appid,
"pagepath" => $pagepath,
"thumb_media_id" => $thumb_media_id
)
);
WxLog::DEBUG("sendMiniProgrampageMessage: ".json_encode($data));
$wechat->sendCustomMessage($data);
if($wechat->errCode ){
WxLog::ERROR("sendMiniProgrampageMessage: {$wechat->errCode} {$wechat->errMsg} ".json_encode($data));
return false;
}
return true;
}
/**
* 发送客服图文消息接口
* @param $touser 当前关注公众号的微信用户openid
* @param $message
* @param WxPHPSDK $wechat
*/
public static function sendCustomerNewsMessage($touser, $message, WxPHPSDK $wechat)
{
WxLog::init();
$data = array(
'touser' => $touser,
'msgtype' => WxPHPSDK::MSGTYPE_NEWS,
'news' => array(
'articles'=>$message
)
);
$wechat->sendCustomMessage($data);
if($wechat->errCode ){
WxLog::ERROR("sendCustomerNewsMessage: {$wechat->errCode} {$wechat->errMsg} ".json_encode($data));
return;
}
WxLog::DEBUG("sendCustomerNewsMessage: ".json_encode($data));
}
/**
* 发送客服图片消息接口
* @param $touser 当前关注公众号的微信用户openid
* @param $media_id
* @param WxPHPSDK $wechat
*/
public static function sendCustomerImage($touser, $media_id, WxPHPSDK $wechat)
{
WxLog::init();
$data = array(
'touser' => $touser,
'msgtype' => WxPHPSDK::MSGTYPE_IMAGE,
'image' => array(
'media_id'=>$media_id
)
);
$wechat->sendCustomMessage($data);
if($wechat->errCode ){
WxLog::ERROR("sendCustomerImage: {$wechat->errCode} {$wechat->errMsg} ".json_encode($data));
return;
}
WxLog::DEBUG("sendCustomerImage: ".json_encode($data));
}
}