Score.php
5.54 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
<?php
namespace addons\exam\controller;
use addons\exam\enum\ScoreGoodOrderStatus;
use addons\exam\enum\ScoreGoodStatus;
use addons\exam\model\ScoreGoodModel;
use addons\exam\model\ScoreGoodOrderModel;
use addons\exam\model\UserModel;
use addons\exam\model\UserScoreLogModel;
/**
* 积分接口
*/
class Score extends Base
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
/**
* 积分记录
*/
public function logs()
{
$list = UserScoreLogModel::where('user_id', $this->auth->id)->order('id desc')->paginate();
$this->success('', ['list' => $list]);
}
/**
* 积分商品列表
*/
public function goods()
{
$this->success('', ScoreGoodModel::where('status', '>', 0)->paginate(10));
}
/**
* 积分商品详情
*/
public function goodDetail()
{
if (!$id = input('id/d', 0)) {
$this->error('缺少商品ID');
}
if (!$good = ScoreGoodModel::get($id)) {
$this->error('商品信息不存在');
}
if ($good['status'] != ScoreGoodStatus::NORMAL) {
$this->error('商品已下架或已售罄');
}
// 上次的收货信息
$order = ScoreGoodOrderModel::where('user_id', $this->auth->id)->order('id', 'desc')->find();
$receiver_info = [
'user_name' => $order['user_name'] ?? '',
'phone' => $order['phone'] ?? '',
'address' => $order['address'] ?? '',
];
$this->success('', [
'good' => $good,
'receiver_info' => $receiver_info
]);
}
/**
* 积分兑换
*/
public function exchange()
{
// $user = $this->auth->getUser();
$user_info = UserModel::getInfo($this->auth->id);
switch (true) {
case !$id = input('id/d'):
fail('缺少商品参数');
case !$quantity = input('quantity/d'):
fail('请填写兑换数量');
case !$user_name = input('user_name/s'):
fail('请填写收货人');
case !$phone = input('phone/s'):
fail('请填写收货人手机');
case !$address = input('address/s'):
fail('请填写收货人地址');
case !$good = ScoreGoodModel::where('id', $id)->where('status', ScoreGoodStatus::NORMAL)->find():
fail('积分商品不存在或已下架');
case $good->stocks < $quantity:
fail('当前积分商品库存不足');
case $good->limit < $quantity || $good->limit < $quantity + ScoreGoodOrderModel::where('user_id', $this->auth->id)->where('good_id', $id)->count():
fail('您的兑换次数超过该商品限购数量');
case $good->price * $quantity > $user_info['score']:
fail('您的积分不足');
}
// 兑换
$exchange = ScoreGoodOrderModel::exchange($this->auth->id, $good, $quantity, $user_name, $phone, $address);
$this->success('兑换成功', [
'exchange' => $exchange,
]);
}
/**
* 积分兑换订单列表
*/
public function orders()
{
$query = ScoreGoodOrderModel::where('user_id', $this->auth->id);
$status = input('status/d');
if (is_numeric($status) && in_array($status, ScoreGoodOrderStatus::getConstantsValues())) {
$query->where('status', $status);
}
$status_list = [
[
'name' => '全部',
'status' => -1,
// 'list' => [],
]
];
foreach (ScoreGoodOrderStatus::getValueDescription() as $status => $text) {
$status_list[] = [
'name' => $text,
'status' => $status,
];
}
$this->success('', [
'status_list' => $status_list,
'list' => $query->order('id', 'desc')->paginate(10),
]);
}
/**
* 积分兑换订单详情
*/
public function orderDetail()
{
if (!$id = input('id/d', 0)) {
$this->error('缺少订单ID');
}
if (!$order = ScoreGoodOrderModel::get($id)) {
$this->error('订单信息不存在');
}
$status_list = [];
foreach (ScoreGoodOrderStatus::getValueDescription() as $status => $text) {
$status_list[] = [
'name' => $text,
'status' => $status,
];
}
$this->success('', [
'status_list' => $status_list,
'order' => $order,
]);
}
/**
* 订单完成
*/
public function complete()
{
if (!$id = input('id/d', 0)) {
$this->error('缺少订单ID');
}
if (!$order = ScoreGoodOrderModel::where('id', $id)->where('user_id', $this->auth->id)->find()) {
$this->error('订单信息不存在');
}
if ($order['status'] != ScoreGoodOrderStatus::SHIP) {
$this->error('订单未发货,无法进行完成操作');
}
$order['status'] = ScoreGoodOrderStatus::COMPLETE;
$order['complete_time'] = time();
if ($order->save()) {
$this->success('订单完成成功');
}
$this->error('完成操作失败,请重试');
}
}