|
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\UniformMessage; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
|
15
|
|
|
use EasyWeChat\OfficialAccount\TemplateMessage\Client as BaseClient; |
|
16
|
|
|
|
|
17
|
|
|
class Client extends BaseClient |
|
18
|
|
|
{ |
|
19
|
|
|
public const API_SEND = 'cgi-bin/message/wxopen/template/uniform_send'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc}. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $message = [ |
|
27
|
|
|
'touser' => '', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Weapp Attributes. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $weappMessage = [ |
|
36
|
|
|
'template_id' => '', |
|
37
|
|
|
'page' => '', |
|
38
|
|
|
'form_id' => '', |
|
39
|
|
|
'data' => [], |
|
40
|
|
|
'emphasis_keyword' => '', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Official account attributes. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $mpMessage = [ |
|
49
|
|
|
'appid' => '', |
|
50
|
|
|
'template_id' => '', |
|
51
|
|
|
'url' => '', |
|
52
|
|
|
'miniprogram' => [], |
|
53
|
|
|
'data' => [], |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Required attributes. |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $required = ['touser', 'template_id', 'form_id', 'miniprogram', 'appid']; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $data |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
* |
|
68
|
|
|
* @throws InvalidArgumentException |
|
69
|
|
|
*/ |
|
70
|
1 |
|
protected function formatMessage(array $data = []) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$params = array_merge($this->message, $data); |
|
73
|
|
|
|
|
74
|
1 |
|
if (empty($params['touser'])) { |
|
75
|
1 |
|
throw new InvalidArgumentException(sprintf('Attribute "touser" can not be empty!')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
if (!empty($params['weapp_template_msg'])) { |
|
79
|
1 |
|
$params['weapp_template_msg'] = $this->formatWeappMessage($params['weapp_template_msg']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
if (!empty($params['mp_template_msg'])) { |
|
83
|
1 |
|
$params['mp_template_msg'] = $this->formatMpMessage($params['mp_template_msg']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return $params; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param array $data |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
* |
|
94
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
95
|
|
|
*/ |
|
96
|
2 |
|
protected function formatWeappMessage(array $data = []) |
|
97
|
|
|
{ |
|
98
|
2 |
|
$params = $this->baseFormat($data, $this->weappMessage); |
|
99
|
|
|
|
|
100
|
2 |
|
$params['data'] = $this->formatData($params['data'] ?? []); |
|
101
|
|
|
|
|
102
|
2 |
|
return $params; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $data |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
* |
|
110
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
111
|
|
|
*/ |
|
112
|
3 |
|
protected function formatMpMessage(array $data = []) |
|
113
|
|
|
{ |
|
114
|
3 |
|
$params = $this->baseFormat($data, $this->mpMessage); |
|
115
|
|
|
|
|
116
|
3 |
|
if (empty($params['miniprogram']['appid'])) { |
|
117
|
1 |
|
$params['miniprogram']['appid'] = $this->app['config']['app_id']; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
$params['data'] = $this->formatData($params['data'] ?? []); |
|
121
|
|
|
|
|
122
|
3 |
|
return $params; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param array $data |
|
127
|
|
|
* @param array $default |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
* |
|
131
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
132
|
|
|
*/ |
|
133
|
4 |
|
protected function baseFormat($data = [], $default = []) |
|
134
|
|
|
{ |
|
135
|
4 |
|
$params = array_merge($default, $data); |
|
136
|
4 |
|
foreach ($params as $key => $value) { |
|
137
|
4 |
|
if (in_array($key, $this->required, true) && empty($value) && empty($default[$key])) { |
|
138
|
2 |
|
throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
4 |
|
$params[$key] = empty($value) ? $default[$key] : $value; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
4 |
|
return $params; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|