Messages.php
3.9 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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
* 系统消息接口
*/
class Messages extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $user_id = '';//token存贮user_id
protected $read = 1;//是否已读 1:已读
public function _initialize()
{
parent::_initialize();
$this->user_id = $this->auth->getUserId();
}
/**
* @ApiTitle (系统消息列表)
* @ApiSummary (系统消息列表)
* @ApiMethod (GET)
* @ApiRoute (/api/messages/getMessageList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="integer", required=true, description="分页页码")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553777266",
"data":{
"id": 1,
"title": "系统消息",//系统消息标题
"is_read": 0 //系统消息是否已读 0:未读,1:已读
},
{
"id": 5,
"title": "重大好消息1",
"is_read": 0
}
})
*/
public function getMessageList(){
if($this->request->isGet()){
$page = $this->request->get('page');//分页页码
$limit = config('site.page_limit');//分页限制数量
$rule = config('site.gift_pages');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['page'=>$page])) {
$this->error($validate->getError());
}
$data = Db::table('gc_message')
->alias('l')
->join('gc_message m','l.message_id = m.id','LEFT')
->where(['l.user_id'=>$this->user_id])
->page($page,$limit)
->field('m.id,m.title,l.is_read')
->select();
$this->success('成功',$data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (系统消息详情)
* @ApiSummary (系统消息详情)
* @ApiMethod (GET)
* @ApiRoute (/api/messages/messageDetail)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="integer", required=true, description="分页页码")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553777266",
"data":{
"id": 5,
"title": "重大好消息1",//系统消息标题
"description": "系统消息系统消息详情“,
"createtime": "2019-03-20 16:24:48"
}
})
*/
public function messageDetail(){
if($this->request->isGet()){
$message_id = $this->request->get('id');//商品id
$rule = config('site.goods');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$message_id])) {
$this->error($validate->getError());
}
$data = Db::table('gc_message_log')
->alias('l')
->join('gc_message m','l.message_id = m.id','LEFT')
->where(['l.user_id'=>$this->user_id,'m.id'=>$message_id])
->field('m.id,m.title,m.description,l.createtime')
->select();
foreach($data as &$value){
$value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
}
Db::table('gc_message_log')
->where(['message_id'=>$message_id,'user_id'=>$this->user_id])
->update(['is_read'=>$this->read,'readtime'=>time()]);
$this->success('成功',$data);
}else{
$this->error('请求方式错误');
}
}
}