Normal.php
4.4 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
<?php
namespace app\api\controller\v7\notices;
use app\admin\model\Admin;
use app\admin\model\notices\Article;
use app\admin\model\notices\Noticesnormal;
use app\admin\model\User;
use app\common\controller\Api;
/**
* 通知相关接口
* Class Normal
* @package app\api\controller\v7\notices
*/
class Normal extends Api
{
// protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
}
//获取通知消息
public function getNotices(){
$params = $this->request->get();
$page = $params['page'] ?? 1;
$limit = $params['limit'] ?? 10;
$where = [
'rec_type' => 1,
'rec_id' => $this->auth->id
];
if (isset($params['notice_type'])){
$where['notice_type'] = $params['notice_type'];
}
$noticesnormalModel = new Noticesnormal();
$total = $noticesnormalModel->where($where)->count();
$notices = $noticesnormalModel
->where($where)
->order(['createtime' => 'desc'])
->page($page, $limit)
->select();
foreach ($notices as $notice){
//查询接收者信息
$userInfo = $this->auth->getUser();
$notice['rec_info'] = [
'id' => $userInfo['id'],
'username' => $userInfo['username'],
'nickname' => $userInfo['nickname'],
'email' => $userInfo['email'],
'avatar' => $userInfo['avatar']
];
//查询发送者信息
if ($notice['send_type'] == 1){
$sendInfo = User::get($notice['send_id']);
}elseif($notice['send_type'] == 2){
$sendInfo = Admin::get($notice['send_id']);
}
$notice['send_info'] = [
'id' => $sendInfo['id'],
'username' => $sendInfo['username'],
'nickname' => $sendInfo['nickname'],
'email' => $sendInfo['email'],
'avatar' => $sendInfo['avatar']
];
}
$result = [
'page' => $page,
'limit' => $limit,
'total' => $total,
'rows' => $notices,
];
$this->success('成功', $result);
}
//获取公告详情
public function noticeDetail(){
$params = $this->request->get();
if (empty($params['id'])){
$this->error('公告id必须');
}
$articleModel = new Article();
$article = $articleModel
->with('depart')
->where(['article.id' => $params['id']])
->find();
if (!$article){
$this->error('公告不存在');
}
$this->success('成功', $article);
}
//获取公告列表
public function getArticle(){
$params = $this->request->get();
$page = $params['page'] ?? 1;
$limit = $params['limit'] ?? 10;
$articleModel = new Article();
$total = $articleModel->count();
$list = $articleModel
->with('depart')
->order(['article.id' => 'desc'])
->page($page, $limit)
->select();
$result = [
'page' => $page,
'limit' => $limit,
'total' => $total,
'rows' => $list,
];
$this->success('成功', $result);
}
//未读通知公告统计
public function unreadNum(){
$params = $this->request->get();
$where = [
'read' => 0,
'rec_type' => 1,
'rec_id' => $this->auth->id
];
if (isset($params['notice_type'])){
$where['notice_type'] = $params['notice_type'];
}
$noticesnormalModel = new Noticesnormal();
$total = $noticesnormalModel->where($where)->count();
$this->success('成功', ['unread_num' => $total]);
}
//设置消息为已读
public function read(){
$params = $this->request->post();
if (empty($params['id'])){
$this->error('通知id必须');
}
$notice = Noticesnormal::get($params['id']);
if (!$notice){
$this->error('通知不存在');
}
if ($notice['rec_type'] != 1 || $notice['rec_id'] != $this->auth->id){
$this->error('无操作权限');
}
$notice['read'] = 1;
$notice->save();
$this->success('操作成功');
}
}