功能:新增微信小程序界面
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
const auth = require('./utils/auth')
|
||||
const cartApi = require('./api/cart')
|
||||
|
||||
App({
|
||||
globalData: {
|
||||
userInfo: null,
|
||||
isLoggedIn: false,
|
||||
cartCount: 0,
|
||||
pendingProductFilter: null
|
||||
},
|
||||
|
||||
onLaunch() {
|
||||
this.bootstrapSession()
|
||||
},
|
||||
|
||||
bootstrapSession() {
|
||||
const userInfo = auth.getStoredUserInfo()
|
||||
const isLoggedIn = auth.isLoggedIn()
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = isLoggedIn
|
||||
|
||||
if (isLoggedIn) {
|
||||
this.updateCartBadge()
|
||||
return
|
||||
}
|
||||
|
||||
this.clearCartBadge()
|
||||
},
|
||||
|
||||
setUserSession(payload) {
|
||||
auth.setSession(payload)
|
||||
this.globalData.userInfo = auth.getStoredUserInfo()
|
||||
this.globalData.isLoggedIn = auth.isLoggedIn()
|
||||
this.updateCartBadge()
|
||||
},
|
||||
|
||||
refreshUserInfo(userInfo) {
|
||||
auth.updateStoredUserInfo(userInfo)
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = auth.isLoggedIn()
|
||||
},
|
||||
|
||||
clearUserSession() {
|
||||
auth.clearSession()
|
||||
this.globalData.userInfo = null
|
||||
this.globalData.isLoggedIn = false
|
||||
this.clearCartBadge()
|
||||
},
|
||||
|
||||
async updateCartBadge() {
|
||||
if (!auth.isLoggedIn()) {
|
||||
this.clearCartBadge()
|
||||
return 0
|
||||
}
|
||||
|
||||
try {
|
||||
const cartList = await cartApi.getCartList()
|
||||
const count = Array.isArray(cartList)
|
||||
? cartList.reduce((sum, item) => sum + Number(item.quantity || 0), 0)
|
||||
: 0
|
||||
|
||||
this.globalData.cartCount = count
|
||||
|
||||
if (count > 0) {
|
||||
wx.setTabBarBadge({
|
||||
index: 2,
|
||||
text: count > 99 ? '99+' : String(count)
|
||||
})
|
||||
} else {
|
||||
wx.removeTabBarBadge({ index: 2 })
|
||||
}
|
||||
|
||||
return count
|
||||
} catch (error) {
|
||||
this.clearCartBadge()
|
||||
return 0
|
||||
}
|
||||
},
|
||||
|
||||
clearCartBadge() {
|
||||
this.globalData.cartCount = 0
|
||||
wx.removeTabBarBadge({
|
||||
index: 2,
|
||||
fail() {}
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user