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
|
|
|
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
|
2 |
|
protected function formatWeappMessage(array $data = []) |
95
|
|
|
{ |
96
|
2 |
|
$params = $this->baseFormat($data, $this->weappMessage); |
97
|
|
|
|
98
|
2 |
|
$params['data'] = $this->formatData($params['data'] ?? []); |
99
|
|
|
|
100
|
2 |
|
return $params; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $data |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
3 |
|
protected function formatMpMessage(array $data = []) |
109
|
|
|
{ |
110
|
3 |
|
$params = $this->baseFormat($data, $this->mpMessage); |
111
|
|
|
|
112
|
3 |
|
if (empty($params['miniprogram']['appid'])) { |
113
|
1 |
|
$params['miniprogram']['appid'] = $this->app['config']['app_id']; |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
$params['data'] = $this->formatData($params['data'] ?? []); |
117
|
|
|
|
118
|
3 |
|
return $params; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $data |
123
|
|
|
* @param array $default |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
* |
127
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
128
|
|
|
*/ |
129
|
4 |
|
protected function baseFormat($data = [], $default = []) |
130
|
|
|
{ |
131
|
4 |
|
$params = array_merge($default, $data); |
132
|
4 |
|
foreach ($params as $key => $value) { |
133
|
4 |
|
if (in_array($key, $this->required, true) && empty($value) && empty($default[$key])) { |
134
|
2 |
|
throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key)); |
135
|
|
|
} |
136
|
|
|
|
137
|
4 |
|
$params[$key] = empty($value) ? $default[$key] : $value; |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
return $params; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|