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
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Notice constructor. |
75
|
|
|
* |
76
|
|
|
* @param \EasyWeChat\Core\AccessToken $accessToken |
77
|
|
|
*/ |
78
|
7 |
|
public function __construct(AccessToken $accessToken) |
79
|
|
|
{ |
80
|
7 |
|
parent::__construct($accessToken); |
81
|
|
|
|
82
|
7 |
|
$this->messageBackup = $this->message; |
83
|
7 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set default color. |
87
|
|
|
* |
88
|
|
|
* @param string $color example: #0f0f0f |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function defaultColor($color) |
93
|
|
|
{ |
94
|
|
|
$this->defaultColor = $color; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set industry. |
101
|
|
|
* |
102
|
|
|
* @param int $industryOne |
103
|
|
|
* @param int $industryTwo |
104
|
|
|
* |
105
|
|
|
* @return \EasyWeChat\Support\Collection |
106
|
|
|
*/ |
107
|
1 |
|
public function setIndustry($industryOne, $industryTwo) |
108
|
|
|
{ |
109
|
|
|
$params = [ |
110
|
1 |
|
'industry_id1' => $industryOne, |
111
|
1 |
|
'industry_id2' => $industryTwo, |
112
|
1 |
|
]; |
113
|
|
|
|
114
|
1 |
|
return $this->parseJSON('json', [self::API_SET_INDUSTRY, $params]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get industry. |
119
|
|
|
* |
120
|
|
|
* @return \EasyWeChat\Support\Collection |
121
|
|
|
*/ |
122
|
1 |
|
public function getIndustry() |
123
|
|
|
{ |
124
|
1 |
|
return $this->parseJSON('json', [self::API_GET_INDUSTRY]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Add a template and get template ID. |
129
|
|
|
* |
130
|
|
|
* @param string $shortId |
131
|
|
|
* |
132
|
|
|
* @return \EasyWeChat\Support\Collection |
133
|
|
|
*/ |
134
|
1 |
|
public function addTemplate($shortId) |
135
|
|
|
{ |
136
|
1 |
|
$params = ['template_id_short' => $shortId]; |
137
|
|
|
|
138
|
1 |
|
return $this->parseJSON('json', [self::API_ADD_TEMPLATE, $params]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get private templates. |
143
|
|
|
* |
144
|
|
|
* @return \EasyWeChat\Support\Collection |
145
|
|
|
*/ |
146
|
1 |
|
public function getPrivateTemplates() |
147
|
|
|
{ |
148
|
1 |
|
return $this->parseJSON('json', [self::API_GET_ALL_PRIVATE_TEMPLATE]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Delete private template. |
153
|
|
|
* |
154
|
|
|
* @param string $templateId |
155
|
|
|
* |
156
|
|
|
* @return \EasyWeChat\Support\Collection |
157
|
|
|
*/ |
158
|
1 |
|
public function deletePrivateTemplate($templateId) |
159
|
|
|
{ |
160
|
1 |
|
$params = ['template_id' => $templateId]; |
161
|
|
|
|
162
|
1 |
|
return $this->parseJSON('json', [self::API_DEL_PRIVATE_TEMPLATE, $params]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Send a notice message. |
167
|
|
|
* |
168
|
|
|
* @param $data |
169
|
|
|
* |
170
|
|
|
* @return \EasyWeChat\Support\Collection |
171
|
|
|
* |
172
|
|
|
* @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
173
|
|
|
*/ |
174
|
2 |
|
public function send($data = []) |
175
|
|
|
{ |
176
|
2 |
|
$params = array_merge($this->message, $data); |
177
|
|
|
|
178
|
2 |
|
foreach ($params as $key => $value) { |
179
|
2 |
|
if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) { |
180
|
1 |
|
throw new InvalidArgumentException("Attribute '$key' can not be empty!"); |
181
|
|
|
} |
182
|
|
|
|
183
|
2 |
|
$params[$key] = empty($value) ? $this->message[$key] : $value; |
184
|
2 |
|
} |
185
|
|
|
|
186
|
2 |
|
$params['data'] = $this->formatData($params['data']); |
187
|
|
|
|
188
|
2 |
|
$this->message = $this->messageBackup; |
189
|
|
|
|
190
|
2 |
|
return $this->parseJSON('json', [static::API_SEND_NOTICE, $params]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Magic access.. |
195
|
|
|
* |
196
|
|
|
* @param string $method |
197
|
|
|
* @param array $args |
198
|
|
|
* |
199
|
|
|
* @return Notice |
200
|
|
|
*/ |
201
|
2 |
|
public function __call($method, $args) |
202
|
|
|
{ |
203
|
|
|
$map = [ |
204
|
2 |
|
'template' => 'template_id', |
205
|
2 |
|
'templateId' => 'template_id', |
206
|
2 |
|
'uses' => 'template_id', |
207
|
2 |
|
'to' => 'touser', |
208
|
2 |
|
'receiver' => 'touser', |
209
|
2 |
|
'url' => 'url', |
210
|
2 |
|
'link' => 'url', |
211
|
2 |
|
'data' => 'data', |
212
|
2 |
|
'with' => 'data', |
213
|
2 |
|
'formId' => 'form_id', |
214
|
2 |
|
'prepayId' => 'form_id', |
215
|
2 |
|
]; |
216
|
|
|
|
217
|
2 |
|
if (0 === stripos($method, 'with') && strlen($method) > 4) { |
218
|
1 |
|
$method = lcfirst(substr($method, 4)); |
219
|
1 |
|
} |
220
|
|
|
|
221
|
2 |
|
if (0 === stripos($method, 'and')) { |
222
|
1 |
|
$method = lcfirst(substr($method, 3)); |
223
|
1 |
|
} |
224
|
|
|
|
225
|
2 |
|
if (isset($map[$method])) { |
226
|
2 |
|
$this->message[$map[$method]] = array_shift($args); |
227
|
2 |
|
} |
228
|
|
|
|
229
|
2 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Format template data. |
234
|
|
|
* |
235
|
|
|
* @param array $data |
236
|
|
|
* |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
2 |
|
protected function formatData($data) |
240
|
|
|
{ |
241
|
2 |
|
$return = []; |
242
|
|
|
|
243
|
2 |
|
foreach ($data as $key => $item) { |
244
|
1 |
|
if (is_scalar($item)) { |
245
|
1 |
|
$value = $item; |
246
|
1 |
|
$color = $this->defaultColor; |
247
|
1 |
|
} elseif (is_array($item) && !empty($item)) { |
248
|
1 |
|
if (isset($item['value'])) { |
249
|
1 |
|
$value = strval($item['value']); |
250
|
1 |
|
$color = empty($item['color']) ? $this->defaultColor : strval($item['color']); |
251
|
1 |
|
} elseif (count($item) < 2) { |
252
|
1 |
|
$value = array_shift($item); |
253
|
1 |
|
$color = $this->defaultColor; |
254
|
1 |
|
} else { |
255
|
1 |
|
list($value, $color) = $item; |
256
|
|
|
} |
257
|
1 |
|
} else { |
258
|
1 |
|
$value = 'error data item.'; |
259
|
1 |
|
$color = $this->defaultColor; |
260
|
|
|
} |
261
|
|
|
|
262
|
1 |
|
$return[$key] = [ |
263
|
1 |
|
'value' => $value, |
264
|
1 |
|
'color' => $color, |
265
|
|
|
]; |
266
|
2 |
|
} |
267
|
|
|
|
268
|
2 |
|
return $return; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|