Comments.php
4.6 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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\Comment;
use app\admin\model\Porder;
use think\Db;
use think\Validate;
/**
* 评论接口**
*/
class Comments extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $user_id = '';//token存贮user_id
protected $order_status = [];//订单状态
public function _initialize()
{
parent::_initialize();
$this->order_status = config('site.order_status');
$this->user_id = $this->auth->getUserId();
}
/**
* @ApiTitle (已完成订单评价)
* @ApiSummary (已完成订单评价)
* @ApiMethod (POST)
* @ApiRoute (/api/comments/comment)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="商品id")
* @ApiParams (name="content", type="string", required=true, description="评价内容")
* @ApiReturn ({
"code": 1,
"msg": "评价成功",
"time": "1553837090",
"data": null
})
*/
public function comment(){
if($this->request->isPost()){
$goods_id = $this->request->post('id');
$order_id = $this->request->post('order_id');
$content = $this->request->post('content');
$rule = config('site.comment');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$goods_id,'order_id'=>$order_id,'content'=>trim($content,' ')])) {
$this->error($validate->getError());
}
Db::startTrans();
//评价
$comment = new Comment();
$res = $comment::create(['uid'=>$this->user_id,'p_id'=>$goods_id,'content'=>$content]);
//更新订单表状态为已完成
$p_order = new Porder();
$result = $p_order->where(['id'=>$order_id,'uid'=>$this->user_id,'status'=>$this->order_status[2]])
->update(['status'=>$this->order_status[3]]);
if($res && $result){
Db::commit();
$this->success('评价成功');
}else{
Db::rollback();
$this->error('评价失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (评价列表)
* @ApiSummary (评价列表)
* @ApiMethod (GET)
* @ApiRoute (/api/comments/commentList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="商品id")
* @ApiParams (name="page", type="integer", required=true, description="分页页码")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553777266",
"data":[
{
"id": 3,
"nickname": "风起时888",//昵称
"avatar": "分飞机的搜房888",//头像
"content": " ",//评论内容
"num": 0,//购买数量
"createtime": "2019-03-25"//评论时间
},
{
"id": 4,
"nickname": "风起时888",
"avatar": "分飞机的搜房888",
"content": " ",
"createtime": "2019-03-25"
},
]
})
*/
public function commentList(){
if($this->request->isGet()){
$goods_id = $this->request->get('id');//商品id
$page = $this->request->get('page');//分页页码
$limit = config('site.page_limit');//分页限制数量
$rule = config('site.pages');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$goods_id,'page'=>$page])) {
$this->error($validate->getError());
}
$data = Db::table('gc_comment')
->alias('c')
->join('gc_user u','c.uid = u.id','LEFT')
->where(['c.p_id'=>$goods_id,'u.status'=>'normal'])
->page($page,$limit)
->field('c.id,u.nickname,u.avatar,c.content,c.num,c.createtime')
->select();
foreach($data as &$value){
$value['createtime'] = date('Y-m-d',$value['createtime']);
}
$this->success('成功',$data);
}else{
$this->error('请求方式错误');
}
}
}