Goods.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
<?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]);
}
}