功能:收口小程序前后台安全与防重复提交

This commit is contained in:
xiaowang
2026-04-20 16:02:57 +08:00
parent eb1c209d79
commit a3df262fae
237 changed files with 14068 additions and 728 deletions
+60 -35
View File
@@ -3,6 +3,7 @@ const productApi = require('../../api/product')
const productLikeApi = require('../../api/product-like')
const shopApi = require('../../api/shop')
const wishlistApi = require('../../api/wishlist')
const actionGuard = require('../../utils/action-guard')
const auth = require('../../utils/auth')
const navigation = require('../../utils/navigation')
const { resolveImageList } = require('../../utils/file')
@@ -110,7 +111,7 @@ Page({
...product,
displayTitle: getProductTitle(product),
priceText: `¥${formatPrice(product.price)}`,
descriptionText: product.description || '店家还没有补充作品说明。'
descriptionText: product.description || '商家还没有补充作品说明。'
}
this.setData({
@@ -193,25 +194,32 @@ Page({
}
try {
const nextFavorite = !this.data.isFavorite
if (this.data.isFavorite) {
await runWithLoading(() => wishlistApi.removeFromWishlist(product.id))
product.collectCount = Math.max(0, Number(product.collectCount || 0) - 1)
} else {
await runWithLoading(() => wishlistApi.addToWishlist(product.id))
product.collectCount = Number(product.collectCount || 0) + 1
}
await actionGuard.run(this, `wishlist:toggle:${product.id}`, async () => {
const nextFavorite = !this.data.isFavorite
if (this.data.isFavorite) {
await runWithLoading(() => wishlistApi.removeFromWishlist(product.id))
product.collectCount = Math.max(0, Number(product.collectCount || 0) - 1)
} else {
await runWithLoading(() => wishlistApi.addToWishlist(product.id))
product.collectCount = Number(product.collectCount || 0) + 1
}
this.setData({
isFavorite: nextFavorite,
product
})
this.setData({
isFavorite: nextFavorite,
product
})
wx.showToast({
title: nextFavorite ? '已加入收藏' : '已取消收藏',
icon: 'none'
wx.showToast({
title: nextFavorite ? '已加入收藏' : '已取消收藏',
icon: 'none'
})
}, {
message: '操作进行中,请勿重复点击'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '收藏操作失败',
icon: 'none'
@@ -226,25 +234,32 @@ Page({
}
try {
const nextLiked = !this.data.isLiked
if (this.data.isLiked) {
await runWithLoading(() => productLikeApi.unlikeProduct(product.id))
product.likeCount = Math.max(0, Number(product.likeCount || 0) - 1)
} else {
await runWithLoading(() => productLikeApi.likeProduct(product.id))
product.likeCount = Number(product.likeCount || 0) + 1
}
await actionGuard.run(this, `like:toggle:${product.id}`, async () => {
const nextLiked = !this.data.isLiked
if (this.data.isLiked) {
await runWithLoading(() => productLikeApi.unlikeProduct(product.id))
product.likeCount = Math.max(0, Number(product.likeCount || 0) - 1)
} else {
await runWithLoading(() => productLikeApi.likeProduct(product.id))
product.likeCount = Number(product.likeCount || 0) + 1
}
this.setData({
isLiked: nextLiked,
product
})
this.setData({
isLiked: nextLiked,
product
})
wx.showToast({
title: nextLiked ? '已点赞' : '已取消点赞',
icon: 'none'
wx.showToast({
title: nextLiked ? '已点赞' : '已取消点赞',
icon: 'none'
})
}, {
message: '操作进行中,请勿重复点击'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '点赞操作失败',
icon: 'none'
@@ -259,16 +274,21 @@ Page({
}
try {
await runWithLoading(() => cartApi.addToCart({
await actionGuard.run(this, `cart:add:${product.id}`, () => runWithLoading(() => cartApi.addToCart({
productId: product.id,
quantity: 1
}))
})), {
message: '购物车处理中,请勿重复操作'
})
await getApp().updateCartBadge()
wx.showToast({
title: '已加入购物车',
icon: 'none'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '加入购物车失败',
icon: 'none'
@@ -283,15 +303,20 @@ Page({
}
try {
await runWithLoading(() => cartApi.addToCart({
await actionGuard.run(this, `cart:buy-now:${product.id}`, () => runWithLoading(() => cartApi.addToCart({
productId: product.id,
quantity: 1
}))
})), {
message: '操作进行中,请勿重复点击'
})
await getApp().updateCartBadge()
wx.switchTab({
url: '/pages/cart/index'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '操作失败',
icon: 'none'