功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
const authApi = require('./api/auth')
|
||||
const cartApi = require('./api/cart')
|
||||
const userApi = require('./api/user')
|
||||
const auth = require('./utils/auth')
|
||||
const idleSession = require('./utils/idle-session')
|
||||
const { IDLE_TIMEOUT_MS, IDLE_WARNING_MS } = require('./utils/config')
|
||||
|
||||
App({
|
||||
globalData: {
|
||||
@@ -13,12 +16,26 @@ App({
|
||||
},
|
||||
|
||||
onLaunch() {
|
||||
idleSession.init({
|
||||
timeoutMs: IDLE_TIMEOUT_MS,
|
||||
warningMs: IDLE_WARNING_MS,
|
||||
isLoggedIn: () => auth.isLoggedIn(),
|
||||
onLogout: () => this.handleIdleLogout()
|
||||
})
|
||||
this.bootstrapSession()
|
||||
if (!auth.isLoggedIn()) {
|
||||
this.tryWechatMiniAutoLogin()
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
idleSession.onAppShow()
|
||||
},
|
||||
|
||||
onHide() {
|
||||
idleSession.onAppHide()
|
||||
},
|
||||
|
||||
bootstrapSession() {
|
||||
const userInfo = auth.getStoredUserInfo()
|
||||
const isLoggedIn = auth.isLoggedIn()
|
||||
@@ -30,6 +47,7 @@ App({
|
||||
}
|
||||
|
||||
if (isLoggedIn) {
|
||||
this.refreshSessionUserInfo()
|
||||
this.updateCartBadge()
|
||||
return
|
||||
}
|
||||
@@ -168,6 +186,35 @@ App({
|
||||
this.clearCartBadge()
|
||||
},
|
||||
|
||||
async refreshSessionUserInfo() {
|
||||
if (!auth.isLoggedIn()) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
const userInfo = await userApi.getUserInfo()
|
||||
this.refreshUserInfo(userInfo)
|
||||
} catch (error) {}
|
||||
},
|
||||
|
||||
async handleIdleLogout() {
|
||||
if (!auth.isLoggedIn()) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
await authApi.logout()
|
||||
} catch (error) {}
|
||||
this.clearUserSession()
|
||||
wx.showToast({
|
||||
title: '长时间未操作,已退出登录',
|
||||
icon: 'none'
|
||||
})
|
||||
setTimeout(() => {
|
||||
wx.reLaunch({
|
||||
url: '/pages/auth/index'
|
||||
})
|
||||
}, 260)
|
||||
},
|
||||
|
||||
async updateCartBadge() {
|
||||
if (!auth.isLoggedIn()) {
|
||||
this.clearCartBadge()
|
||||
|
||||
Reference in New Issue
Block a user