|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace EasyWeChat\OfficialAccount\TemplateMessage; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\BaseClient; |
|
15
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
|
16
|
|
|
use ReflectionClass; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Client. |
|
20
|
|
|
* |
|
21
|
|
|
* @author overtrue <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Client extends BaseClient |
|
24
|
|
|
{ |
|
25
|
|
|
const API_SEND = 'cgi-bin/message/template/send'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Attributes. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $message = [ |
|
33
|
|
|
'touser' => '', |
|
34
|
|
|
'template_id' => '', |
|
35
|
|
|
'url' => '', |
|
36
|
|
|
'data' => [], |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Required attributes. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $required = ['touser', 'template_id']; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set industry. |
|
48
|
|
|
* |
|
49
|
|
|
* @param int $industryOne |
|
50
|
|
|
* @param int $industryTwo |
|
51
|
|
|
* |
|
52
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setIndustry($industryOne, $industryTwo) |
|
55
|
|
|
{ |
|
56
|
|
|
$params = [ |
|
57
|
|
|
'industry_id1' => $industryOne, |
|
58
|
|
|
'industry_id2' => $industryTwo, |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
return $this->httpPostJson('cgi-bin/template/api_set_industry', $params); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get industry. |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getIndustry() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->httpPostJson('cgi-bin/template/get_industry'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Add a template and get template ID. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $shortId |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function addTemplate($shortId) |
|
82
|
|
|
{ |
|
83
|
|
|
$params = ['template_id_short' => $shortId]; |
|
84
|
|
|
|
|
85
|
|
|
return $this->httpPostJson('cgi-bin/template/api_add_template', $params); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get private templates. |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getPrivateTemplates() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->httpPostJson('cgi-bin/template/get_all_private_template'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Delete private template. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $templateId |
|
102
|
|
|
* |
|
103
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function deletePrivateTemplate($templateId) |
|
106
|
|
|
{ |
|
107
|
|
|
$params = ['template_id' => $templateId]; |
|
108
|
|
|
|
|
109
|
|
|
return $this->httpPostJson('cgi-bin/template/del_private_template', $params); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Send a template message. |
|
114
|
|
|
* |
|
115
|
|
|
* @param $data |
|
116
|
|
|
* |
|
117
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
118
|
|
|
* |
|
119
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
120
|
|
|
*/ |
|
121
|
|
|
public function send($data = []) |
|
122
|
|
|
{ |
|
123
|
|
|
$params = $this->formatMessage($data); |
|
124
|
|
|
|
|
125
|
|
|
$this->restoreMessage(); |
|
126
|
|
|
|
|
127
|
|
|
return $this->httpPostJson(static::API_SEND, $params); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Send template-message for subscription. |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $data |
|
134
|
|
|
* |
|
135
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function sendSubscription(array $data = []) |
|
140
|
|
|
{ |
|
141
|
|
|
$params = $this->formatMessage($data); |
|
142
|
|
|
|
|
143
|
|
|
$this->restoreMessage(); |
|
144
|
|
|
|
|
145
|
|
|
return $this->httpPostJson('cgi-bin/message/template/subscribe', $params); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param array $data |
|
150
|
|
|
* |
|
151
|
|
|
* @return array |
|
152
|
|
|
* |
|
153
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function formatMessage(array $data = []) |
|
156
|
|
|
{ |
|
157
|
|
|
$params = array_merge($this->message, $data); |
|
158
|
|
|
|
|
159
|
|
|
foreach ($params as $key => $value) { |
|
160
|
|
|
if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) { |
|
161
|
|
|
throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$params[$key] = empty($value) ? $this->message[$key] : $value; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$params['data'] = $this->formatData($params['data'] ?? []); |
|
168
|
|
|
|
|
169
|
|
|
return $params; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param array $data |
|
174
|
|
|
* |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function formatData(array $data) |
|
178
|
|
|
{ |
|
179
|
|
|
$formatted = []; |
|
180
|
|
|
|
|
181
|
|
|
foreach ($data as $key => $value) { |
|
182
|
|
|
if (is_array($value)) { |
|
183
|
|
|
if (isset($value['value'])) { |
|
184
|
|
|
$formatted[$key] = $value; |
|
185
|
|
|
|
|
186
|
|
|
continue; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (count($value) >= 2) { |
|
190
|
|
|
$value = [ |
|
191
|
|
|
'value' => $value[0], |
|
192
|
|
|
'color' => $value[1], |
|
193
|
|
|
]; |
|
194
|
|
|
} |
|
195
|
|
|
} else { |
|
196
|
|
|
$value = [ |
|
197
|
|
|
'value' => strval($value), |
|
198
|
|
|
]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$formatted[$key] = $value; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $formatted; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Restore message. |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function restoreMessage() |
|
211
|
|
|
{ |
|
212
|
|
|
$this->message = (new ReflectionClass(__CLASS__))->getDefaultProperties()['message']; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|