Coupons.php
14.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
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 think\Validate;
/**
* 优惠券接口
*/
class Coupons extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $uid = '';
public function _initialize()
{
parent::_initialize();
$this->uid = $this->auth->getUserId();
}
/**
* @ApiTitle (领券中心)
* @ApiSummary (领券中心)
* @ApiMethod (GET)
* @ApiRoute (/api/coupons/couponCenterList)
*
* @ApiParams (name="page", type="inter", required=true, description="分页页码")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575365883",
"data": {
"data": [
{
"id": 2,//优惠券id
"coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
"coupon_price": "¥300",//(折扣或减少金额)
"coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
"is_receive": 0,//是否领取(0:否,1:是)
"coupon_name": "全场优惠券",//优惠券名称
"coupon_number": 100,//优惠券剩余数量(0:领光了)
"type": "全场通用"//优惠券用途
},
{
"id": 11,
"coupon_tag": "无门槛",
"coupon_price": "¥100",
"coupon_tag1": "无门槛",
"is_receive": 0,
"coupon_name": "品牌箱包优惠券",
"coupon_number": 100,
"type": "部分品牌可用"
},
],
"total_page": 1//总页码
}
})
*/
public function couponCenterList(){
if($this->request->isGet()){
$page = $this->request->get('page');
$rule = config('verify.page');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['page'=>$page])) {
$this->error($validate->getError());
}
$flag = config('verify.flag');
$current_time = time();
$where = ['is_new'=>$flag[0],'start_time'=>['<',$current_time],'end_time'=>['>',$current_time]];
//查询已经领取过
$receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid],'id,c_id');
$receive_s = array_column($receive,'c_id');
$limit = config('verify.goods_limit');
$data = Common::selectWherePageData('coupon',$where,'id,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number',$page,$limit,'sort desc,id desc');
$res = [];
$arr = [];
foreach($data as $key=>$value){
$res[$key]['id'] = $value['id'];
//无门槛,有门槛
if($value['c_type'] == 0){
//无门槛
if($value['coupon_type'] == 0){
//减少券,去掉满减金额,折扣字段,
unset($value['full_reduce']);
unset($value['discount']);
$res[$key]['coupon_tag'] = '无门槛';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
$res[$key]['coupon_tag1'] = '无门槛';
}else{
//折扣券,去掉满减金额,减少金额字段,
unset($value['full_reduce']);
unset($value['reduce']);
$res[$key]['coupon_tag'] = '折扣券';
$res[$key]['coupon_price'] = $value['discount'].'折';
$res[$key]['coupon_tag1'] = '无门槛';
}
}else{
//有门槛
if($value['coupon_type'] == 0){
//减少券,去掉折扣字段,
unset($value['discount']);
$res[$key]['coupon_tag'] = '满减券';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
$res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
}else{
//折扣券,去掉减少金额字段,
unset($value['reduce']);
$res[$key]['coupon_tag'] = '折扣券';
$res[$key]['coupon_price'] = $value['discount'].'折';
$res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
}
}
$res[$key]['is_receive'] = 0;//未领取
if(in_array($value['id'],$receive_s)){
$res[$key]['is_receive'] = 1;//已领取
}
$res[$key]['coupon_name'] = $value['coupon_name'];//优惠券名称
$res[$key]['coupon_number'] = $value['coupon_number'];//优惠券剩余数量
//全场,品牌,商品
if($value['type'] == 0){
$res[$key]['type'] = '全场通用';
}else if($value['type'] == 1){
$res[$key]['type'] = '部分品牌可用';
}else{
$res[$key]['type'] = '部分商品可用';
}
}
$arr['data'] = $res;
//总页数
$arr['total_page'] = Common::count('coupon',$where,$limit);
$this->success('成功',$arr);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (品牌优惠券)
* @ApiSummary (品牌优惠券)
* @ApiMethod (GET)
* @ApiRoute (/api/coupons/brandCouponList)
*
* @ApiParams (name="b_id", type="inter", required=true, description="品牌分类id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575367045",
"data": [
{
"id": 5,优惠券id
"coupon_number": 100,//优惠券剩余数量(0:显示领光了)
"is_receive": 0,//是否领取(0:否,1:是)
"coupon_tag": "无门槛",//优惠券(无门槛或满减金额)
"coupon_price": "¥100"//(折扣或减少金额)
},
{
"id": 11,
"coupon_number": 100,
"is_receive": 0,
"coupon_tag": "无门槛",
"coupon_price": "¥100"
}
]
})
*/
public function brandCouponList(){
if($this->request->isGet()){
$b_id = $this->request->get('b_id');
$rule = config('verify.brand_detail');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['b_id'=>$b_id])) {
$this->error($validate->getError());
}
$flag = config('verify.flag');
$current_time = time();
$where = ['is_new'=>$flag[0],'type'=>$flag[1],'bg_id'=>$b_id,'start_time'=>['<',$current_time],'end_time'=>['>',$current_time]];
//查询已经领取过
$receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid],'id,c_id');
$receive_s = array_column($receive,'c_id');
$data = Common::selectWhereData('coupon',$where,'id,c_type,coupon_type,full_reduce,reduce,discount,coupon_number','sort desc,id desc');
$res = [];
foreach($data as $key=>$value){
$res[$key]['id'] = $value['id'];
$res[$key]['coupon_number'] = $value['coupon_number'];
$res[$key]['is_receive'] = 0;//未领取
if(in_array($value['id'],$receive_s)){
$res[$key]['is_receive'] = 1;//已领取
}
//无门槛,有门槛
if($value['c_type'] == 0){
//无门槛
if($value['coupon_type'] == 0){
//减少券,去掉满减金额,折扣字段,
unset($value['full_reduce']);
unset($value['discount']);
$res[$key]['coupon_tag'] = '无门槛';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
}else{
//折扣券,去掉满减金额,减少金额字段,
unset($value['full_reduce']);
unset($value['reduce']);
$res[$key]['coupon_tag'] = '无门槛';
$res[$key]['coupon_price'] = $value['discount'].'折';
}
}else{
//有门槛
if($value['coupon_type'] == 0){
//减少券,去掉折扣字段,
unset($value['discount']);
$res[$key]['coupon_tag'] = '满'.$value['full_reduce'].'可用';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
}else{
//折扣券,去掉减少金额字段,
unset($value['reduce']);
$res[$key]['coupon_tag'] = '满'.$value['full_reduce'].'可用';
$res[$key]['coupon_price'] = $value['discount'].'折';
}
}
}
$this->success('成功',$res);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (商品优惠券)
* @ApiSummary (商品优惠券)
* @ApiMethod (GET)
* @ApiRoute (/api/coupons/goodsCouponList)
*
* @ApiParams (name="goods_id", type="inter", required=true, description="商品id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575372162",
"data": [
{
"id": 2,//优惠券id
"coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
"coupon_price": "¥300",//(折扣或减少金额)
"coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
"is_receive": 0,//是否领取(0:否,1:是)
"coupon_name": "全场优惠券",//优惠券名称
"coupon_number": 100,//优惠券剩余数量(0:领光了)
"type": "全场通用"//优惠券用途
},
{
"id": 2,//优惠券id
"coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
"coupon_price": "¥300",//(折扣或减少金额)
"coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
"is_receive": 0,//是否领取(0:否,1:是)
"coupon_name": "全场优惠券",//优惠券名称
"coupon_number": 100,//优惠券剩余数量(0:领光了)
"type": "全场通用"//优惠券用途
}
]
})
*/
public function goodsCouponList(){
if($this->request->isGet()){
$goods_id = $this->request->get('goods_id');
$rule = config('verify.goods_coupon');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['goods_id'=>$goods_id])) {
$this->error($validate->getError());
}
$type = config('verify.type');
$current_time = time();
$where = ['type'=>$type[2],'bg_id'=>$goods_id,'start_time'=>['<',$current_time],'end_time'=>['>',$current_time]];
//查询已经领取过
$receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid],'id,c_id');
$receive_s = array_column($receive,'c_id');
$data = Common::selectWhereData('coupon',$where,'id,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number','sort desc,id desc');
$res = [];
foreach($data as $key=>$value){
$res[$key]['id'] = $value['id'];
//无门槛,有门槛
if($value['c_type'] == 0){
//无门槛
if($value['coupon_type'] == 0){
//减少券,去掉满减金额,折扣字段,
unset($value['full_reduce']);
unset($value['discount']);
$res[$key]['coupon_tag'] = '无门槛';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
$res[$key]['coupon_tag1'] = '无门槛';
}else{
//折扣券,去掉满减金额,减少金额字段,
unset($value['full_reduce']);
unset($value['reduce']);
$res[$key]['coupon_tag'] = '折扣券';
$res[$key]['coupon_price'] = $value['discount'].'折';
$res[$key]['coupon_tag1'] = '无门槛';
}
}else{
//有门槛
if($value['coupon_type'] == 0){
//减少券,去掉折扣字段,
unset($value['discount']);
$res[$key]['coupon_tag'] = '满减券';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
$res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
}else{
//折扣券,去掉减少金额字段,
unset($value['reduce']);
$res[$key]['coupon_tag'] = '折扣券';
$res[$key]['coupon_price'] = $value['discount'].'折';
$res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
}
}
$res[$key]['is_receive'] = 0;//未领取
if(in_array($value['id'],$receive_s)){
$res[$key]['is_receive'] = 1;//已领取
}
$res[$key]['coupon_name'] = $value['coupon_name'];//优惠券名称
$res[$key]['coupon_number'] = $value['coupon_number'];//优惠券剩余数量
//全场,品牌,商品
$res[$key]['type'] = '本商品可用';
}
$this->success('成功',$res);
}else{
$this->error('请求方式错误');
}
}
}