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
|
|
|
/** |
13
|
|
|
* Notice.php. |
14
|
|
|
* |
15
|
|
|
* @author overtrue <[email protected]> |
16
|
|
|
* @copyright 2015 overtrue <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @see https://github.com/overtrue |
19
|
|
|
* @see http://overtrue.me |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace EasyWeChat\Notice; |
23
|
|
|
|
24
|
|
|
use EasyWeChat\Core\AbstractAPI; |
25
|
|
|
use EasyWeChat\Core\AccessToken; |
26
|
|
|
use EasyWeChat\Core\Exceptions\InvalidArgumentException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Notice. |
30
|
|
|
*/ |
31
|
|
|
class Notice extends AbstractAPI |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Default color. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $defaultColor = '#173177'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Attributes. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $message = [ |
46
|
|
|
'touser' => '', |
47
|
|
|
'template_id' => '', |
48
|
|
|
'url' => '', |
49
|
|
|
'data' => [], |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Required attributes. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $required = ['touser', 'template_id']; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Message backup. |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $messageBackup; |
65
|
|
|
|
66
|
|
|
const API_SEND_NOTICE = 'https://api.weixin.qq.com/cgi-bin/message/template/send'; |
67
|
|
|
const API_SET_INDUSTRY = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry'; |
68
|
|
|
const API_ADD_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template'; |
69
|
|
|
const API_GET_INDUSTRY = 'https://api.weixin.qq.com/cgi-bin/template/get_industry'; |
70
|
|
|
const API_GET_ALL_PRIVATE_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template'; |
71
|
|
|
const API_DEL_PRIVATE_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/template/del_private_template'; |
72
|
|
|
const API_SEND_SUBSCRIPTION = 'https://api.weixin.qq.com/cgi-bin/message/template/subscribe'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Notice constructor. |
76
|
|
|
* |
77
|
|
|
* @param \EasyWeChat\Core\AccessToken $accessToken |
78
|
|
|
*/ |
79
|
8 |
|
public function __construct(AccessToken $accessToken) |
80
|
|
|
{ |
81
|
8 |
|
parent::__construct($accessToken); |
82
|
|
|
|
83
|
8 |
|
$this->messageBackup = $this->message; |
84
|
8 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set default color. |
88
|
|
|
* |
89
|
|
|
* @param string $color example: #0f0f0f |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function defaultColor($color) |
94
|
|
|
{ |
95
|
|
|
$this->defaultColor = $color; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set industry. |
102
|
|
|
* |
103
|
|
|
* @param int $industryOne |
104
|
|
|
* @param int $industryTwo |
105
|
|
|
* |
106
|
|
|
* @return \EasyWeChat\Support\Collection |
107
|
|
|
*/ |
108
|
1 |
|
public function setIndustry($industryOne, $industryTwo) |
109
|
|
|
{ |
110
|
|
|
$params = [ |
111
|
1 |
|
'industry_id1' => $industryOne, |
112
|
1 |
|
'industry_id2' => $industryTwo, |
113
|
1 |
|
]; |
114
|
|
|
|
115
|
1 |
|
return $this->parseJSON('json', [self::API_SET_INDUSTRY, $params]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get industry. |
120
|
|
|
* |
121
|
|
|
* @return \EasyWeChat\Support\Collection |
122
|
|
|
*/ |
123
|
1 |
|
public function getIndustry() |
124
|
|
|
{ |
125
|
1 |
|
return $this->parseJSON('json', [self::API_GET_INDUSTRY]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add a template and get template ID. |
130
|
|
|
* |
131
|
|
|
* @param string $shortId |
132
|
|
|
* |
133
|
|
|
* @return \EasyWeChat\Support\Collection |
134
|
|
|
*/ |
135
|
1 |
|
public function addTemplate($shortId) |
136
|
|
|
{ |
137
|
1 |
|
$params = ['template_id_short' => $shortId]; |
138
|
|
|
|
139
|
1 |
|
return $this->parseJSON('json', [self::API_ADD_TEMPLATE, $params]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get private templates. |
144
|
|
|
* |
145
|
|
|
* @return \EasyWeChat\Support\Collection |
146
|
|
|
*/ |
147
|
1 |
|
public function getPrivateTemplates() |
148
|
|
|
{ |
149
|
1 |
|
return $this->parseJSON('json', [self::API_GET_ALL_PRIVATE_TEMPLATE]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Delete private template. |
154
|
|
|
* |
155
|
|
|
* @param string $templateId |
156
|
|
|
* |
157
|
|
|
* @return \EasyWeChat\Support\Collection |
158
|
|
|
*/ |
159
|
1 |
|
public function deletePrivateTemplate($templateId) |
160
|
|
|
{ |
161
|
1 |
|
$params = ['template_id' => $templateId]; |
162
|
|
|
|
163
|
1 |
|
return $this->parseJSON('json', [self::API_DEL_PRIVATE_TEMPLATE, $params]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Send a notice message. |
168
|
|
|
* |
169
|
|
|
* @param $data |
170
|
|
|
* |
171
|
|
|
* @return \EasyWeChat\Support\Collection |
172
|
|
|
* |
173
|
|
|
* @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
174
|
|
|
*/ |
175
|
2 |
|
public function send($data = []) |
176
|
|
|
{ |
177
|
2 |
|
return $this->parseJSON('json', [static::API_SEND_NOTICE, $this->validParams($data)]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Send template-message for subscription. |
182
|
|
|
* |
183
|
|
|
* @param array $data |
184
|
|
|
* |
185
|
|
|
* @return \EasyWeChat\Support\Collection |
186
|
|
|
*/ |
187
|
1 |
|
public function sendSubscription(array $data = []) |
188
|
|
|
{ |
189
|
1 |
|
return $this->parseJSON('json', [static::API_SEND_SUBSCRIPTION, $this->validParams($data)]); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param array $data |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
3 |
|
protected function validParams(array $data = []) |
198
|
|
|
{ |
199
|
3 |
|
$params = array_merge($this->message, $data); |
200
|
|
|
|
201
|
3 |
|
foreach ($params as $key => $value) { |
202
|
3 |
|
if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) { |
203
|
2 |
|
throw new InvalidArgumentException("Attribute '$key' can not be empty!"); |
204
|
|
|
} |
205
|
|
|
|
206
|
3 |
|
$params[$key] = empty($value) ? $this->message[$key] : $value; |
207
|
3 |
|
} |
208
|
|
|
|
209
|
3 |
|
$params['data'] = $this->formatData($params['data']); |
210
|
|
|
|
211
|
3 |
|
$this->message = $this->messageBackup; |
212
|
|
|
|
213
|
3 |
|
return $params; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Magic access.. |
218
|
|
|
* |
219
|
|
|
* @param string $method |
220
|
|
|
* @param array $args |
221
|
|
|
* |
222
|
|
|
* @return Notice |
223
|
|
|
*/ |
224
|
3 |
|
public function __call($method, $args) |
225
|
|
|
{ |
226
|
|
|
$map = [ |
227
|
3 |
|
'template' => 'template_id', |
228
|
3 |
|
'templateId' => 'template_id', |
229
|
3 |
|
'uses' => 'template_id', |
230
|
3 |
|
'to' => 'touser', |
231
|
3 |
|
'receiver' => 'touser', |
232
|
3 |
|
'url' => 'url', |
233
|
3 |
|
'link' => 'url', |
234
|
3 |
|
'data' => 'data', |
235
|
3 |
|
'with' => 'data', |
236
|
3 |
|
'formId' => 'form_id', |
237
|
3 |
|
'prepayId' => 'form_id', |
238
|
3 |
|
]; |
239
|
|
|
|
240
|
3 |
|
if (0 === stripos($method, 'with') && strlen($method) > 4) { |
241
|
2 |
|
$method = lcfirst(substr($method, 4)); |
242
|
2 |
|
} |
243
|
|
|
|
244
|
3 |
|
if (0 === stripos($method, 'and')) { |
245
|
2 |
|
$method = lcfirst(substr($method, 3)); |
246
|
2 |
|
} |
247
|
|
|
|
248
|
3 |
|
if (isset($map[$method])) { |
249
|
3 |
|
$this->message[$map[$method]] = array_shift($args); |
250
|
3 |
|
} |
251
|
|
|
|
252
|
3 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Format template data. |
257
|
|
|
* |
258
|
|
|
* @param array $data |
259
|
|
|
* |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
3 |
|
protected function formatData($data) |
263
|
|
|
{ |
264
|
3 |
|
$return = []; |
265
|
|
|
|
266
|
3 |
|
foreach ($data as $key => $item) { |
267
|
2 |
|
if (is_scalar($item)) { |
268
|
1 |
|
$value = $item; |
269
|
1 |
|
$color = $this->defaultColor; |
270
|
2 |
|
} elseif (is_array($item) && !empty($item)) { |
271
|
2 |
|
if (isset($item['value'])) { |
272
|
2 |
|
$value = strval($item['value']); |
273
|
2 |
|
$color = empty($item['color']) ? $this->defaultColor : strval($item['color']); |
274
|
2 |
|
} elseif (count($item) < 2) { |
275
|
1 |
|
$value = array_shift($item); |
276
|
1 |
|
$color = $this->defaultColor; |
277
|
1 |
|
} else { |
278
|
1 |
|
list($value, $color) = $item; |
279
|
|
|
} |
280
|
2 |
|
} else { |
281
|
1 |
|
$value = 'error data item.'; |
282
|
1 |
|
$color = $this->defaultColor; |
283
|
|
|
} |
284
|
|
|
|
285
|
2 |
|
$return[$key] = [ |
286
|
2 |
|
'value' => $value, |
287
|
2 |
|
'color' => $color, |
288
|
|
|
]; |
289
|
3 |
|
} |
290
|
|
|
|
291
|
3 |
|
return $return; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|