功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -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': ''
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user