Gorders.php
10.2 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
<?php
namespace app\api\controller;
use app\admin\model\Gift;
use app\common\controller\Api;
use app\admin\model\Gorder;
use app\admin\model\User;
use think\Db;
use think\Validate;
/**
* 礼品订单接口**
*/
class Gorders extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $user_id = '';//token存贮user_id
protected $normal = '';//商品正常状态,1:下架
public function _initialize()
{
parent::_initialize();
$this->normal = config('site.goods_status');
$this->user_id = $this->auth->getUserId();
}
/**
* @ApiTitle (礼品兑换是否有足够的积分)
* @ApiSummary (礼品兑换是否有足够的积分)
* @ApiMethod (GET)
* @ApiRoute (/api/gorders/isEnough)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="g_id", type="integer", required=true, description="礼品id")
* @ApiParams (name="g_num", type="integer", required=true, description="礼品数量")
* @ApiReturn {
"code": 0,
"msg": "您的积分不够用哦",
"time": "1553837503",
"data": null
}
*/
public function isEnough(){
if($this->request->isGet()){
$g_id = $this->request->get('g_id');//礼品id,
$g_num = $this->request->get('g_num');//礼品数量
$rule = config('site.g_settle');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['g_id'=>$g_id,'g_num'=>$g_num])) {
$this->error($validate->getError());
}
$data = Db::table('gc_gift')
->where(['id'=>$g_id,'status'=>$this->normal])
->field('score')
->find();
//礼品总积分
$g_score = intval($data['score']*$g_num);
$user = Db::table('gc_user')
->where(['id'=>$this->user_id,'status'=>'normal'])
->field('score')
->find();
$u_score = intval($user['score']);
//判断登录用户积分是否够用
if($u_score < $g_score){
$this->error('您的积分不够用哦');
}
$this->success('成功');
}else{
$this->error('请求方式出错');
}
}
/**
* @ApiTitle (礼品兑换订单页)
* @ApiSummary (礼品兑换订单页)
* @ApiMethod (GET)
* @ApiRoute (/api/gorders/exchange)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="g_id", type="integer", required=true, description="礼品id")
* @ApiParams (name="g_num", type="integer", required=true, description="礼品数量")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553837670",
"data": {
"user": {
"username": "风起时呀",//收货人
"mobile": ""//收货人电话
收货地址:微信地址(由小程序自己调取,这里不提供)
},
"order": {
"title": "礼品标题1",//礼品标题
"score": 100,//礼品积分
"images": "/uploads/20190319/74cf9cbb633386b889f15a9797169b7d.jpg,/uploads/20190319/4e98e7e8bb1fa28db4f7a49df3159938.jpg",
"num": "1",//礼品数量
"order_sn": "gc_155359401330746300",//订单
"total_score": 100 //礼品总积分
}
}
})
*/
public function exchange(){
if($this->request->isGet()){
$g_id = $this->request->get('g_id');//礼品id,
$g_num = $this->request->get('g_num');//礼品数量
$rule = config('site.g_settle');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['g_id'=>$g_id,'g_num'=>$g_num])) {
$this->error($validate->getError());
}
$data = Db::table('gc_gift')
->where(['id'=>$g_id,'status'=>$this->normal])
->field('title,score,images')
->find();
$data['num'] = $g_num;
$data['order_sn'] = $this->auth->genOrderSn('gc_');//订单编号
$data['total_score'] = intval($data['score']*$g_num);
$user = Db::table('gc_user')
->where(['id'=>$this->user_id,'status'=>'normal'])
->field('username,mobile')
->find();
$this->success('成功',['user'=>$user,'order'=>$data]);
}else{
$this->error('请求方式出错');
}
}
/**
* @ApiTitle (礼品提交订单)
* @ApiSummary (礼品提交订单)
* @ApiMethod (POST)
* @ApiRoute (/api/gorders/giftPurchase)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="g_id", type="integer", required=true, description="礼品id")
* @ApiParams (name="g_num", type="integer", required=true, description="礼品数量")
* @ApiParams (name="order_sn", type="string", required=true, description="礼品订单号")
* @ApiParams (name="address", type="string", required=true, description="收货地址(微信地址)")
* @ApiReturn ({
"code": 1,
"msg": "提交成功",
"time": "1553837090",
"data": null
})
*/
public function giftPurchase(){
if($this->request->isPost()){
$g_id = $this->request->post('g_id');//礼品id,
$g_num = $this->request->post('g_num');//礼品数量
$order_sn = $this->request->post('order_sn');//礼品订单号
$address = $this->request->post('address');//收货地址(微信地址)
$rule = config('site.g_orders');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['g_id'=>$g_id,'g_num'=>$g_num,'order_sn'=>$order_sn,'address'=>$address])) {
$this->error($validate->getError());
}
$gift_data = Db::table('gc_gift')
->where(['id'=>$g_id,'status'=>$this->normal])
->field('score')
->find();
$stock = $gift_data['stock'] - $g_num;
if($stock < 0){
$this->error('抱歉,您的订单存在库存不足哦');
}
$total_score = $gift_data['score']*$g_num;
$data['order_sn'] = $order_sn;//订单编号
$data['uid'] = $this->user_id;//用户uid
$data['g_id'] = $g_id;//订单礼品id
$data['unit_score'] = $gift_data['score'];//订单礼品积分
$data['num'] = $g_num;//订单礼品数量
$data['total_score'] = intval($total_score);//订单礼品总积分
$data['address'] = $address;
$g_order = new Gorder();
$res = $g_order::create($data);
$user = Db::table('gc_user')
->where(['id'=>$this->user_id,'status'=>'normal'])
->field('score')
->find();
$score = intval($user['score']-$total_score);
$userModel = new User();
$result = $userModel->where(['id'=>$this->user_id])->update(['score'=>$score]);
if($res && $result){
//减库存
$giftModel = new Gift();
$gift = $giftModel->where(['id'=>$g_id,'status'=>$this->normal])->update(['stock'=>$stock]);
if($gift){
$this->success('提交成功');
}else{
$this->error('提交失败');
}
}else{
$this->error('提交失败');
}
}else{
$this->error('请求方式出错');
}
}
/**
* @ApiTitle (礼品订单列表)
* @ApiSummary (礼品订单列表)
* @ApiMethod (GET)
* @ApiRoute (/api/gorders/giftOrderList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="integer", required=true, description="分页页码")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553837895",
"data": [
{
"id": 1,
"order_sn": "gc_155358608729209200",//订单号
"username": "风起时呀",//用户名
"title": "礼品标题1",//礼品名称
"num": 1,//礼品数量
"createtime": "2019-03-26"//下单时间
},
{
"id": 2,
"order_sn": "gc_155358608729209201",
"username": "风起时呀",
"title": "礼品标题1",
"num": 1,
"createtime": "2019-03-26"
}
]
})
*/
public function giftOrderList(){
if($this->request->isGet()){
$page = $this->request->get('page');//分页页码
$limit = config('site.page_limit');//分页限制数量
$rule = config('site.gift_pages');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['page'=>$page])) {
$this->error($validate->getError());
}
$data = Db::table('gc_gorder')
->alias('o')
->join('gc_gift g','o.g_id = g.id','LEFT')
->join('gc_user u','o.uid = u.id','LEFT')
->where(['o.uid'=>$this->user_id,'g.status'=>$this->normal,'u.status'=>'normal'])
->page($page,$limit)
->field('o.id,o.order_sn,u.username,g.title,o.num,o.createtime')
->select();
foreach($data as &$value){
$value['createtime'] = date('Y-m-d',$value['createtime']);
}
$this->success('成功',$data);
}else{
$this->error('请求方式出错');
}
}
}