|
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
|
|
|
public function send(array $data = []) |
|
52
|
|
|
{ |
|
53
|
|
|
$params = $this->formatMessage($data); |
|
54
|
|
|
|
|
55
|
|
|
$this->restoreMessage(); |
|
56
|
|
|
|
|
57
|
|
|
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
|
|
|
private function formatMessage(array $data = []) |
|
68
|
|
|
{ |
|
69
|
|
|
$params = array_merge($this->message, $data); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($params as $key => $value) { |
|
72
|
|
|
if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) { |
|
73
|
|
|
throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$params[$key] = empty($value) ? $this->message[$key] : $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$params['data'] = $this->formatData($params['data']); |
|
80
|
|
|
|
|
81
|
|
|
return $params; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $data |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function formatData(array $data) |
|
90
|
|
|
{ |
|
91
|
|
|
$formatted = []; |
|
92
|
|
|
|
|
93
|
|
|
foreach ($data as $key => $value) { |
|
94
|
|
|
if (is_array($value)) { |
|
95
|
|
|
if (isset($value['value'])) { |
|
96
|
|
|
$formatted[$key] = $value; |
|
97
|
|
|
|
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (count($value) >= 1) { |
|
102
|
|
|
$value = [ |
|
103
|
|
|
'value' => $value[0], |
|
104
|
|
|
// 'color' => $value[1],// color unsupported |
|
105
|
|
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
} else { |
|
108
|
|
|
$value = [ |
|
109
|
|
|
'value' => strval($value), |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$formatted[$key] = $value; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $formatted; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Restore message. |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function restoreMessage() |
|
123
|
|
|
{ |
|
124
|
|
|
$this->message = (new ReflectionClass(static::class))->getDefaultProperties()['message']; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|