<?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') ->order('c.id desc') ->select(); foreach($data as &$value){ $value['createtime'] = date('Y-m-d',$value['createtime']); } $this->success('成功',$data); }else{ $this->error('请求方式错误'); } } }