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

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
+97 -44
View File
@@ -3,6 +3,7 @@ const couponApi = require('../../api/coupon')
const orderApi = require('../../api/order')
const userApi = require('../../api/user')
const wishlistApi = require('../../api/wishlist')
const actionGuard = require('../../utils/action-guard')
const auth = require('../../utils/auth')
const navigation = require('../../utils/navigation')
const { DEFAULT_AVATAR } = require('../../utils/config')
@@ -25,6 +26,7 @@ Page({
},
async loadProfile() {
this._avatarReloadSource = ''
const isLoggedIn = auth.isLoggedIn()
this.setData({
isLoggedIn,
@@ -60,7 +62,7 @@ Page({
})
} catch (error) {
wx.showToast({
title: error.message || '个人中心加载失败',
title: error.message || '\u4e2a\u4eba\u4e2d\u5fc3\u52a0\u8f7d\u5931\u8d25',
icon: 'none'
})
} finally {
@@ -83,28 +85,28 @@ Page({
},
handleOpenOrders() {
if (!navigation.ensureLogin('查看订单')) {
if (!navigation.ensureLogin('\u67e5\u770b\u8ba2\u5355')) {
return
}
navigation.navigate('/pages/orders/index')
},
handleOpenWishlist() {
if (!navigation.ensureLogin('查看收藏')) {
if (!navigation.ensureLogin('\u67e5\u770b\u6536\u85cf')) {
return
}
navigation.navigate('/pages/wishlist/index')
},
handleOpenCoupons() {
if (!navigation.ensureLogin('查看优惠券')) {
if (!navigation.ensureLogin('\u67e5\u770b\u4f18\u60e0\u5238')) {
return
}
navigation.navigate('/pages/coupons/index')
},
async handleSwitchShop() {
if (!navigation.ensureLogin('切换门店')) {
if (!navigation.ensureLogin('\u5207\u6362\u95e8\u5e97')) {
return
}
@@ -114,7 +116,7 @@ Page({
if (!shopIds.length) {
wx.showToast({
title: '当前账号还没有可用门店',
title: '\u5f53\u524d\u8d26\u53f7\u8fd8\u6ca1\u6709\u53ef\u7528\u95e8\u5e97',
icon: 'none'
})
return
@@ -128,31 +130,38 @@ Page({
return
}
const selection = await new Promise(resolve => {
wx.showActionSheet({
itemList: shopNames.map((name, index) => name || `门店 ${shopIds[index]}`),
success: res => resolve(res.tapIndex),
fail: () => resolve(-1)
})
})
if (selection < 0 || selection >= shopIds.length) {
return
}
try {
const payload = await runWithLoading(() => authApi.switchShop({
shopId: shopIds[selection]
}))
getApp().setUserSession(payload)
wx.showToast({
title: '门店已切换',
icon: 'none'
await actionGuard.run(this, 'user:switch-shop', async () => {
const selection = await new Promise(resolve => {
wx.showActionSheet({
itemList: shopNames.map((name, index) => name || `\u95e8\u5e97 ${shopIds[index]}`),
success: res => resolve(res.tapIndex),
fail: () => resolve(-1)
})
})
if (selection < 0 || selection >= shopIds.length) {
return
}
const payload = await runWithLoading(() => authApi.switchShop({
shopId: shopIds[selection]
}))
getApp().setUserSession(payload)
wx.showToast({
title: '\u95e8\u5e97\u5df2\u5207\u6362',
icon: 'none'
})
await this.loadProfile()
}, {
message: '\u95e8\u5e97\u5207\u6362\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c'
})
this.loadProfile()
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '门店切换失败',
title: error.message || '\u95e8\u5e97\u5207\u6362\u5931\u8d25',
icon: 'none'
})
}
@@ -164,27 +173,71 @@ Page({
return
}
const confirmed = await new Promise(resolve => {
wx.showModal({
title: '退出登录',
content: '退出后需要重新登录才能查看订单和继续下单。',
success: res => resolve(Boolean(res.confirm))
})
})
try {
await actionGuard.run(this, 'auth:logout', async () => {
const confirmed = await new Promise(resolve => {
wx.showModal({
title: '\u9000\u51fa\u767b\u5f55',
content: '\u9000\u51fa\u540e\u9700\u8981\u91cd\u65b0\u767b\u5f55\u624d\u80fd\u67e5\u770b\u8ba2\u5355\u548c\u7ee7\u7eed\u4e0b\u5355\u3002',
success: res => resolve(Boolean(res.confirm))
})
})
if (!confirmed) {
if (!confirmed) {
return
}
try {
await runWithLoading(() => authApi.logout())
} catch (error) {}
getApp().clearUserSession()
await this.loadProfile()
wx.showToast({
title: '\u5df2\u9000\u51fa\u767b\u5f55',
icon: 'none'
})
}, {
message: '\u9000\u51fa\u5904\u7406\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c'
})
} catch (error) {
if (!actionGuard.isGuardError(error)) {
throw error
}
}
},
async handleAvatarError() {
const currentAvatar = this.data.userInfo && this.data.userInfo.avatar
if (!currentAvatar) {
return
}
try {
await runWithLoading(() => authApi.logout())
} catch (error) {}
if (this._avatarReloadSource === currentAvatar) {
this.setData({
'userInfo.avatar': ''
})
return
}
getApp().clearUserSession()
await this.loadProfile()
wx.showToast({
title: '已退出登录',
icon: 'none'
})
this._avatarReloadSource = currentAvatar
try {
const userInfo = await userApi.getUserInfo()
getApp().refreshUserInfo(userInfo)
if (!userInfo || !userInfo.avatar || userInfo.avatar === currentAvatar) {
this.setData({
'userInfo.avatar': ''
})
return
}
this.setData({
userInfo
})
} catch (error) {
this.setData({
'userInfo.avatar': ''
})
}
}
})
+5 -9
View File
@@ -6,16 +6,16 @@
<view class="page-shell profile-page">
<view class="page-content profile-content">
<view wx:if="{{!isLoggedIn}}" class="page-card guest-card">
<image class="guest-avatar" src="{{defaultAvatar}}" mode="aspectFill"></image>
<image class="guest-avatar" src="{{defaultAvatar}}" mode="aspectFill" lazy-load="{{true}}"></image>
<view class="guest-title">还没有登录账号</view>
<view class="guest-desc">登录后可以查看订单、收藏作品、领取和使用优惠。</view>
<view class="guest-desc">登录后查看订单、收藏和优惠券。</view>
<view class="primary-btn" bindtap="handleOpenAuth">去登录</view>
</view>
<block wx:elif="{{userInfo}}">
<view class="page-card profile-card">
<view class="profile-top">
<image class="avatar" src="{{userInfo.avatar || defaultAvatar}}" mode="aspectFill"></image>
<image class="avatar" src="{{userInfo.avatar || defaultAvatar}}" mode="aspectFill" lazy-load="{{true}}" binderror="handleAvatarError"></image>
<view class="profile-copy">
<view class="nickname">{{userInfo.nickname || 'Yumi 用户'}}</view>
<view class="account">{{userInfo.account}}</view>
@@ -27,17 +27,14 @@
<view class="summary-metric" bindtap="handleOpenOrders">
<text class="summary-label">订单</text>
<text class="summary-value">{{orderCount}}</text>
<text class="summary-note">查看预约记录</text>
</view>
<view class="summary-metric" bindtap="handleOpenWishlist">
<text class="summary-label">收藏</text>
<text class="summary-value">{{wishlistCount}}</text>
<text class="summary-note">回看喜欢的作品</text>
</view>
<view class="summary-metric" bindtap="handleOpenCoupons">
<text class="summary-label">优惠券</text>
<text class="summary-value">{{couponCount}}</text>
<text class="summary-note">结算前可直接使用</text>
</view>
</view>
@@ -50,15 +47,14 @@
</view>
<view wx:if="{{needSetPassword}}" class="password-tip">
建议尽快补充登录密码,之后切换设备会更方便。
建议尽快设置登录密码。
</view>
<view class="secondary-btn edit-btn" bindtap="handleOpenSettings">编辑资料与设置</view>
<view class="secondary-btn edit-btn" bindtap="handleOpenSettings">资料与设置</view>
</view>
<view class="page-card menu-card">
<view class="section-title">常用入口</view>
<view class="section-subtitle">订单、收藏、优惠券和账号设置都集中在这里。</view>
<view class="menu-item" bindtap="handleOpenOrders">
<text>我的订单</text>
<text class="menu-arrow">></text>