Prize.php
5.4 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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/12/20
* Time: 10:20
*/
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
/**
* 抽奖模块
*/
class Prize extends Api
{
/**
* @ApiTitle (奖品列表)
* @ApiSummary (奖品列表)
* @ApiMethod (POST)
* @ApiRoute (/api/prize/index)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
"id":"id",//奖品id
"prize_week"://奖品期数
"prize_name"://奖品名称
"content"://奖品描述
"thumbnail"://奖品图片
"probability"://概率
"prize_num"://库存
"createtime":"createtime",//创建时间
}
})
*/
public function index()
{
$user_id = $this->getUserId();
$data = Db::name('prize')
->order('prize_week desc,id desc')
->limit(8)
->select();
foreach ($data as &$v){
$v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
$v['thumbnail'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['thumbnail'];
}
$this->success('success',$data);
}
/**
* @ApiTitle (抽奖)
* @ApiSummary (抽奖)
* @ApiMethod (POST)
* @ApiRoute (/api/prize/chou)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="name", type="string", required=false, description="姓名")
* @ApiParams (name="phone", type="string", required=false, description="电话")
* @ApiParams (name="address", type="string", required=false, description="地址")
*
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
"id":"id",//奖品id
}
})
*/
public function chou()
{
$user_id = $this->getUserId();
$param = $this->request->param();
$user = Db::name('user')
->where('id',$user_id)
->find();
if($user['score'] < 10){
$this->error('您的积分不足',$user['score']);
}
$data = Db::name('prize')
->order('prize_week desc,id desc')
->limit(8)
->select();
//取出所有商品的概率
$arr = array();
foreach ($data as $key => $val) {
$arr[$val['id']] = $val['probability'];
};
$result = '';
//概率数组总精度
$arrSum = array_sum($arr);
//概率数组循环
foreach ($arr as $key => $vv) {
$randNum = mt_rand(1, $arrSum);
if ($randNum <= $vv) {
$result = $key;
break;
} else {
$arrSum -= $vv;
}
}
unset($arr);
if(empty($param['name']) || empty($param['phone'] || empty($param['address']))){
$this->error('缺少必要参数');
}
$prize = Db::name('prize')
->where('id',$result)
->find();
$res['name'] = $param['name'];
$res['phone'] = $param['phone'];
$res['address'] = $param['address'];
$res['user_id'] = $user_id;
$res['prize_id'] = $result;
$res['createtime'] = time();
$res['prize_week'] = $prize['prize_week'];
$res['prize_name'] = $prize['prize_name'];
Db::startTrans();
//将用户的积分对应减少
$score = Db::name('user')
->where('id',$user_id)
->update(['score'=>$user['score']-10]);
//添加到用户获奖记录中
$res = Db::name('prizecard')
->insertGetId($res);
//将商品对应的库存减1
$info = Db::name('prize')
->where('id',$result)
->update(['prize_num'=>$prize['prize_nun']-1]);
if($score && $res && $info){
Db::commit();
$this->success('成功');
}else{
Db::rollback();
$this->error('失败');
}
}
/**
* @ApiTitle (中奖历史)
* @ApiSummary (中奖历史)
* @ApiMethod (POST)
* @ApiRoute (/api/prize/history)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
"id":"id",//id
"user_id"://用户ID
"prize_id"://奖品ID
"prize_week"://奖品期数
"status"://状态
"prize_name"://奖品名称
"thumbnail"://奖品图片
"address"://收货地址
"createtime":"createtime",//获奖时间
}
})
*/
public function history()
{
$user_id = $this->getUserId();
$data = Db::name('prizecard')
->where('id',$user_id)
->field('phone,name',true)
->select();
foreach ($data as &$v){
$thumbnail = Db::name('prize')->where('id',$v['prize_id'])->field('thumbnail,prize_name')->find();
$v['thumbnail'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$thumbnail['thumbnail'];
$v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
}
$this->success('success',$data);
}
}