Index.php
13.8 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Coupon;
use app\common\model\Category;
use app\common\model\Goods;
use app\common\model\GoodsStyle;
use app\common\model\UserSize;
/**
* 首页接口
*/
class Index extends Api
{
protected $noNeedLogin = ['index','searchGoodsList','categoryList','goodsList','memberGoodsList','goodsInfo','appraiseList','styleList','specData'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
$this->user = $this->auth->getUser();
}
/**
* 首页
* @ApiMethod (GET)
* @ApiHeaders (name=token, type=string, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function index()
{
// 轮播图
$banner_list = Banner::banner_list();
$coupon_list = Coupon::couponList($this->user);
// 私人定制(一级分类)
$category_list = Category::where(['type'=>'goods','pid'=>0])->select();
foreach($category_list as $v){
$v->visible(['id','name','image']);
}
$category_list = collection($category_list)->toArray();
// 会员专区
$member_goods_list = Goods::getList(['ismember'=>'1'],1,2)['list'];
// 热卖爆品
$hot_goods_list = Goods::getList(['ishot'=>'1'],1,1000)['list'];
// 返回结果
$this->success(__('成功'),compact(
'banner_list',
'coupon_list',
'category_list',
'member_goods_list',
'hot_goods_list'
));
}
/**
* 领取优惠券
* @ApiMethod (GET)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="coupon_id", type="integer", required=true, description="优惠券ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function receiveCoupon($coupon_id)
{
$coupon = \app\common\model\Coupon::get($coupon_id);
empty($coupon) && $this->error(__('优惠券不存在'));
$coupon['expiretime'] < time() && $this->error(__('优惠券已到期'));
$data = [
'user_id' => $this->user['id'],
'coupon_id' => $coupon_id
];
$UserCoupon = new \app\common\model\UserCoupon;
$user_coupon = $UserCoupon->get($data);
!empty($user_coupon) && $this->error(__('已领取,请勿重复领取'));
$UserCoupon->save($data);
$this->success(__('领取成功'));
}
/**
* 搜索商品
* @ApiMethod (GET)
* @ApiParams (name="rows", type="integer", required=true, description="每页条数")
* @ApiParams (name="page", type="integer", required=true, description="页码")
* @ApiParams (name="keywords", type="string", required=true, description="关键字")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function searchGoodsList()
{
$rows = $this->request->request('rows',null);// 每页条数
$page = $this->request->request('page',1);// 页码
$keywords = $this->request->request('keywords','');
$list = Goods::getList(['goods_name'=>['like','%'.$keywords.'%']],$page,$rows);
$this->success(__('成功'),compact('list'));
}
/**
* 首页-一级分类-二级分类
* @ApiMethod (GET)
* @ApiParams (name="category_id", type="integer", required=true, description="分类ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function categoryList($category_id)
{
empty($category_id) && $this->error(__('参数不合法!'));
$list = Category::where('pid',$category_id)
->where('type','goods')
->select();
foreach ($list as $k => $v) {
$v->visible(['id','name']);
}
$list = collection($list)->toArray();
$this->success(__('成功'),compact('list'));
}
/**
* 二级分类下的商品
* @ApiMethod (GET)
* @ApiParams (name="rows", type="integer", description="每页条数")
* @ApiParams (name="page", type="integer", required=true, description="页码")
* @ApiParams (name="category_id", type="integer", required=true, description="分类ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function goodsList()
{
$rows = $this->request->request('rows',null);// 每页条数
$page = $this->request->request('page',1);// 页码
$category_id = $this->request->request('category_id');// 分类id
empty($category_id) && $this->error(__('参数不合法!'));
$three_category_id_arr = Category::where('pid',$category_id)->column('id');
$where = "FIND_IN_SET('{$category_id}', category_ids)";
if(!empty($three_category_id_arr)){
foreach($three_category_id_arr as $v){
$where .= " OR FIND_IN_SET('{$v}', category_ids)";
}
}
$list = Goods::getList($where,$page,$rows);
$this->success(__('成功'),compact('list'));
}
/**
* 会员专区(首页-会员专区-查看更多)
* @ApiMethod (GET)
* @ApiParams (name="rows", type="integer", description="每页条数")
* @ApiParams (name="page", type="integer", required=true, description="页码")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function memberGoodsList()
{
$rows = $this->request->request('rows',null);// 每页条数
$page = $this->request->request('page',1);// 页码
$list = Goods::getList(['ismember'=>'1'],$page,$rows);
// 返回结果
$this->success(__('成功'),compact('list'));
}
/**
* 热卖爆品(首页-热卖爆品-查看更多)
* @ApiMethod (GET)
* @ApiParams (name="rows", type="integer", description="每页条数")
* @ApiParams (name="page", type="integer", required=true, description="页码")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function hotGoodsList()
{
$rows = $this->request->request('rows',null);// 每页条数
$page = $this->request->request('page',1);// 页码
$list = Goods::getList(['ishot'=>'1'],$page,$rows);
// 返回结果
$this->success(__('成功'),compact('list'));
}
/**
* 商品详情
* @ApiMethod (GET)
* @ApiParams (name="goods_id", type="integer", required=true, description="商品ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function goodsInfo()
{
// 商品id
$goods_id = $this->request->request('goods_id');
$goods = Goods::get($goods_id);
empty($goods) && $this->error(__('商品不存在'));
$goods['issale'] == '0' && $this->error(__('商品已下架'));
$goods['goods_style'] = '';
$goods['size'] = [];
if($goods['ismake'] == '1'){
$goods_style = GoodsStyle::get(['goods_id'=>$goods_id]);
if($goods_style){
$goods['goods_price'] = $goods['goods_price'] + $goods_style['goods_style_price'];
$goods['goods_style'] = $goods_style['goods_style'];
}
$user_size = UserSize::get(['user_id'=>$this->user['id'],'isdefault'=>'1']);
if($user_size){
$goods['size'] = $user_size->visible(['id','name']);
}
}
$goods['appraise'] = \app\common\model\GoodsAppraise::getList(['goods_id'=>$goods_id],1,1);
$ismember = $this->user['mobile'] ? '1' : '0';
// 返回结果
$this->success(__('成功'),compact('goods','ismember'));
}
/**
* 全部评价
* @ApiMethod (GET)
* @ApiParams (name="rows", type="integer", description="每页条数")
* @ApiParams (name="page", type="integer", required=true, description="页码")
* @ApiParams (name="goods_id", type="integer", required=true, description="商品ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function appraiseList()
{
// 商品id
$rows = $this->request->request('rows',null);
$page = $this->request->request('page',1);
$goods_id = $this->request->request('goods_id');
$list = \app\common\model\GoodsAppraise::getList(['goods_id'=>$goods_id],$page,$rows);
// 返回结果
$this->success(__('成功'),compact('list'));
}
/**
* 定制风格
* @ApiMethod (GET)
* @ApiParams (name="goods_id", type="integer", required=true, description="商品ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function styleList()
{
$goods_id = $this->request->request('goods_id');
$goods = Goods::get($goods_id);
empty($goods) && $this->error(__('商品信息不存在'));
$goods_style = GoodsStyle::get(['goods_id'=>$goods_id]);
empty($goods_style) && $this->error(__('该商品还未设置模特款'));
$list = \app\common\model\Style::styleList($goods_style['style_template_id']);
$this->success(__('成功'),compact('list'));
}
/**
* 规格
* @ApiMethod (GET)
* @ApiParams (name="goods_id", type="integer", required=true, description="商品ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function specData()
{
$goods_id = $this->request->request('goods_id');
$goods = Goods::get($goods_id);
empty($goods) && $this->error(__('商品信息不存在'));
$spec_data = $goods['spec_type'] == '2' ? $goods->getManySpecData($goods['spec_rel'], $goods['spec']) : null;
$this->success(__('成功'),compact('spec_data'));
}
/**
* 规格-获取商品sku
* @ApiMethod (GET)
* @ApiParams (name="goods_id", type="integer", required=true, description="商品ID")
* @ApiParams (name="spec_sku_id", type="string", description="商品sku")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function getGoodsSku()
{
$goods_id = $this->request->request('goods_id');
$spec_sku_id = $this->request->request('spec_sku_id');
$goods = Goods::get($goods_id,['spec_rel.spec']);
empty($goods) && $this->error(__('商品信息不存在'));
empty($spec_sku_id) && $this->error(__('请选择规格'));
$this->success(__('成功'),$goods->getGoodsSku($spec_sku_id));
}
}