GoodsComment.php 5.1 KB
<?php

namespace app\api\controller;

/**
 * 商品评价接口
 */
class GoodsComment extends Base
{

    protected $noNeedLogin = ['index', 'type'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiWeigh    (99)
     * @ApiTitle    (评价列表)
     * @ApiSummary  (评价列表)
     * @ApiMethod   (GET)
     *
     * @ApiParams (name=goods_id, type=inter, required=true, description="商品ID")
     * @ApiParams (name=type, type=string, required=false, description="评价类型:all=全部,images=有图,good=好评,moderate=中评,bad=差评")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "评价列表",
        "time": "1609221855",
        "data": {
            "total": 1, //数据总数
            "per_page": 10,
            "current_page": 1,
            "last_page": 1,
            "data": [{
                "id": 1,
                "goods_id": 6,
                "order_id": 17,
                "order_item_id": 19,
                "user_id": 1,
                "level": 5,
                "content": "还行吧", //评价内容
                "images": [ //评价图片
                    "https://yixiaoxian.qiniu.broing.cn/uploads/20201229/5c088559ebcc3f0ffcda663f04dfbeb2.png"
                ],
                "video": null, //评价视频
                "status": "show",
                "admin_id": 0,
                "reply_content": null,
                "replytime": null,
                "createtime": "2020-12-29", //评价时间
                "updatetime": 1609221767,
                "deletetime": null,
                "user": { //评价用户
                    "id": 1, //用户ID
                    "nickname": "wn", //昵称
                    "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/C8dW9GFDHAy3wwnZwoqibeNciaN6jUZXp6QCrtjehdF3GyHickt9oiaDSibMBhATtF7f19w4AgpcQIR1Mibwu1pjYKEA/132", //头像
                    "url": "/u/1"
                }
            }]
            }
    })
     */
    public function index()
    {
        $params = $this->request->get();
        
        $goodsComments = \addons\shopro\model\GoodsComment::getList($params);
        
        $this->success('评价列表', $goodsComments);
    }

    /**
     * @ApiWeigh    (97)
     * @ApiTitle    (筛选类型)
     * @ApiSummary  (筛选类型)
     * @ApiMethod   (GET)
     *
     * @ApiParams (name=goods_id, type=inter, required=true, description="商品ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "领取成功",
        "time": "1607911049",
        "data": {
            "user_id": 1, //用户ID
            "coupons_id": "2", //优惠券ID
            "createtime": 1607911050, //领取时间戳
            "id": "1" //领取ID
        }
    })
     */
    public function type() {
        $goods_id = $this->request->get('goods_id', 0);

        $type = array_values(\addons\shopro\model\GoodsComment::$typeAll);

        foreach ($type as $key => $val) {
            // 只查询 count 比查出来所有评论,在判断状态要快
            $comment = \addons\shopro\model\GoodsComment::where('goods_id', $goods_id);
            if ($val['code'] != 'all') {
                $comment = $comment->{$val['code']}();
            }
            $comment = $comment->count();
            $type[$key]['num'] = $comment;
        }

        $this->success('筛选类型', $type);
    }

    /**
     * @ApiWeigh    (95)
     * @ApiTitle    (我的评价)
     * @ApiSummary  (我的评价)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "我的评价",
        "time": "1609221855",
        "data": {
            "total": 1, //数据总数
            "per_page": 10,
            "current_page": 1,
            "last_page": 1,
            "data": [{
                "id": 1,
                "goods_id": 6,
                "order_id": 17,
                "order_item_id": 19,
                "user_id": 1,
                "level": 5,
                "content": "还行吧", //评价内容
                "images": [ //评价图片
                    "https://yixiaoxian.qiniu.broing.cn/uploads/20201229/5c088559ebcc3f0ffcda663f04dfbeb2.png"
                ],
                "video": null, //评价视频
                "status": "show",
                "admin_id": 0,
                "reply_content": null,
                "replytime": null,
                "createtime": "2020-12-29", //评价时间
                "updatetime": 1609221767,
                "deletetime": null,
                "user": { //评价用户
                    "id": 1, //用户ID
                    "nickname": "wn", //昵称
                    "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/C8dW9GFDHAy3wwnZwoqibeNciaN6jUZXp6QCrtjehdF3GyHickt9oiaDSibMBhATtF7f19w4AgpcQIR1Mibwu1pjYKEA/132", //头像
                    "url": "/u/1"
                }
            }]
        }
    })
     */
    public function myComment()
    {
        $params = $this->request->get();

        $goodsComments = \addons\shopro\model\GoodsComment::getMyList($params);

        $this->success('我的评价', $goodsComments);
    }
}