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

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
+94 -62
View File
@@ -1,8 +1,9 @@
const auth = require('../../utils/auth')
const navigation = require('../../utils/navigation')
const cache = require('../../utils/cache')
const { buildProductCards } = require('../../utils/product')
const { resolveImageList } = require('../../utils/file')
const { DEFAULT_SHOP_IMAGE } = require('../../utils/config')
const { DEFAULT_SHOP_IMAGE, CACHE_TTL } = require('../../utils/config')
const productApi = require('../../api/product')
const shopApi = require('../../api/shop')
const userApi = require('../../api/user')
@@ -11,6 +12,7 @@ Page({
data: {
initialized: false,
loading: false,
loadingMore: false,
isLoggedIn: false,
userInfo: null,
greeting: '欢迎来到 Yumi Nail',
@@ -18,14 +20,15 @@ Page({
searchKeyword: '',
bannerImages: [],
featuredItems: [],
popularSource: [],
popularItems: [],
popularPage: 1,
pageSize: 6,
hasMore: false
hasMore: false,
popularTotal: 0
},
onLoad() {
this.homeRequestId = 0
this.loadHomeData()
},
@@ -37,22 +40,16 @@ Page({
},
async onPullDownRefresh() {
cache.removeByPrefix('home:')
await this.loadHomeData()
wx.stopPullDownRefresh()
},
onReachBottom() {
if (!this.data.hasMore) {
if (!this.data.hasMore || this.data.loadingMore) {
return
}
const nextPage = this.data.popularPage + 1
const nextItems = this.data.popularSource.slice(0, nextPage * this.data.pageSize)
this.setData({
popularPage: nextPage,
popularItems: nextItems,
hasMore: nextItems.length < this.data.popularSource.length
})
this.loadPopularProducts(false)
},
syncUserState() {
@@ -88,81 +85,116 @@ Page({
},
async loadHomeData() {
const requestId = ++this.homeRequestId
this.setData({ loading: true })
this.syncUserState()
try {
await Promise.all([
this.loadUserProfile(),
this.loadBannerImages(),
this.loadFeaturedProducts(),
this.loadPopularProducts()
this.loadBannerImages(requestId),
this.loadFeaturedProducts(requestId),
this.loadPopularProducts(true, requestId)
])
this.setData({
initialized: true
})
if (requestId === this.homeRequestId) {
this.setData({
initialized: true
})
}
} catch (error) {
wx.showToast({
title: error.message || '首页加载失败',
icon: 'none'
})
} finally {
this.setData({ loading: false })
if (requestId === this.homeRequestId) {
this.setData({ loading: false })
}
}
},
async loadBannerImages() {
const shops = await shopApi.getPublicShops()
const list = Array.isArray(shops) ? shops : []
const banners = []
for (let index = 0; index < list.length; index += 1) {
const shop = list[index]
const imageIds = shop.coverImageFileId || shop.imageFileIds
const urls = await resolveImageList(imageIds, DEFAULT_SHOP_IMAGE)
if (urls[0]) {
banners.push({
async loadBannerImages(requestId) {
const banners = await cache.remember('home:banners', CACHE_TTL.banner, async () => {
const shops = await shopApi.getPublicShops()
const list = Array.isArray(shops) ? shops : []
const resolved = await Promise.all(list.slice(0, 6).map(async (shop, index) => {
const imageIds = shop.coverImageFileId || shop.imageFileIds
const urls = await resolveImageList(imageIds, DEFAULT_SHOP_IMAGE)
return {
id: `${shop.id}-${index}`,
shopId: shop.id,
shopName: shop.shopName,
businessHours: shop.businessHours || '欢迎到店体验',
imageUrl: urls[0]
}
}))
return resolved.filter(item => item.imageUrl)
})
if (requestId === this.homeRequestId) {
this.setData({
bannerImages: Array.isArray(banners) ? banners : []
})
}
},
async loadFeaturedProducts(requestId) {
const items = await cache.remember('home:featured', CACHE_TTL.homeList, async () => {
const result = await productApi.getProductPage({
page: 1,
size: 6,
tag: '精选'
})
return buildProductCards(result && result.list ? result.list : [])
})
if (requestId === this.homeRequestId) {
this.setData({
featuredItems: Array.isArray(items) ? items : []
})
}
},
async loadPopularProducts(reset, requestId) {
const currentRequestId = requestId || this.homeRequestId
const nextPage = reset ? 1 : this.data.popularPage + 1
const loadingState = reset ? { loading: true } : { loadingMore: true }
this.setData(loadingState)
try {
const result = await productApi.getProductPage({
page: nextPage,
size: this.data.pageSize,
tag: '热门'
})
const cards = await buildProductCards(result && result.list ? result.list : [])
if (currentRequestId !== this.homeRequestId) {
return
}
const mergedItems = reset ? cards : this.data.popularItems.concat(cards)
const total = Number(result && result.total) || 0
this.setData({
popularItems: mergedItems,
popularPage: nextPage,
popularTotal: total,
hasMore: mergedItems.length < total
})
} catch (error) {
if (currentRequestId !== this.homeRequestId) {
return
}
wx.showToast({
title: error.message || '热门作品加载失败',
icon: 'none'
})
} finally {
if (currentRequestId === this.homeRequestId) {
this.setData({
loadingMore: false
})
}
}
this.setData({
bannerImages: banners.slice(0, 6)
})
},
async loadFeaturedProducts() {
let products = await productApi.getProductsByTag('精选')
if (!Array.isArray(products) || !products.length) {
products = await productApi.getAllProducts()
}
const cards = await buildProductCards((products || []).slice(0, 6))
this.setData({
featuredItems: cards
})
},
async loadPopularProducts() {
let products = await productApi.getProductsByTag('热门')
if (!Array.isArray(products) || !products.length) {
products = await productApi.getAllProducts()
}
const cards = await buildProductCards(products || [])
const initialItems = cards.slice(0, this.data.pageSize)
this.setData({
popularSource: cards,
popularItems: initialItems,
popularPage: 1,
hasMore: initialItems.length < cards.length
})
},
handleSearchInput(event) {
+15 -12
View File
@@ -7,7 +7,7 @@
<view class="page-hero home-hero">
<text class="hero-kicker">{{isLoggedIn ? greeting : 'YUMI NAIL 小程序'}}</text>
<text class="hero-title">{{displayName}}</text>
<text class="hero-desc">先看门店氛围,再挑喜欢的作品,最后一站式完成收藏、加购和预约。</text>
<text class="hero-desc">先选喜欢的作品。</text>
<view class="hero-action-row">
<view class="hero-action primary" bindtap="handleProfileTap">{{isLoggedIn ? '进入个人中心' : '立即登录'}}</view>
@@ -31,22 +31,22 @@
<view class="summary-metric">
<text class="summary-label">门店橱窗</text>
<text class="summary-value">{{bannerImages.length ? bannerImages.length : 0}} 家</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">{{featuredItems.length ? featuredItems.length : 0}} 款</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">{{popularItems.length ? popularItems.length : 0}} 条</text>
<text class="summary-note">当前最受欢迎的预约款式</text>
<text class="summary-note">热门款</text>
</view>
</view>
<view wx:if="{{!isLoggedIn}}" class="guest-tip">
登录后可同步收藏、购物车和预约记录,切换设备也不会丢。
登录后可同步收藏和预约记录。
</view>
</view>
@@ -55,7 +55,7 @@
<view class="section-copy">
<text class="section-kicker">门店氛围</text>
<text class="section-title">先看看线下环境</text>
<text class="section-subtitle">轮播展示门店图片和营业时间,帮助你快速判断想去的店。</text>
<text class="section-subtitle">看店铺环境</text>
</view>
</view>
@@ -63,7 +63,7 @@
<swiper class="banner-swiper" indicator-dots autoplay circular interval="3200">
<swiper-item wx:for="{{bannerImages}}" wx:key="id">
<view class="banner-item" data-shop-id="{{item.shopId}}" bindtap="handleBannerTap">
<image class="banner-image" src="{{item.imageUrl}}" mode="aspectFill"></image>
<image class="banner-image" src="{{item.imageUrl}}" mode="aspectFill" lazy-load="{{true}}"></image>
<view class="banner-mask">
<view class="banner-badge">门店实拍</view>
<view class="banner-title">{{item.shopName}}</view>
@@ -76,7 +76,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>
</view>
@@ -85,7 +85,7 @@
<view class="section-copy">
<text class="section-kicker">快速入门</text>
<text class="section-title">精选作品</text>
<text class="section-subtitle">适合先快速逛一圈的热门款式,向右滑动查看更多。</text>
<text class="section-subtitle">先看热门款</text>
</view>
<view class="section-link" bindtap="handleSeeAllFeatured">查看全部</view>
</view>
@@ -102,7 +102,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>
</view>
@@ -111,7 +111,7 @@
<view class="section-copy">
<text class="section-kicker">热门预约</text>
<text class="section-title">最近大家都在看</text>
<text class="section-subtitle">结合点赞和收藏热度展示当前最受欢迎的款式。</text>
<text class="section-subtitle">热门款式</text>
</view>
<view class="section-link" bindtap="handleSeeAllPopular">查看全部</view>
</view>
@@ -121,6 +121,9 @@
<product-card item="{{item}}" show-stats="{{true}}" bindselect="handleProductSelect"></product-card>
</view>
</view>
<view wx:if="{{popularItems.length && (loadingMore || !hasMore)}}" class="section-load-state">
<text class="section-load-text">{{loadingMore ? '正在加载更多热门作品...' : '已经到底了'}}</text>
</view>
<view wx:elif="{{loading}}" class="page-card empty-card">
<text class="empty-title">正在加载首页内容</text>
@@ -128,7 +131,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>
</view>
</view>
+11
View File
@@ -148,3 +148,14 @@
.popular-card {
width: calc(50% - 9rpx);
}
.section-load-state {
display: flex;
justify-content: center;
padding-top: 18rpx;
}
.section-load-text {
font-size: 22rpx;
color: #8d7a84;
}