功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<view class="media-box">
|
||||
<swiper class="media-swiper" indicator-dots autoplay circular>
|
||||
<swiper-item wx:for="{{productImages}}" wx:key="*this">
|
||||
<image class="media-image" src="{{item}}" mode="aspectFill" data-url="{{item}}" bindtap="handlePreviewImage"></image>
|
||||
<image class="media-image" src="{{item}}" mode="aspectFill" data-url="{{item}}" bindtap="handlePreviewImage" lazy-load="{{true}}"></image>
|
||||
</swiper-item>
|
||||
</swiper>
|
||||
<view class="media-mask">
|
||||
@@ -30,24 +30,24 @@
|
||||
<text class="section-kicker">作品详情</text>
|
||||
<view class="product-title">{{product.displayTitle}}</view>
|
||||
<view class="product-price">{{product.priceText}}</view>
|
||||
<view class="product-brief">适合先看作品风格、门店信息和可预约情况,再决定是否立即下单。</view>
|
||||
<view class="product-brief">查看作品、门店和可预约信息。</view>
|
||||
</view>
|
||||
|
||||
<view class="summary-strip detail-summary">
|
||||
<view class="summary-metric" bindtap="handleOpenShop">
|
||||
<text class="summary-label">所属门店</text>
|
||||
<text class="summary-value detail-summary-value">{{shopInfo ? shopInfo.shopName : '待绑定'}}</text>
|
||||
<text class="summary-note">点击查看门店详情</text>
|
||||
<text class="summary-note">查看门店</text>
|
||||
</view>
|
||||
<view class="summary-metric">
|
||||
<text class="summary-label">可约美甲师</text>
|
||||
<text class="summary-value detail-summary-value">{{artistCountText}}</text>
|
||||
<text class="summary-note">预约前可在购物车继续选择</text>
|
||||
<text class="summary-note">可继续选择</text>
|
||||
</view>
|
||||
<view class="summary-metric">
|
||||
<text class="summary-label">最早可约</text>
|
||||
<text class="summary-value detail-summary-value">{{earliestBookableText}}</text>
|
||||
<text class="summary-note">以门店营业时间为准</text>
|
||||
<text class="summary-note">以营业时间为准</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user