|
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\MiniProgram\SubscribeMessage; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\BaseClient; |
|
15
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
|
16
|
|
|
use ReflectionClass; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Client. |
|
20
|
|
|
* |
|
21
|
|
|
* @author hugo <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Client extends BaseClient |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc}. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $message = [ |
|
29
|
|
|
'touser' => '', |
|
30
|
|
|
'template_id' => '', |
|
31
|
|
|
'page' => '', |
|
32
|
|
|
'data' => [], |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc}. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $required = ['touser', 'template_id', 'data']; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Send a template message. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $data |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
46
|
|
|
* |
|
47
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
48
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
49
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function send(array $data = []) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$params = $this->formatMessage($data); |
|
54
|
|
|
|
|
55
|
1 |
|
$this->restoreMessage(); |
|
56
|
|
|
|
|
57
|
1 |
|
return $this->httpPostJson('cgi-bin/message/subscribe/send', $params); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $data |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
66
|
|
|
*/ |
|
67
|
1 |
|
protected function formatMessage(array $data = []) |
|
68
|
|
|
{ |
|
69
|
1 |
|
$params = array_merge($this->message, $data); |
|
70
|
|
|
|
|
71
|
1 |
|
foreach ($params as $key => $value) { |
|
72
|
1 |
|
if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) { |
|
73
|
1 |
|
throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
$params[$key] = empty($value) ? $this->message[$key] : $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
foreach ($params['data'] as $key => $value) { |
|
80
|
1 |
|
if (is_array($value)) { |
|
81
|
1 |
|
if (\array_key_exists('value', $value)) { |
|
82
|
1 |
|
$params['data'][$key] = ['value' => $value['value']]; |
|
83
|
|
|
|
|
84
|
1 |
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
if (count($value) >= 1) { |
|
88
|
|
|
$value = [ |
|
89
|
1 |
|
'value' => $value[0], |
|
90
|
|
|
// 'color' => $value[1],// color unsupported |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
} else { |
|
94
|
|
|
$value = [ |
|
95
|
1 |
|
'value' => strval($value), |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
$params['data'][$key] = $value; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
return $params; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Restore message. |
|
107
|
|
|
*/ |
|
108
|
1 |
|
protected function restoreMessage() |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->message = (new ReflectionClass(static::class))->getDefaultProperties()['message']; |
|
111
|
1 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Combine templates and add them to your personal template library under your account. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $tid |
|
117
|
|
|
* @param array $kidList |
|
118
|
|
|
* @param string|null $sceneDesc |
|
119
|
|
|
* |
|
120
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
121
|
|
|
* |
|
122
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
123
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function addTemplate(string $tid, array $kidList, string $sceneDesc = null) |
|
126
|
|
|
{ |
|
127
|
1 |
|
$sceneDesc = $sceneDesc ?? ''; |
|
128
|
1 |
|
$data = \compact('tid', 'kidList', 'sceneDesc'); |
|
129
|
|
|
|
|
130
|
1 |
|
return $this->httpPost('wxaapi/newtmpl/addtemplate', $data); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Delete personal template under account. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $id |
|
137
|
|
|
* |
|
138
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
139
|
|
|
* |
|
140
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
141
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function deleteTemplate(string $id) |
|
144
|
|
|
{ |
|
145
|
1 |
|
return $this->httpPost('wxaapi/newtmpl/deltemplate', ['priTmplId' => $id]); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get keyword list under template title. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $tid |
|
152
|
|
|
* |
|
153
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
154
|
|
|
* |
|
155
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
156
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function getTemplateKeywords(string $tid) |
|
159
|
|
|
{ |
|
160
|
1 |
|
return $this->httpGet('wxaapi/newtmpl/getpubtemplatekeywords', compact('tid')); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get the title of the public template under the category to which the account belongs. |
|
165
|
|
|
* |
|
166
|
|
|
* @param array $ids |
|
167
|
|
|
* @param int $start |
|
168
|
|
|
* @param int $limit |
|
169
|
|
|
* |
|
170
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
171
|
|
|
* |
|
172
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
173
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function getTemplateTitles(array $ids, int $start = 0, int $limit = 30) |
|
176
|
|
|
{ |
|
177
|
1 |
|
$ids = \implode(',', $ids); |
|
178
|
1 |
|
$query = \compact('ids', 'start', 'limit'); |
|
179
|
|
|
|
|
180
|
1 |
|
return $this->httpGet('wxaapi/newtmpl/getpubtemplatetitles', $query); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get list of personal templates under the current account. |
|
185
|
|
|
* |
|
186
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
187
|
|
|
* |
|
188
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
189
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
190
|
|
|
*/ |
|
191
|
1 |
|
public function getTemplates() |
|
192
|
|
|
{ |
|
193
|
1 |
|
return $this->httpGet('wxaapi/newtmpl/gettemplate'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Get the category of the applet account. |
|
198
|
|
|
* |
|
199
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
200
|
|
|
* |
|
201
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
202
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function getCategory() |
|
205
|
|
|
{ |
|
206
|
1 |
|
return $this->httpGet('wxaapi/newtmpl/getcategory'); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|