2026-04-02 13:20:28 +08:00
|
|
|
const authApi = require('./api/auth')
|
2026-04-01 15:18:35 +08:00
|
|
|
const cartApi = require('./api/cart')
|
2026-05-21 18:43:59 +08:00
|
|
|
const shopApi = require('./api/shop')
|
2026-04-20 16:02:57 +08:00
|
|
|
const userApi = require('./api/user')
|
2026-04-02 13:20:28 +08:00
|
|
|
const auth = require('./utils/auth')
|
2026-04-20 16:02:57 +08:00
|
|
|
const idleSession = require('./utils/idle-session')
|
2026-05-21 18:43:59 +08:00
|
|
|
const theme = require('./utils/theme')
|
|
|
|
|
const file = require('./utils/file')
|
2026-04-20 16:02:57 +08:00
|
|
|
const { IDLE_TIMEOUT_MS, IDLE_WARNING_MS } = require('./utils/config')
|
2026-04-01 15:18:35 +08:00
|
|
|
|
|
|
|
|
App({
|
|
|
|
|
globalData: {
|
|
|
|
|
userInfo: null,
|
|
|
|
|
isLoggedIn: false,
|
|
|
|
|
cartCount: 0,
|
2026-05-21 18:43:59 +08:00
|
|
|
shopTheme: theme.DEFAULT_SHOP_THEME,
|
2026-04-02 13:20:28 +08:00
|
|
|
pendingProductFilter: null,
|
|
|
|
|
pendingWechatMiniLogin: null,
|
|
|
|
|
wechatMiniAutoLoginState: 'idle'
|
2026-04-01 15:18:35 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onLaunch() {
|
2026-04-20 16:02:57 +08:00
|
|
|
idleSession.init({
|
|
|
|
|
timeoutMs: IDLE_TIMEOUT_MS,
|
|
|
|
|
warningMs: IDLE_WARNING_MS,
|
|
|
|
|
isLoggedIn: () => auth.isLoggedIn(),
|
|
|
|
|
onLogout: () => this.handleIdleLogout()
|
|
|
|
|
})
|
2026-04-01 15:18:35 +08:00
|
|
|
this.bootstrapSession()
|
2026-04-02 13:20:28 +08:00
|
|
|
if (!auth.isLoggedIn()) {
|
|
|
|
|
this.tryWechatMiniAutoLogin()
|
|
|
|
|
}
|
2026-04-01 15:18:35 +08:00
|
|
|
},
|
|
|
|
|
|
2026-04-20 16:02:57 +08:00
|
|
|
onShow() {
|
|
|
|
|
idleSession.onAppShow()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onHide() {
|
|
|
|
|
idleSession.onAppHide()
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-01 15:18:35 +08:00
|
|
|
bootstrapSession() {
|
|
|
|
|
const userInfo = auth.getStoredUserInfo()
|
|
|
|
|
const isLoggedIn = auth.isLoggedIn()
|
|
|
|
|
this.globalData.userInfo = userInfo
|
|
|
|
|
this.globalData.isLoggedIn = isLoggedIn
|
2026-05-21 18:43:59 +08:00
|
|
|
this.refreshShopTheme(userInfo)
|
2026-04-01 15:18:35 +08:00
|
|
|
|
2026-04-02 13:20:28 +08:00
|
|
|
if (userInfo && userInfo.shopId) {
|
|
|
|
|
auth.setPreferredMiniLoginShopId(userInfo.shopId)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 15:18:35 +08:00
|
|
|
if (isLoggedIn) {
|
2026-04-20 16:02:57 +08:00
|
|
|
this.refreshSessionUserInfo()
|
2026-04-01 15:18:35 +08:00
|
|
|
this.updateCartBadge()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.clearCartBadge()
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-02 13:20:28 +08:00
|
|
|
async tryWechatMiniAutoLogin() {
|
|
|
|
|
if (auth.isLoggedIn()) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this._wechatMiniAutoLoginPromise) {
|
|
|
|
|
return this._wechatMiniAutoLoginPromise
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof wx === 'undefined' || typeof wx.login !== 'function') {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.globalData.wechatMiniAutoLoginState = 'pending'
|
|
|
|
|
this._wechatMiniAutoLoginPromise = this.requestWechatMiniCode()
|
|
|
|
|
.then(code => authApi.wechatMiniAutoLogin({
|
|
|
|
|
wxCode: code,
|
|
|
|
|
preferredShopId: auth.getPreferredMiniLoginShopId() || undefined
|
|
|
|
|
}))
|
|
|
|
|
.then(result => this.handleWechatMiniLoginResult(result, { silent: true }))
|
|
|
|
|
.catch(() => false)
|
|
|
|
|
.finally(() => {
|
|
|
|
|
this._wechatMiniAutoLoginPromise = null
|
|
|
|
|
if (this.globalData.wechatMiniAutoLoginState === 'pending') {
|
|
|
|
|
this.globalData.wechatMiniAutoLoginState = 'idle'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return this._wechatMiniAutoLoginPromise
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleWechatMiniLoginResult(result, options) {
|
|
|
|
|
const config = options || {}
|
|
|
|
|
const status = result && result.status ? result.status : ''
|
|
|
|
|
|
|
|
|
|
if (status === 'SUCCESS') {
|
|
|
|
|
this.setUserSession(result)
|
|
|
|
|
this.clearPendingWechatMiniLogin()
|
|
|
|
|
this.globalData.wechatMiniAutoLoginState = 'idle'
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status === 'SELECT_SHOP') {
|
|
|
|
|
this.setPendingWechatMiniLogin(result)
|
|
|
|
|
this.globalData.wechatMiniAutoLoginState = 'idle'
|
|
|
|
|
if (!config.silent && result.message) {
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: result.message,
|
|
|
|
|
icon: 'none'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.clearPendingWechatMiniLogin()
|
|
|
|
|
this.globalData.wechatMiniAutoLoginState = 'idle'
|
|
|
|
|
if (!config.silent && result && result.message) {
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: result.message,
|
|
|
|
|
icon: 'none'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
requestWechatMiniCode() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
wx.login({
|
|
|
|
|
success: res => {
|
|
|
|
|
if (res && res.code) {
|
|
|
|
|
resolve(res.code)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
reject(new Error('获取微信登录凭证失败'))
|
|
|
|
|
},
|
|
|
|
|
fail: reject
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isWechatMiniAutoLoginPending() {
|
|
|
|
|
return this.globalData.wechatMiniAutoLoginState === 'pending'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
waitForWechatMiniAutoLogin() {
|
|
|
|
|
return this._wechatMiniAutoLoginPromise || Promise.resolve(auth.isLoggedIn())
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setPendingWechatMiniLogin(payload) {
|
|
|
|
|
this.globalData.pendingWechatMiniLogin = payload || null
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
consumePendingWechatMiniLogin() {
|
|
|
|
|
const pending = this.globalData.pendingWechatMiniLogin
|
|
|
|
|
this.globalData.pendingWechatMiniLogin = null
|
|
|
|
|
return pending
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
clearPendingWechatMiniLogin() {
|
|
|
|
|
this.globalData.pendingWechatMiniLogin = null
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-01 15:18:35 +08:00
|
|
|
setUserSession(payload) {
|
|
|
|
|
auth.setSession(payload)
|
|
|
|
|
this.globalData.userInfo = auth.getStoredUserInfo()
|
|
|
|
|
this.globalData.isLoggedIn = auth.isLoggedIn()
|
2026-05-21 18:43:59 +08:00
|
|
|
this.refreshShopTheme(this.globalData.userInfo)
|
2026-04-02 13:20:28 +08:00
|
|
|
|
|
|
|
|
if (this.globalData.userInfo && this.globalData.userInfo.shopId) {
|
|
|
|
|
auth.setPreferredMiniLoginShopId(this.globalData.userInfo.shopId)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 15:18:35 +08:00
|
|
|
this.updateCartBadge()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
refreshUserInfo(userInfo) {
|
|
|
|
|
auth.updateStoredUserInfo(userInfo)
|
|
|
|
|
this.globalData.userInfo = userInfo
|
|
|
|
|
this.globalData.isLoggedIn = auth.isLoggedIn()
|
2026-05-21 18:43:59 +08:00
|
|
|
this.refreshShopTheme(userInfo)
|
2026-04-02 13:20:28 +08:00
|
|
|
|
|
|
|
|
if (userInfo && userInfo.shopId) {
|
|
|
|
|
auth.setPreferredMiniLoginShopId(userInfo.shopId)
|
|
|
|
|
}
|
2026-04-01 15:18:35 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
clearUserSession() {
|
|
|
|
|
auth.clearSession()
|
|
|
|
|
this.globalData.userInfo = null
|
|
|
|
|
this.globalData.isLoggedIn = false
|
2026-05-21 18:43:59 +08:00
|
|
|
this.setShopTheme(null)
|
2026-04-01 15:18:35 +08:00
|
|
|
this.clearCartBadge()
|
|
|
|
|
},
|
|
|
|
|
|
2026-05-21 18:43:59 +08:00
|
|
|
setShopTheme(shop) {
|
|
|
|
|
const resolvedTheme = theme.resolveShopTheme(shop)
|
|
|
|
|
this.globalData.shopTheme = resolvedTheme
|
|
|
|
|
this.syncThemeToCurrentPages()
|
|
|
|
|
if (resolvedTheme.backgroundImageFileId) {
|
|
|
|
|
this.resolveShopThemeBackground(shop, resolvedTheme.backgroundImageFileId)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async resolveShopThemeBackground(shop, fileId) {
|
|
|
|
|
try {
|
|
|
|
|
const backgroundImageUrl = await file.getSignedUrl(fileId)
|
|
|
|
|
if (
|
|
|
|
|
!backgroundImageUrl
|
|
|
|
|
|| !this.globalData.shopTheme
|
|
|
|
|
|| this.globalData.shopTheme.backgroundImageFileId !== String(fileId)
|
|
|
|
|
) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.globalData.shopTheme = theme.resolveShopTheme({
|
|
|
|
|
...(shop || {}),
|
|
|
|
|
themeBackgroundImageUrl: backgroundImageUrl
|
|
|
|
|
})
|
|
|
|
|
this.syncThemeToCurrentPages()
|
|
|
|
|
} catch (error) {}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async refreshShopTheme(userInfo) {
|
|
|
|
|
if (!userInfo || !userInfo.shopId) {
|
|
|
|
|
this.setShopTheme(null)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const shop = await shopApi.getShopById(userInfo.shopId)
|
|
|
|
|
if (this.globalData.userInfo && String(this.globalData.userInfo.shopId) === String(userInfo.shopId)) {
|
|
|
|
|
this.setShopTheme(shop)
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.setShopTheme(null)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
syncThemeToCurrentPages() {
|
|
|
|
|
const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
|
|
|
|
|
const data = theme.pageThemeData(this.globalData.shopTheme)
|
|
|
|
|
pages.forEach(page => {
|
|
|
|
|
if (page && typeof page.setData === 'function') {
|
|
|
|
|
page.setData(data)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-20 16:02:57 +08:00
|
|
|
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)
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-01 15:18:35 +08:00
|
|
|
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() {}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|