Message.php
13.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
namespace app\api\controller\inspection;
use app\common\controller\Api;
use fast\Tree;
use think\Db;
/**
*
*
* @icon fa fa-circle-o
*/
class Message extends Api
{
/**
* Depart模型对象
* @var \app\admin\model\inspection\Depart
*/
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\inspection\Message;
$this->modellog = new \app\admin\model\inspection\Messagelog;
\think\Lang::load(APP_PATH . '/admin/lang/zh-cn/inspection/message.php');
\think\Lang::load(APP_PATH . '/admin/lang/zh-cn/inspection/message_log.php');
}
public function unread_message_count()
{
$count = $this->modellog
->where('user_id', $this->auth->id)
->where('status', '0')
->count();
$data = ['UnreadMessages' => $count];
$this->success('', $data);
}
public function list()
{
$cat = $this->request->param('cat', 0);
if ($cat == 1) {
//合并版app
$condtion = [];
$page = $this->request->param('page', 1);
$limit = $this->request->param('total', 15);
$type = $this->request->param('type');
$time = $this->request->param('time');
$issend = $this->request->param('issend', 0);
$optstatus = $this->request->param("optstatus", 0);//0全部 1已读 2未读
$uid = $this->auth->id;
$opt = "LEFT";
$cdt = "log.user_id='{$uid}' and log.insepection_message_id=a.id";
if ($optstatus == 2) {
// $condtion['log.status'] = ["neq","1"];
$cdt = "log.insepection_message_id=a.id";
} elseif ($optstatus == 1) {
$opt = "INNER";
$condtion['log.status'] = ["=", "1"];
}
$total = Db::name("inspection_message")
->alias("a")
->join('inspection_message_log log', $cdt, $opt)
->field('a.id')
->where($condtion)
->group("a.id")
->order('a.id desc')
->select();
$list = Db::name("inspection_message")->alias("a")
->join('inspection_message_log log', $cdt, $opt)
->field('a.id,a.type,a.title,a.content,from_unixtime(a.createtime,"%Y-%m-%d %H:%i:%s") as crts')
->where($condtion)
->group("a.id")
->order('a.id desc')
->page($page, $limit)
->select();
if (!empty($list)) {
foreach ($list as $k => $v) {
$read = Db::name("inspection_message_log")->where(["insepection_message_id" => $v['id'], "user_id" => $uid, "status" => "1"])->find();
if ($read) {
$list[$k]["status"] = 1;
if ($optstatus == 2) {
unset($list[$k]);
}
} else {
$list[$k]['status'] = 0;
}
}
}
$total = count($total);
$ids = Db::name("inspection_message")
//->where("type","2")
->column("id");
if (!empty($ids)) {
$ww = [];
$ww["insepection_message_id"] = ["in", $ids];
$ww["user_id"] = ["=", $uid];
$ww["status"] = ["=", "1"];
$hasread = Db::name("inspection_message_log")->where($ww)->count();
$noread_num = count($ids) - $hasread > 0 ? count($ids) - $hasread : 0;
} else {
$noread_num = 0;
}
$data = [
'page' => $page,
'total' => $total,
'list' => $list,
'noread_num' => $noread_num
];
$this->success('', $data);
}
$condtion = [];
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$type = $this->request->param('type');
$time = $this->request->param('time');
if ($time) {
$createtime = strtotime($time);
$createtimeend = strtotime($time . ' 23:59:59');
$condtion['message.createtime'] = ['between', [$createtime, $createtimeend]];
}
if (in_array($type, ['1', '2'])) {
$condtion['message.type'] = ['=', $type];
}
$condtion['log.user_id'] = ['=', $this->auth->id];
$total = $this->model
->alias('message')
->join('inspection_message_log log', 'log.insepection_message_id=message.id', 'LEFT')
->where($condtion)
->count();
$list = $this->model
->alias('message')
->join('inspection_message_log log', 'log.insepection_message_id=message.id', 'LEFT')
->field('message.id,message.type,message.title,from_unixtime(message.createtime,"%Y-%m-%d %H:%i:%s") as crts,log.status')
->where($condtion)
->order('message.createtime desc')
->page($page, $limit)
->select();
$noread = Db::name('inspection_message_log')->where('user_id', $this->auth->id)->where('status', 0)->count();
$data = [
'page' => $page,
'total' => $total,
'list' => $list,
'noread' => $noread
];
$this->success('', $data);
}
public function details($ids = null)
{
$row = Db::name('inspection_message')->where(['id' => $ids])->find();
if (!$row) {
$this->error('信息不存在');
}
$ids = $row['user_ids'];
$names = Db::name("inspection_staff")->where(["user_id" => ["in", $ids]])->column("staff_name");
$row['unames'] = implode($names, ",");
$row['weather'] = $row['weather'] ? json_decode($row['weather'], true) : [];
$row['createtime'] = date("Y-m-d H:i:s", $row['createtime']);
//图片和文件 地址处理
if ($row['images']) {
$picarr = explode(',', $row['images']);
//var_dump($picarr);
$row['images'] = $this->setQiniuFileUrl($picarr);
}
if ($row['filenames']) {
$row['fileoldnameArr'] = explode(',', $row['filenames']);
} else {
$row['fileoldnameArr'] = [];
}
if ($row['fileurl']) {
$farr = explode(',', $row['fileurl']);
$row['fileurl'] = $this->setQiniuFileUrl($farr);
foreach ($farr as $kk => $vv) {
$file_data[$kk]["name"] = $row['fileoldnameArr'][$kk];
$file_data[$kk]["url"] = full_image($vv);
$file_data[$kk]["type"] = explode(".", $vv)[1];
}
}
$row['file_data'] = $file_data;
//已读状态追加
if ($row['user_ids'] && empty($row['reservoir_ids'])) {
$w = [];
$w["l.user_id"] = ["IN", $row['user_ids']];
$w["l.insepection_message_id"] = ["=", $row['id']];
$row['staff_read_status'] =
Db::name("inspection_message_log")->alias("l")
->join("inspection_staff s", "s.user_id=l.user_id", "LEFT")
->where($w)
->where('l.user_id>0')
->field("l.user_id,l.status,s.staff_name")
->select();
}
$this->success('获取成功', $row);
}
public function confirm($ids = null)
{
$row = $this->modellog->get(['user_id' => $this->auth->id, 'insepection_message_id' => $ids]);
if (!$row) {
$this->error('通知不存在');
} else if ($row->status == '1') {
$this->error('通知是已读状态');
}
$row->status = '1';
$res = $row->save();
if (!$res) {
$this->error('网络繁忙');
}
$this->success('操作成功');
}
/***
* 通知公告发布
* add
*/
public function messageAdd()
{
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data['title'])) {
$this->error('请填写标题内容');
}
if (empty($data['type'])) {
$this->error("请选择分类");
}
if ($data['type'] == 1) {
// if (empty($data['user_ids'])) {
// $this->error("请选择指派人");
// }
unset($data['reservoir_ids']);
}
if ($data['type'] == 2) {
// if (empty($data['reservoir_ids'])) {
// $this->error("请选择水库");
// }
unset($data['user_ids']);
}
if (empty($data['content'])) {
$this->error('请填写详情内容');
}
$data['createtime'] = time();
$data['images'] = implode(',', $data['imagearr']);
$data['fileurl'] = implode(',', $data['filearr']);
$data['filenames'] = implode(",", $data['fileoldnameArr']);
unset($data['imagearr']);
unset($data['filearr']);
unset($data['fileoldnameArr']);
$data['add_uid'] = $this->auth->id;
$data['user_ids'] = $data['user_ids'] ? $data['user_ids'] : $this->auth->id;//$this->auth->id;
if (!empty($data['weather'])) {
$data['weather'] = json_encode($data['weather'], JSON_UNESCAPED_UNICODE);
}
$rs = Db::name("inspection_message")->insertGetId($data);
if ($rs) {
//追加未读记录
if (!empty($data['user_ids'])) {
$ids = explode(",", $data['user_ids']);
if (!empty($ids)) {
foreach ($ids as $k => $v) {
$log = [];
$log = [
"user_id" => $v,
"insepection_message_id" => $rs,
"status" => 0
];
Db::name("inspection_message_log")->insert($log);
}
}
}
//unipiush推送
// $jgPush = new JgPush();
// $res = $jgPush->pushMessage($rs);
// if ($res['state'] == true) {
// $msg = "【计划推送人数:" . $res['data']['sum_count'] . ";实际推送人数:" . $res['data']['react_count'] . "】";
// } else {
// $msg = "[" . $res['msg'] . "]";
// }
$this->success('发布成功', $rs);
} else {
$this->error('发布失败!');
}
}
}
/**
* 更新通知公告阅读状态
*
*/
public function isRead()
{
if ($this->request->isPost()) {
$ids = $this->request->post('ids', 0);
$user_id = $this->auth->id;
$info = $this->model::get($ids);
if (empty($info)) {
$this->error('信息不存在');
}
$res = Db::name("inspection_message_log")->where(["user_id" => $user_id, "insepection_message_id" => $ids])->find();
if (empty($res)) {
Db::name("inspection_message_log")->insert(["user_id" => $user_id, "insepection_message_id" => $ids, "status" => 1]);
} else {
Db::name("inspection_message_log")->where(["user_id" => $user_id, "insepection_message_id" => $ids])->update(["status" => 1]);
}
$this->success('更新成功');
}
}
/**
* 处理七牛一的文件地址:图片、视频、文件
* $data 可以传入一维数组
* 文件地址前添加域名地址
*/
private function setQiniuFileUrl($data)
{
$domen = 'https://qiniu.ynzhsk.cn';
if (is_array($data)) {
$arr = array();
foreach ($data as $v) {
if ($v) {
array_push($arr, $domen . $v);
}
}
return $arr;
} else {
return $domen . $data;
}
}
/**
* 36、我发布的公告列表
*/
public function my_post_gonggao_list()
{
$condtion = [];
$page = $this->request->param('page', 1);
$limit = $this->request->param('total', 15);
$issend = $this->request->param('issend', 1);
$uid = $this->auth->id;
$condtion['message.add_uid'] = ['=', $this->auth->id];
$condtion['message.type'] = ['=', 2];//公告
// if(!empty($uid)){
// $condtion['log.user_id'] = ['=',$uid];
// }
$total = $this->model
->alias('message')
->join('inspection_message_log log', 'log.insepection_message_id=message.id', 'LEFT')
->field('message.id')
->where($condtion)
->order('message.id desc')
->distinct(true)
->group('message.id')
->select();
$list = $this->model
->alias('message')
->join('inspection_message_log log', 'log.insepection_message_id=message.id', 'LEFT')
->field('message.id,message.type,message.title,message.content,from_unixtime(message.createtime,"%Y-%m-%d %H:%i") as crts,log.status as `status`')
->where($condtion)
->order('message.id desc')
->distinct(true)
->group('message.id')
->page($page, $limit)
->select();
$total = count($total);
$data = [
'page' => $page,
'total' => $total,
'list' => $list
];
$this->success('', $data);
}
}