Group.php
14.1 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Created by PhpStorm.
* User: ty01
* Date: 2022/3/10
* Time: 14:31
*/
namespace app\api\controller\v1;
use app\admin\model\collection\FocusCollection;
use app\common\helper\HttpHelper;
use think\Db;
use think\Request;
/**
* 角色组
* Class Group
* @package app\api\controller\core
*/
class Group extends Base
{
/**
* 1.管理组及其检索关键词
*/
public function list()
{
$where['searchword'] = ['neq', ''];
$where['id'] = ['gt', 1];
$result = Db::name("auth_group")->where($where)->field("id as group_id,searchword")->select();
if (!empty($result)) {
foreach ($result as $k => $v) {
//所有微信公众号
$w = [];
$w = [
"from" => "微信",
"auth_group_id" => $v['group_id']
];
$collection = Db::name("focus_collection")->where($w)->column("unikey");
// if (empty($collection)) {
// unset($result[$k]);
// continue;
// }
//获取角色组下所有管理员发布的敏感词返回数组
$uids = Db::name("auth_group_access")->where("group_id", $v['group_id'])->column("uid");
if (empty($uids)) {
unset($result[$k]);
continue;
}
$keywords = Db::name("theme_keywords")->where('admin_id', 'in', $uids)->column("name as keywords_name");
if (empty($keywords)) {
unset($result[$k]);
continue;
}
$result[$k]['collection'] = $collection;
$result[$k]['keywords'] = $keywords;
}
}
$this->success("数据获取成功", $result);
}
/**
* 2.获取管理组、检索关键词、最新begin数据
*/
public function group_list()
{
$where['searchword'] = ['neq', ''];
$where['id'] = ['gt', 1];
$result = Db::name("auth_group")->where($where)->field("id as group_id,searchword")->select();
if (!empty($result)) {
foreach ($result as $k => $v) {
//所有微信公众号
$w = [];
$w = [
"from" => "微信",
"auth_group_id" => $v['group_id']
];
$begin = Db::name("focus_collection")->where($w)->order("begin desc")->value("begin");
$result[$k]['begin'] = $begin ? $begin : 0;
}
}
$this->success("数据获取成功!", $result);
}
/**
* 2. 一键拉取对应角色组检索关键词的公众号
* @param group_id 角色组id
* @param begin 最新一篇文章的begin值;
*/
public function pullFocusCollection()
{
echo "kevin";
die;
$response = $this->request->param();
if (empty($response['group_id'])) {
return array_callback(false, "角色组id不能为空");
}
if (empty($response['key'])) {
return array_callback(false, "检索关键词不能为空");
}
$params["group_id"] = $response['group_id'];
$params["key"] = $response['key'];
$params["begin"] = $response['begin'] > 0 ? intval($response['begin']) : 0;
$query_string = is_array($params) ? http_build_query($params) : $params;
// $request_url = "http://op.cn/api/v1/group/test";
$request_url = "http://124.71.110.50:8080/getpublic";
$geturl = $query_string ? $request_url . (stripos($request_url, "?") !== false ? "&" : "?") . $query_string : $request_url;
$s = file_get_contents($geturl);
$res = json_decode($s, true);
// $res = json_decode($res, true);
$time = time();
$insert_data = [];
//日志入库
$log = [
"auth_group_id" => $response['group_id'],
"request_url" => $request_url,
"request_method" => "GET",
"request_param" => json_encode($params, JSON_UNESCAPED_UNICODE),
"response_code" => $res['code'],
"createtime" => $time
];
if (!empty($res['data'])) {
foreach ($res['data'] as $k => $v) {
$from = "微信";
$admin_id = Db::name("auth_group_access")->where("group_id", $v['group_id'])->value("uid");
//可以优化成查询最新begin做对比
$count = Db::name("focus_collection")->where("unikey", $v['id'])->count();
if ($count == 0) {
$insert_data[] = [
"from" => $from,
"unikey" => $v['id'],
"name" => $v['name'],
"pinyin" => $v['alias'],
"headavatar" => $v['round_head_img'],
"service_type" => $v['service_type'] ? $v['service_type'] : 1,
"signature" => $v['signature'],
"begin" => $v['begin'],
"createtime" => $time,
"updatetime" => $time,
"admin_id" => $admin_id ? $admin_id : 1,
"auth_group_id" => $v['group_id']
];
}
}
if (!empty($insert_data)) {
// 启动事务
Db::startTrans();
try {
$res = Db::name("focus_collection")->insertAll($insert_data);
if (!$res) {
Db::rollback();
return false;
}
// 执行提交操作
Db::commit();
} catch (\Exception $e) {
// 更新失败 回滚事务
Db::rollback();
Db::name("focus_collection_log")->insert($log);
$this->error("数据更新失败!" . $e->getMessage());
}
$log["upt_num"] = $res;
Db::name("focus_collection_log")->insert($log);
$this->success("数据更新成功【" . $res . "条】");
}
}
Db::name("focus_collection_log")->insert($log);
$this->success("暂无需要更新的数据");
}
public function test()
{
$data = [
[
"id" => "MzA5MDQxNjgyNw==",
"name" => "今日蒙自",
"alias" => "jrmzzx",
"round_head_img" => "https://open.hikvision.com/img/a985847.png",
"service_type" => 1,
"signature" => "发布蒙自人文、旅游、民生、时政最新资讯,关注大事、小事、身边事",
"begin" => 1,
"group_id" => 4
],
[
"id" => "MzI3Nzc5NtcwMg==",
"name" => "蒙自招聘",
"alias" => "jrmzzx",
"round_head_img" => "https://open.hikvision.com/img/a985847.png",
"service_type" => 1,
"signature" => "发布蒙自人文、旅游、民生、时政最新资讯,关注大事、小事、身边事",
"begin" => 2,
"group_id" => 4
],
// [
// "id" => "MzAwNzI2Njk4Mg==",
// "name" => "锡都个旧",
// "alias" => "gjsrmtzx",
// "round_head_img" => "https://open.hikvision.com/img/a985847.png",
// "service_type" => 1,
// "signature" => "发布个旧旅游、民生、时政最新资讯,关注大事、小事、身边事",
// "begin" => 1,
// "group_id" => 5
// ],
// [
// "id" => "MzAxNTMwOTE1Mg==",
// "name" => "早安个旧",
// "alias" => "zaoangejiu",
// "round_head_img" => "https://open.hikvision.com/img/a985847.png",
// "service_type" => 1,
// "signature" => "发布个旧民生、时政最新资讯,关注大事、小事、身边事",
// "begin" => 2,
// "group_id" => 5
// ],
];
$this->success("成功", $data);
}
/**
* 4. 公众号批量入库
*/
public function addFocusCollection()
{
$request = Request::instance();
$response = $this->request->param();
if (!is_array($response)) {
$this->error("请求数据格式异常");
}
$time = time();
$insert_data = [];
if (!empty($response)) {
foreach ($response as $k => $v) {
$from = "微信";
$admin_id = Db::name("auth_group_access")->where("group_id", $v['group_id'])->value("uid");//获取角色组下第一层的某个管理id
$count = Db::name("focus_collection")->where("unikey", $v['id'])->count();//检查unikey是否存在
if ($count == 0) {
//没入过库
$insert_data[] = [
"from" => $from,
"unikey" => $v['id'],
"name" => $v['name'],
"pinyin" => $v['alias'],
"headavatar" => $v['round_head_img'],
"service_type" => $v['service_type'] ? $v['service_type'] : 1,
"signature" => $v['signature'],
"begin" => $v['begin'],
"createtime" => $time,
"updatetime" => $time,
"admin_id" => $admin_id ? $admin_id : 1,
"auth_group_id" => $v['group_id'],
"account_principal"=>$v['themainbody'] //微信号账号主体
];
}
}
if (!empty($insert_data)) {
// 启动事务
Db::startTrans();
try {
$res = Db::name("focus_collection")->insertAll($insert_data);
if (!$res) {
Db::rollback();
return false;
}
// 执行提交操作
Db::commit();
} catch (\Exception $e) {
// 更新失败 回滚事务
Db::rollback();
//日志入库
$log = [];
$log = [
"auth_group_id" => $response[0]['group_id'],
"request_url" => $request->url(true),
"request_method" => $_SERVER['REQUEST_METHOD'],
"request_ip" => $request->ip(),
"request_param" => json_encode($response, JSON_UNESCAPED_UNICODE),
"response_code" => 0,
"upt_num" => 0,
"createtime" => $time
];
Db::name("focus_collection_log")->insert($log);
$this->error("数据更新失败!" . $e->getMessage());
}
$log = [];
$log = [
"auth_group_id" => $response[0]['group_id'],
"request_url" => $request->url(true),
"request_method" => $_SERVER['REQUEST_METHOD'],
"request_ip" => $request->ip(),
"request_param" => json_encode($response, JSON_UNESCAPED_UNICODE),
"response_code" => 0,
"upt_num" => $res,
"createtime" => $time
];
Db::name("focus_collection_log")->insert($log);
$this->success("数据更新成功【" . $res . "条】");
}
}
$log = [];
$log = [
"auth_group_id" => $response[0]['group_id'],
"request_url" => $request->url(true),
"request_method" => $_SERVER['REQUEST_METHOD'],
"request_ip" => $request->ip(),
"request_param" => json_encode($response, JSON_UNESCAPED_UNICODE),
"response_code" => 0,
"upt_num" => 0,
"createtime" => $time
];
Db::name("focus_collection_log")->insert($log);
$this->success("暂无需要更新的数据");
}
/**
* 5. 根据渠道名称获取渠道unikey
*/
public function get_fakeid()
{
//获取渠道数组
$focusCollectionModel = new FocusCollection();
$from_list = $focusCollectionModel->getFromList();
$from_list = array_keys($from_list);
$param = $this->request->param();
$from = $param['from'];//渠道来源
$name = $param['name'];//渠道名称
if (empty($from)) {
return array_callback(false, "请先选择渠道来源");
} elseif (!in_array($from, $from_list)) {
return array_callback(false, "请先选择有效的渠道来源");
}
if (empty($name)) {
return array_callback(false, "请先填写渠道名称");
}
$where = [
"from" => $from,
"name" => $name
];
$count = $focusCollectionModel->where($where)->count();
if ($count > 0) {
return array_callback(false, $param['name'] . "`已存在,请更换渠道名称后重新操作");
}
//请求接口
$url = "http://124.71.110.50:8080/get_fakeid?name=" . $name;
$res = HttpHelper::curl($url,"GET");
$result = json_decode($res,true);
if($result['code']!=1){
return array_callback(false, "接口请求异常".$result['msg']);
}else{
return array_callback(true,"数据获取成功",$result['data']);
}
}
}