1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace EasyWeChat\Work\Crm; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use EasyWeChat\Kernel\BaseClient; |
8
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class MessageClient. |
12
|
|
|
* |
13
|
|
|
* @author milkmeowo <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class MessageClient extends BaseClient |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Required attributes. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $required = ['content', 'media_id', 'title', 'url', 'pic_media_id', 'appid', 'page']; |
23
|
|
|
|
24
|
|
|
protected $textMessage = [ |
25
|
|
|
'content' => '' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
protected $imageMessage = [ |
29
|
|
|
'media_id' => '' |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
protected $linkMessage = [ |
33
|
|
|
'title' => '', |
34
|
|
|
'picurl' => '', |
35
|
|
|
'desc' => '', |
36
|
|
|
'url' => '', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
protected $miniprogramMessage = [ |
40
|
|
|
'title' => '', |
41
|
|
|
'pic_media_id' => '', |
42
|
|
|
'appid' => '', |
43
|
|
|
'page' => '' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 添加企业群发消息模板 |
48
|
|
|
* @link https://work.weixin.qq.com/api/doc#90000/90135/91560 |
49
|
|
|
* |
50
|
|
|
* @param array $msg |
51
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
52
|
|
|
* @throws InvalidArgumentException |
53
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
54
|
|
|
*/ |
55
|
|
|
public function addTemplateMessage(array $msg) |
56
|
|
|
{ |
57
|
|
|
$params = $this->formatMessage($msg); |
58
|
|
|
|
59
|
|
|
return $this->httpPostJson('cgi-bin/externalcontact/add_msg_template', $params); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 获取企业群发消息发送结果 |
64
|
|
|
* @link https://work.weixin.qq.com/api/doc#90000/90135/91561 |
65
|
|
|
* |
66
|
|
|
* @param $msgId |
67
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
68
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
69
|
|
|
*/ |
70
|
|
|
public function getGroupMsgResult($msgId) |
71
|
|
|
{ |
72
|
|
|
return $this->httpPostJson('cgi-bin/externalcontact/get_group_msg_result', [ |
73
|
|
|
'msgid' => $msgId |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 发送新客户欢迎语 |
79
|
|
|
* @link https://work.weixin.qq.com/api/doc#90000/90135/91688 |
80
|
|
|
* |
81
|
|
|
* @param string $welcomeCode |
82
|
|
|
* @param array $msg |
83
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
84
|
|
|
* @throws InvalidArgumentException |
85
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
86
|
|
|
*/ |
87
|
|
|
public function sendWelcomeMsg(string $welcomeCode, array $msg) |
88
|
|
|
{ |
89
|
|
|
$formattedMsg = $this->formatMessage($msg); |
90
|
|
|
|
91
|
|
|
$params = array_merge($formattedMsg, [ |
92
|
|
|
'welcome_code' => $welcomeCode |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
return $this->httpPostJson('cgi-bin/externalcontact/send_welcome_msg', $params); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $data |
100
|
|
|
* @return array |
101
|
|
|
* @throws InvalidArgumentException |
102
|
|
|
*/ |
103
|
|
|
protected function formatMessage(array $data = []) |
104
|
|
|
{ |
105
|
|
|
$params = $data; |
106
|
|
|
|
107
|
|
|
if (!empty($params['text'])) { |
108
|
|
|
$params['text'] = $this->baseFormat($params['text'], $this->textMessage); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!empty($params['image'])) { |
112
|
|
|
$params['image'] = $this->baseFormat($params['image'], $this->imageMessage); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!empty($params['link'])) { |
116
|
|
|
$params['link'] = $this->baseFormat($params['link'], $this->linkMessage); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (!empty($params['miniprogram'])) { |
120
|
|
|
$params['miniprogram'] = $this->baseFormat($params['miniprogram'], $this->miniprogramMessage); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $params; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $data |
129
|
|
|
* @param array $default |
130
|
|
|
* @return array |
131
|
|
|
* @throws InvalidArgumentException |
132
|
|
|
*/ |
133
|
|
|
protected function baseFormat($data = [], $default = []) |
134
|
|
|
{ |
135
|
|
|
$params = array_merge($default, $data); |
136
|
|
|
foreach ($params as $key => $value) { |
137
|
|
|
if (in_array($key, $this->required, true) && empty($value) && empty($default[$key])) { |
138
|
|
|
throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$params[$key] = empty($value) ? $default[$key] : $value; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $params; |
145
|
|
|
} |
146
|
|
|
} |