功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
const wishlistApi = require('../../api/wishlist')
|
||||
const cartApi = require('../../api/cart')
|
||||
const actionGuard = require('../../utils/action-guard')
|
||||
const auth = require('../../utils/auth')
|
||||
const navigation = require('../../utils/navigation')
|
||||
const { buildProductCard } = require('../../utils/product')
|
||||
@@ -23,6 +25,7 @@ Page({
|
||||
})
|
||||
|
||||
if (!isLoggedIn) {
|
||||
this.cartProductIdSet = new Set()
|
||||
this.setData({
|
||||
wishlist: [],
|
||||
loading: false
|
||||
@@ -33,10 +36,14 @@ Page({
|
||||
try {
|
||||
const wishlist = await wishlistApi.getWishlist()
|
||||
const list = Array.isArray(wishlist) ? wishlist : []
|
||||
const cartProductIdSet = await this.fetchCartProductIdSet()
|
||||
const cards = await Promise.all(list.map(async item => ({
|
||||
id: item.id,
|
||||
productId: item.productId,
|
||||
productCard: await buildProductCard(item.product || {})
|
||||
productCard: this.markCardInCart(
|
||||
await buildProductCard(item.product || {}),
|
||||
cartProductIdSet
|
||||
)
|
||||
})))
|
||||
this.setData({
|
||||
wishlist: cards
|
||||
@@ -46,7 +53,7 @@ Page({
|
||||
wishlist: []
|
||||
})
|
||||
wx.showToast({
|
||||
title: error.message || '收藏列表加载失败',
|
||||
title: error.message || '\u6536\u85cf\u5217\u8868\u52a0\u8f7d\u5931\u8d25',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
@@ -56,6 +63,57 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
normalizeId(value) {
|
||||
return value === undefined || value === null ? '' : String(value)
|
||||
},
|
||||
|
||||
async fetchCartProductIdSet() {
|
||||
try {
|
||||
const cartList = await cartApi.getCartList()
|
||||
const idSet = new Set(
|
||||
(Array.isArray(cartList) ? cartList : [])
|
||||
.map(item => this.normalizeId(item && item.productId))
|
||||
.filter(Boolean)
|
||||
)
|
||||
this.cartProductIdSet = idSet
|
||||
return idSet
|
||||
} catch (error) {
|
||||
this.cartProductIdSet = new Set()
|
||||
return this.cartProductIdSet
|
||||
}
|
||||
},
|
||||
|
||||
markCardInCart(card, idSet) {
|
||||
return {
|
||||
...card,
|
||||
inCart: (idSet || new Set()).has(this.normalizeId(card && card.id))
|
||||
}
|
||||
},
|
||||
|
||||
markWishlistProductInCart(productId) {
|
||||
const normalizedId = this.normalizeId(productId)
|
||||
if (!normalizedId) {
|
||||
return
|
||||
}
|
||||
|
||||
this.cartProductIdSet = this.cartProductIdSet || new Set()
|
||||
this.cartProductIdSet.add(normalizedId)
|
||||
|
||||
this.setData({
|
||||
wishlist: this.data.wishlist.map(item => (
|
||||
this.normalizeId(item && item.productId) === normalizedId
|
||||
? {
|
||||
...item,
|
||||
productCard: {
|
||||
...item.productCard,
|
||||
inCart: true
|
||||
}
|
||||
}
|
||||
: item
|
||||
))
|
||||
})
|
||||
},
|
||||
|
||||
handleOpenAuth() {
|
||||
navigation.openAuth('/pages/wishlist/index')
|
||||
},
|
||||
@@ -65,11 +123,55 @@ Page({
|
||||
if (!product.id) {
|
||||
return
|
||||
}
|
||||
|
||||
navigation.navigate('/pages/product-detail/index', {
|
||||
id: product.id
|
||||
})
|
||||
},
|
||||
|
||||
async handleQuickAdd(event) {
|
||||
const product = event.detail || {}
|
||||
const productId = this.normalizeId(product.id)
|
||||
if (!productId) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!navigation.ensureLogin('\u52a0\u5165\u8d2d\u7269\u8f66')) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.cartProductIdSet && this.cartProductIdSet.has(productId)) {
|
||||
wx.showToast({
|
||||
title: '\u8d2d\u7269\u8f66\u5df2\u6709',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await actionGuard.run(this, `cart:add:${productId}`, () => runWithLoading(() => cartApi.addToCart({
|
||||
productId,
|
||||
quantity: 1
|
||||
})), {
|
||||
message: '\u8d2d\u7269\u8f66\u5904\u7406\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c'
|
||||
})
|
||||
this.markWishlistProductInCart(productId)
|
||||
await getApp().updateCartBadge()
|
||||
wx.showToast({
|
||||
title: '\u5df2\u52a0\u5165\u8d2d\u7269\u8f66',
|
||||
icon: 'none'
|
||||
})
|
||||
} catch (error) {
|
||||
if (actionGuard.isGuardError(error)) {
|
||||
return
|
||||
}
|
||||
wx.showToast({
|
||||
title: error.message || '\u52a0\u5165\u8d2d\u7269\u8f66\u5931\u8d25',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
async handleRemove(event) {
|
||||
const productId = event.currentTarget.dataset.productId
|
||||
if (!productId) {
|
||||
@@ -77,15 +179,20 @@ Page({
|
||||
}
|
||||
|
||||
try {
|
||||
await runWithLoading(() => wishlistApi.removeFromWishlist(productId))
|
||||
await actionGuard.run(this, `wishlist:remove:${productId}`, () => runWithLoading(() => wishlistApi.removeFromWishlist(productId)), {
|
||||
message: '\u64cd\u4f5c\u8fdb\u884c\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u70b9\u51fb'
|
||||
})
|
||||
wx.showToast({
|
||||
title: '已取消收藏',
|
||||
title: '\u5df2\u53d6\u6d88\u6536\u85cf',
|
||||
icon: 'none'
|
||||
})
|
||||
this.loadWishlist()
|
||||
await this.loadWishlist()
|
||||
} catch (error) {
|
||||
if (actionGuard.isGuardError(error)) {
|
||||
return
|
||||
}
|
||||
wx.showToast({
|
||||
title: error.message || '取消收藏失败',
|
||||
title: error.message || '\u53d6\u6d88\u6536\u85cf\u5931\u8d25',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<view class="page-content">
|
||||
<view wx:if="{{!isLoggedIn}}" class="page-card empty-card">
|
||||
<text class="empty-title">登录后查看收藏</text>
|
||||
<text class="empty-desc">你收藏过的作品会保存在这里,方便之后快速回看。</text>
|
||||
<text class="empty-desc">登录后查看收藏作品。</text>
|
||||
<view class="ghost-btn" bindtap="handleOpenAuth">去登录</view>
|
||||
</view>
|
||||
|
||||
@@ -12,12 +12,20 @@
|
||||
<view class="page-hero wishlist-hero">
|
||||
<text class="hero-kicker">收藏夹</text>
|
||||
<text class="hero-title">已收藏 {{wishlist.length}} 个作品</text>
|
||||
<text class="hero-desc">遇到喜欢的款式先放进收藏,之后再决定是否预约会更从容。</text>
|
||||
<text class="hero-desc">喜欢的款式先收藏。</text>
|
||||
</view>
|
||||
|
||||
<view wx:for="{{wishlist}}" wx:key="id" class="wishlist-item">
|
||||
<product-card item="{{item.productCard}}" bindselect="handleProductSelect"></product-card>
|
||||
<view class="wishlist-action" data-product-id="{{item.productId}}" bindtap="handleRemove">取消收藏</view>
|
||||
<product-card
|
||||
item="{{item.productCard}}"
|
||||
show-quick-add="{{true}}"
|
||||
in-cart="{{item.productCard.inCart}}"
|
||||
bindselect="handleProductSelect"
|
||||
bindquickadd="handleQuickAdd"
|
||||
></product-card>
|
||||
<view class="wishlist-actions">
|
||||
<view class="wishlist-action wishlist-action--secondary" data-product-id="{{item.productId}}" bindtap="handleRemove">取消收藏</view>
|
||||
</view>
|
||||
</view>
|
||||
</block>
|
||||
|
||||
@@ -27,7 +35,7 @@
|
||||
|
||||
<view wx:else class="page-card empty-card">
|
||||
<text class="empty-title">你还没有收藏作品</text>
|
||||
<text class="empty-desc">遇到喜欢的款式先收藏,之后再决定要不要预约。</text>
|
||||
<text class="empty-desc">先去逛逛作品。</text>
|
||||
<view class="ghost-btn" bindtap="handleBack">去逛作品</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@@ -6,15 +6,26 @@
|
||||
margin-top: 22rpx;
|
||||
}
|
||||
|
||||
.wishlist-actions {
|
||||
display: flex;
|
||||
gap: 16rpx;
|
||||
margin-top: 12rpx;
|
||||
}
|
||||
|
||||
.wishlist-action {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 74rpx;
|
||||
margin-top: 12rpx;
|
||||
flex: 1;
|
||||
border-radius: 20rpx;
|
||||
background: #fff1f5;
|
||||
color: #c6597d;
|
||||
font-size: 26rpx;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.wishlist-action--secondary {
|
||||
background: #fff1f5;
|
||||
color: #c6597d;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user