Evaluate.php 5.8 KB
<?php

namespace app\api\controller;

use app\api\model\OrderDetail;
use app\api\model\OrderInfo;
use app\common\controller\Api;
use function GuzzleHttp\Psr7\uri_for;
use think\Db;
use think\Validate;
use think\Config;

/**
 * 评价接口
 */
class Evaluate extends Api
{
    protected $evaluateModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->evaluateModel = new \app\api\model\Evaluate;
    }


    /**
     * @ApiTitle    (获取商品评价)
     * @ApiSummary  (获取商品评价)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/evaluate/getGoodsEvaluate)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=goods_id, type=string, required=true, description="商品id")
     * @ApiParams  (name=page, type=string, required=false, description="页数")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587472510",
    "data": {
    "total": 1,
    "list": [
    {
    "content": "content",    评论内容
    "images": [
    "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png"   评论图片
    ],
    "createtime": "2020-04-20",  评论日期
    "avatar": "avatar",  用户头像
    "nickname": "admin"  用户昵称
    }
    ]
    }
    })
     */
    public function getGoodsEvaluate(){
        $goods_id = $this->request->param('goods_id');
        if (!$goods_id) $this->error('缺少参数 goods_id!');

        $page = $this->request->param('page');
        $limit = Config::get('paginate.index_rows');
        $where['e.goods_id'] = $goods_id;
        $data = $this->evaluateModel->selectPageData($where,$page,$limit);
        $this->success('SUCCESS',$data);
    }


    /**
     * @ApiTitle    (添加商品评价)
     * @ApiSummary  (添加商品评价)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/evaluate/addGoodsEvaluate)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=goods_id, type=string, required=true, description="商品id")
     * @ApiParams  (name=images, type=string, required=false, description="图片 例1.jpg,2.jpg")
     * @ApiParams  (name=content, type=string, required=true, description="评价内容")
     * @ApiParams  (name=type, type=string, required=true, description="类型:1=普通商品,2=积分商品")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587472510",
    "data": {}
    })
     */
    public function addGoodsEvaluate(){
        $userId = $this->getUserId();
        $data = $this->request->param();
        $data['user_id'] = $userId;
        $validate = new Validate([
            'goods_id' => 'require',
            'content' => 'require',
            'type' => 'require',
        ]);

        $validate->message([
            'goods_id' => '缺少参数 goods_id!',
            'content' => '缺少参数 content!',
            'type' => '缺少参数 type!',
        ]);

        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $data = $this->evaluateModel->save($data);
        $this->success('SUCCESS');
    }

    /**
     * @ApiTitle    (添加商品评价(批量))
     * @ApiSummary  (添加商品评价(批量))
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/evaluate/addAllGoodsEvaluate)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=data, type=string, required=true, description="[['goods_id=>1,'content'=>1,'images'=>1,'type'=>1],['goods_id=>1,'content'=>1,'images'=>1,'type'=>1]]")

     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587472510",
    "data":
    })
     */
    public function addAllGoodsEvaluate(){
        $userId = $this->getUserId();
        $data = $this->request->param();
        if (empty($data)) $this->error('缺少参数 data!');
        $validate = new Validate([
            'goods_id' => 'require',
            'content' => 'require',
            'type' => 'require',
        ]);

        $validate->message([
            'goods_id' => '缺少参数 goods_id!',
            'content' => '缺少参数 content!',
            'type' => '缺少参数 type!',
        ]);
        foreach ($data as $k=>$v){
            if (!$validate->check($v)) {
                $this->error($validate->getError());
            }
            $data[$k]['user_id'] = $userId;
        }
        $res = $this->evaluateModel->saveAll($data);
        if ($res) $this->success('SUCCESS');
        else $this->success('ERROR');
    }

    /**
     * @ApiTitle    (商品评价页)
     * @ApiSummary  (商品评价页)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/evaluate/goodsEvaluate)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=order_detail_id, type=string, required=true, description="订单子表id")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587472510",
    "data": {
    {
    "id": 3,
    "goods_name": "Japanese black cattle",   商品名称
    "type": "1"      类型:1=普通商品,2=积分商品
    },
    {
    "id": 4,
    "goods_name": "EMP ",
    "type": "2"
    }
    }
    })
     */
    public function goodsEvaluate(){
        $orderInfoModel = new OrderInfo();
        $userId = $this->getUserId();
        $order_detail_id = $this->request->param('order_detail_id');
        if (empty($order_detail_id)) $this->error('缺少参数 order_detail_id!');
        if ($this->lang == 'ch') $field = 'id,ch_goods_name goods_name,type';
        else $field = 'id,en_goods_name goods_name,type';
        $data = $orderInfoModel->where('order_detail_id',$order_detail_id)->field($field)->select();
        $this->success('SUCCESS',$data);
    }
}