Goods.php 4.6 KB
<?php

namespace app\api\controller;

use app\api\model\goods\GoodsSku;
use app\api\validate\IndexValidate;
use app\common\controller\Api;
use function Couchbase\basicDecoderV1;

/**
 * 产品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 产品列表
     * @ApiTitle (产品列表)
     * @ApiParams   (name="service_type", type="string", description="服务类型:1=报修服务,2=全损保值服务(字段值可为空,字段逼必传)")
     * @ApiParams   (name="year", type="int", description="时间(字段值可为空,字段逼必传)")
     * @ApiParams   (name="title", type="int", description="查询关键字(字段值可为空,字段逼必传)")
     * @ApiReturnParams   (name="id", type="string", description="产品id")
     * @ApiReturnParams   (name="title", type="string", description="产品标题")
     * @ApiReturnParams   (name="brief_introduction", type="string", description="产品简介")
     * @ApiReturnParams   (name="price", type="string", description="产品最低价格")
     */
    public function goods_list()
    {
        $params       = $this->request->param();
        $service_type = $params['service_type'];
        $year         = $params['year'];
        $title        = $params['title'];
        if (!empty($service_type)) {
            $where['service_type'] = ['=', $service_type];
        }
        if (!empty($year)) {
            $where['service_period'] = ['=', $year];
        }
        if (!empty($title)) {
            $where['title'] = ['like', '%' . $title . '%'];
        }
        $where['goods_status'] = ['=', '10'];
        $goods_list            = (new \app\api\model\goods\Goods())
            ->field('id,title,brief_introduction')
            ->where($where)
            ->order('weigh', 'desc')
            ->paginate()
            ->each(function (&$item) {
                $item['price'] = (new GoodsSku())->where('goods_id', $item['id'])
                    ->order('price', 'asc')->value('price');
            });
        $this->success('产品列表', $goods_list);
    }

    /**
     * 产品详情
     * @ApiTitle (产品详情)
     * @ApiParams   (name="id", type="string", description="产品id")
     * @ApiReturnParams   (name="id", type="string", description="产品id")
     * @ApiReturnParams   (name="title", type="string", description="产品标题")
     * @ApiReturnParams   (name="brief_introduction", type="string", description="产品简介")
     * @ApiReturnParams   (name="price", type="string", description="产品最低价格")
     * @ApiReturnParams   (name="image", type="string", description="产品图")
     * @ApiReturnParams   (name="content", type="string", description="富文本详情")
     *
     * @ApiReturnParams   (name="sku", type="array", description="产品sku")
     * @ApiReturnParams   (name="sku.id", type="int", description="skuID")
     * @ApiReturnParams   (name="sku.minimum", type="int", description="最低价")
     * @ApiReturnParams   (name="sku.max", type="int", description="最高价")
     * @ApiReturnParams   (name="sku.price", type="int", description="规格价格")
     * @ApiReturnParams   (name="sku.sku_text", type="int", description="规格价格")
     */
    public function goods_detail()
    {
        $id = $this->request->param('id', '', 'intval');
        if (empty($id) || $id <= 0) {
            $this->error('请确认产品ID');
        }
        $model = new \app\api\model\goods\Goods();
        $res   = $model->with(['sku'])
            ->field('id,title,image,brief_introduction,content')
            ->where('id', $id)
            ->where('goods_status', '10')
            ->find();
        $sku   = $res['sku'];
        foreach ($sku as &$value) {
            $value['sku_text'] = $value['level'] . $value['minimum'] . '-' . $value['max'] . '万元';
        }
        $res['price'] = (new GoodsSku())->where('goods_id', $res['id'])
            ->order('price', 'asc')->value('price');
        if ($res) {
            $this->success('ok', $res);
        }
        $this->error('您查找的产品不存在或已下架');

    }

    /**
     * 筛选年限数据
     * @ApiTitle (筛选年限数据)
     * @ApiReturnParams   (name="year", type="array", description="可筛选年限")
     */
    public function year()
    {
        $max_year = db('goods')->where('goods_status', 10)->max('service_period');
        $year_arr = [];
        for ($i = 1; $i <= intval($max_year); $i++) {
            $year[] = array_push($year_arr, $i);
        }
        $this->success('可选年限', ['year' => $year]);

    }
}