Dinghorn.php
8.8 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
<?php
namespace addons\dinghorn;
use addons\dinghorn\library\DinghornLib;
use app\common\library\Menu;
use think\Addons;
use think\Db;
/**
* 钉钉小喇叭
*/
class Dinghorn extends Addons
{
/**
* 通过模板发送通知消息
* @param string $template 消息模板Id或Code
* @param array $tpl_data 要覆盖的模板数据 (参见:\application\admin\controller\dinghorn\Example.php 的 example2 方法)
* @param array $tpl_variable 模板变量键值对,若模板变量已预先定义,则会自动获取值,无需在此传递
* @return array 发送结果
*/
public static function msgNotice($template, $tpl_data = array(), $tpl_variable = array())
{
$now_time = time();
$dinghorn_config = get_addon_config('dinghorn');
$template = Db::name('dinghorn_template')
->where('id = :tpl OR code = :tpl_code', ['tpl' => $template, 'tpl_code' => $template])
->where('deletetime', null)
->find();
if (!$template || !$template['openswitch']) {
if ($dinghorn_config['error_log']) {
$log_arr = array(
'robot_id' => 0,
'template_id' => isset($template['id']) ? $template['id'] : 0,
'template_data' => json_encode($tpl_data),
'errmsg' => '消息模板未找到或已禁用!(-1)',
'status' => '0',
'createtime' => $now_time
);
Db::name('dinghorn_msglog')->insert($log_arr);
}
return ['errcode' => -1, 'errmsg' => '消息模板未找到或已禁用!'];
}
$DinghornLib = new DinghornLib($template, $tpl_data, $tpl_variable);
// 获取模板内的机器人
$robot_ids = isset($tpl_data['robot_ids']) ? $tpl_data['robot_ids'] : $template['robot_ids'];
$robots = Db::name('dinghorn_robot')
->whereIn('id', $robot_ids)
->where('openswitch', 1)
->where('deletetime', null)
->select();
// 组装消息数据
$msg_data = $DinghornLib->deal_with_msg();
$log_arr = [];//要插入的日志
$is_err = false;//是否有错误,用于判定本次操作的返回值
// 循环所有要发送的机器人
foreach ($robots as $key => $value) {
$res = $DinghornLib->msgSend($value['access_token'], $msg_data, $value['sign']);
if ($res['errcode'] == 0 && $dinghorn_config['success_log']) {
$log_arr[] = array(
'robot_id' => $value['id'],
'template_id' => $template['id'],
'template_data' => json_encode($msg_data),
'errmsg' => '',
'status' => '1',
'createtime' => $now_time
);
} else if ($res['errcode'] != 0) {
$is_err = true;
$errmsg = $res['errmsg'] . '(' . $res['errcode'] . ')';
if ($dinghorn_config['error_log']) {
$log_arr[] = array(
'robot_id' => $value['id'],
'template_id' => $template['id'],
'template_data' => json_encode($msg_data),
'errmsg' => $errmsg,
'status' => '0',
'createtime' => $now_time
);
}
}
}
if ($log_arr) {
Db::startTrans();
try {
Db::name('dinghorn_msglog')->insertAll($log_arr);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
}
return $is_err ? ['errcode' => -2, 'errmsg' => '消息发送失败:' . $errmsg] : ['errcode' => 0];
}
/**
* 插件安装方法
* @return bool
*/
public function install()
{
$menu = [
[
'name' => 'dinghorn',
'title' => '钉钉小喇叭',
'icon' => 'fa fa-volume-up',
'sublist' => [
[
'name' => 'dinghorn/robot',
'title' => '机器人管理',
'icon' => 'fa fa-circle-o',
'sublist' => [
['name' => 'dinghorn/robot/index', 'title' => '查看'],
['name' => 'dinghorn/robot/add', 'title' => '添加'],
['name' => 'dinghorn/robot/edit', 'title' => '编辑'],
['name' => 'dinghorn/robot/del', 'title' => '删除'],
['name' => 'dinghorn/robot/multi', 'title' => '批量更新'],
['name' => 'dinghorn/robot/recyclebin', 'title' => '回收站'],
['name' => 'dinghorn/robot/destroy', 'title' => '真实删除'],
['name' => 'dinghorn/robot/restore', 'title' => '还原'],
]
],
[
'name' => 'dinghorn/variable',
'title' => '模板变量管理',
'icon' => 'fa fa-circle-o',
'remark' => '在此处设置变量,随后可在模板通知内使用变量,变量值可来源于方法返回值或SQL查询结果',
'sublist' => [
['name' => 'dinghorn/variable/index', 'title' => '查看'],
['name' => 'dinghorn/variable/add', 'title' => '添加'],
['name' => 'dinghorn/variable/edit', 'title' => '编辑'],
['name' => 'dinghorn/variable/del', 'title' => '删除'],
['name' => 'dinghorn/variable/multi', 'title' => '批量更新'],
['name' => 'dinghorn/variable/recyclebin', 'title' => '回收站'],
['name' => 'dinghorn/variable/destroy', 'title' => '真实删除'],
['name' => 'dinghorn/variable/restore', 'title' => '还原'],
['name' => 'dinghorn/variable/view_variable', 'title' => '计算变量值'],
]
],
[
'name' => 'dinghorn/template',
'title' => '通知模板管理',
'icon' => 'fa fa-circle-o',
'sublist' => [
['name' => 'dinghorn/template/index', 'title' => '查看'],
['name' => 'dinghorn/template/add', 'title' => '添加'],
['name' => 'dinghorn/template/edit', 'title' => '编辑'],
['name' => 'dinghorn/template/del', 'title' => '删除'],
['name' => 'dinghorn/template/multi', 'title' => '批量更新'],
['name' => 'dinghorn/template/recyclebin', 'title' => '回收站'],
['name' => 'dinghorn/template/destroy', 'title' => '真实删除'],
['name' => 'dinghorn/template/restore', 'title' => '还原'],
]
],
[
'name' => 'dinghorn/msglog',
'title' => '通知发送日志管理',
'icon' => 'fa fa-circle-o',
'sublist' => [
['name' => 'dinghorn/msglog/index', 'title' => '查看'],
['name' => 'dinghorn/msglog/edit', 'title' => '编辑'],
['name' => 'dinghorn/msglog/del', 'title' => '删除'],
['name' => 'dinghorn/msglog/multi', 'title' => '批量更新'],
['name' => 'dinghorn/msglog/recyclebin', 'title' => '回收站'],
['name' => 'dinghorn/msglog/destroy', 'title' => '真实删除'],
['name' => 'dinghorn/msglog/restore', 'title' => '还原'],
]
],
]
]
];
Menu::create($menu);
return true;
}
/**
* 插件卸载方法
* @return bool
*/
public function uninstall()
{
Menu::delete('dinghorn');
return true;
}
/**
* 插件启用方法
* @return bool
*/
public function enable()
{
Menu::enable('dinghorn');
return true;
}
/**
* 插件禁用方法
* @return bool
*/
public function disable()
{
Menu::disable('dinghorn');
return true;
}
}