更新完善页面
This commit is contained in:
@@ -89,21 +89,28 @@ async function deletePointsGoods(ctx) {
|
||||
}
|
||||
|
||||
async function exchangePointsGoods(ctx) {
|
||||
const { userId, goodsId, quantity } = ctx.request.body
|
||||
const user = ctx.state.user
|
||||
if (!user) {
|
||||
ctx.status = 401
|
||||
ctx.body = { code: 401, message: '未登录' }
|
||||
return
|
||||
}
|
||||
const { goodsId, quantity } = ctx.request.body || {}
|
||||
|
||||
if (!userId || !goodsId) {
|
||||
if (!goodsId) {
|
||||
ctx.body = { code: 400, message: '参数不完整' }
|
||||
return
|
||||
}
|
||||
|
||||
const qty = quantity || 1
|
||||
const userId = user.id
|
||||
const qty = Math.max(1, Math.min(parseInt(quantity) || 1, 99))
|
||||
|
||||
const users = await query('SELECT * FROM users WHERE id = ? AND status = 1', [userId])
|
||||
if (users.length === 0) {
|
||||
ctx.body = { code: 404, message: '用户不存在' }
|
||||
return
|
||||
}
|
||||
const user = users[0]
|
||||
const currentUserRow = users[0]
|
||||
|
||||
const goods = await query('SELECT * FROM points_goods WHERE id = ? AND is_show = 1', [goodsId])
|
||||
if (goods.length === 0) {
|
||||
@@ -118,13 +125,14 @@ async function exchangePointsGoods(ctx) {
|
||||
}
|
||||
|
||||
const totalPoints = goodsItem.points * qty
|
||||
if (user.points < totalPoints) {
|
||||
if (currentUserRow.points < totalPoints) {
|
||||
ctx.body = { code: 400, message: '积分不足' }
|
||||
return
|
||||
}
|
||||
|
||||
let newPoints
|
||||
await transaction(async (conn) => {
|
||||
const newPoints = user.points - totalPoints
|
||||
newPoints = currentUserRow.points - totalPoints
|
||||
await conn.execute('UPDATE users SET points = ? WHERE id = ?', [newPoints, userId])
|
||||
await conn.execute('UPDATE points_goods SET stock = stock - ? WHERE id = ?', [qty, goodsId])
|
||||
await conn.execute(
|
||||
@@ -136,9 +144,7 @@ async function exchangePointsGoods(ctx) {
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
message: '兑换成功',
|
||||
data: {
|
||||
remainingPoints: newPoints
|
||||
}
|
||||
data: { remainingPoints: newPoints }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user