Notification.php
7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace app\admin\controller\groupon;
use app\common\controller\Backend;
/**
* 消息管理
*
* @icon fa fa-circle-o
*/
class Notification extends Backend
{
/**
* Notification模型对象
* @var \app\admin\model\groupon\Notification
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\groupon\Notification;
$this->modelConfig = new \app\admin\model\groupon\notification\Config;
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
}
public function config()
{
if ($this->request->isAjax()) {
// 检测队列
checkEnv('queue');
// 消息类型
$notificationType = [
\addons\groupon\notifications\Refund::class,
\addons\groupon\notifications\Wallet::class,
\addons\groupon\notifications\Order::class,
\addons\groupon\notifications\store\Order::class,
\addons\groupon\notifications\store\Apply::class
];
// 获取所有要发送的消息
$fields = [];
foreach ($notificationType as $key => $class) {
$currentFields = $class::$returnField;
if ($currentFields) {
$fields = array_merge($fields, $currentFields);
}
}
// 读取数据库相关消息配置项
$notificationConfig = $this->modelConfig->select();
// 组合消息类型和设置值
$newFields = [];
foreach ($fields as $k => $field) {
// 组合每个平台的消息默认值和数据库值
$kConfig = $this->getKconfig($notificationConfig, $k, $field);
$newFields[] = [
'type' => $k,
'name' => $field['name'],
'wxMiniProgram' => $kConfig['wxMiniProgram'] ?? [],
'wxOfficialAccount' => $kConfig['wxOfficialAccount'] ?? [],
'sms' => $kConfig['sms'] ?? [],
'email' => $kConfig['email'] ?? []
];
}
$this->success('获取成功', null, $newFields);
}
return $this->view->fetch();
}
// 配置状态
public function set_status() {
$platform = $this->request->post('platform', '');
$event = $this->request->post('event', '');
$name = $this->request->post('name', '');
$status = $this->request->post('status', 0);
if (!$platform || !$event) {
$this->error(__('Parameter %s can not be empty', ''));
}
$config = $this->modelConfig->where([
'platform' => $platform,
'event' => $event
])->find();
if (!$config) {
$config = $this->modelConfig;
$config->platform = $platform;
$config->event = $event;
$config->name = $name;
}
$config->status = intval($status);
$config->save();
$this->success('设置成功');
}
// 配置模板
public function set_template()
{
$platform = $this->request->post('platform');
$event = $this->request->post('event');
$name = $this->request->post('name');
$content = $this->request->post('content', "");
if (!$platform || !$event) {
$this->error(__('Parameter %s can not be empty', ''));
}
$config = $this->modelConfig->where([
'platform' => $platform,
'event' => $event
])->find();
if (!$config) {
$config = $this->modelConfig;
$config->platform = $platform;
$config->event = $event;
$config->name = $name;
}
$config->content = $content;
$config->save();
$this->success('设置成功');
}
private function getKConfig($notificationConfig, $k, $field) {
// 将默认值中追加 template_field 和 value 空字段
foreach ($field['fields'] as &$f) {
$f['template_field'] = $f['template_field'] ?? '';
$f['value'] = $f['value'] ?? '';
}
// 初始化defalut
$kConfig = [
'wxMiniProgram' => [
'id' => 0,
'platform' => 'wxMiniProgram',
'name' => $field['name'],
'event' => $k,
'status' => 0,
'sendnum' => 0,
'content_arr' => [
'template_id' => '',
'fields' => $field['fields']
]
],
'wxOfficialAccount' => [
'id' => 0,
'platform' => 'wxOfficialAccount',
'name' => $field['name'],
'event' => $k,
'status' => 0,
'sendnum' => 0,
'content_arr' => [
'template_id' => '',
'fields' => $field['fields']
]
],
'sms' => [
'id' => 0,
'platform' => 'sms',
'name' => $field['name'],
'event' => $k,
'status' => 0,
'sendnum' => 0,
'content_arr' => [
'template_id' => '',
'fields' => $field['fields']
]
],
'email' => [
'id' => 0,
'platform' => 'email',
'name' => $field['name'],
'event' => $k,
'status' => 0,
'sendnum' => 0,
'content_arr' => [
'template_id' => '',
'fields' => $field['fields']
]
]
];
// 合并数据库中的设置
foreach ($notificationConfig as $config) {
if ($config['event'] == $k) {
$currentConfig = $config->toArray();
// 如果数据库中有内容
if ($currentConfig['content_arr']) {
$contentArr = $currentConfig['content_arr'];
// 合并,数据库和默认 fields 字段(发送类型增加返回字段时候有用)
$contentArrFields = [];
if (isset($contentArr['fields']) && $contentArr['fields']) { // 判断数组是否存在 fields 设置
$contentArrFields = array_column($contentArr['fields'], null, 'field');
}
$kConfigFields = array_column($kConfig[$config['platform']]['content_arr']['fields'], null, 'field');
$configField = array_merge($kConfigFields, $contentArrFields);
$contentArr['fields'] = array_values($configField);
$currentConfig['content_arr'] = $contentArr;
} else {
// 数据库有记录,但内容是空,(先开启了开关)
$currentConfig['content_arr'] = $kConfig[$config['platform']]['content_arr'];
}
$kConfig[$config['platform']] = $currentConfig;
}
}
return $kConfig;
}
}