602 lines
20 KiB
JavaScript
602 lines
20 KiB
JavaScript
/**
|
|
* Purpose: authApi supports app.js by holding or executing the auth api page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const authApi = require('./api/auth')
|
|
/**
|
|
* Purpose: cartApi supports app.js by holding or executing the cart api page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const cartApi = require('./api/cart')
|
|
/**
|
|
* Purpose: shopApi supports app.js by holding or executing the shop api page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const shopApi = require('./api/shop')
|
|
/**
|
|
* Purpose: userApi supports app.js by holding or executing the user api page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const userApi = require('./api/user')
|
|
/**
|
|
* Purpose: auth supports app.js by holding or executing the auth page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const auth = require('./utils/auth')
|
|
/**
|
|
* Purpose: idleSession supports app.js by holding or executing the idle session page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const idleSession = require('./utils/idle-session')
|
|
/**
|
|
* Purpose: theme supports app.js by holding or executing the theme page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const theme = require('./utils/theme')
|
|
/**
|
|
* Purpose: navigation opens popup-message action paths from the global App lifecycle.
|
|
* Usage: call openSystemMessage after marking a popup message as shown.
|
|
*/
|
|
const navigation = require('./utils/navigation')
|
|
/**
|
|
* Purpose: file supports app.js by holding or executing the file page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const file = require('./utils/file')
|
|
const { IDLE_TIMEOUT_MS, IDLE_WARNING_MS } = require('./utils/config')
|
|
const adminAuth = require('./admin/utils/auth')
|
|
const adminPermission = require('./admin/utils/permission')
|
|
const adminIdleSession = require('./admin/utils/idle-session')
|
|
const { resetLoading: resetAdminLoading } = require('./admin/utils/loading')
|
|
const adminPermissionApi = require('./admin/api/permission')
|
|
const adminAuthApi = require('./admin/api/auth')
|
|
const {
|
|
IDLE_TIMEOUT_MS: ADMIN_IDLE_TIMEOUT_MS,
|
|
IDLE_WARNING_MS: ADMIN_IDLE_WARNING_MS
|
|
} = require('./admin/utils/config')
|
|
|
|
function buildAdminCurrentShop(userInfo) {
|
|
if (!userInfo || !userInfo.shopId) {
|
|
return null
|
|
}
|
|
|
|
return {
|
|
id: userInfo.shopId,
|
|
shopName: userInfo.shopName || ''
|
|
}
|
|
}
|
|
|
|
App({
|
|
globalData: {
|
|
userInfo: null,
|
|
isLoggedIn: false,
|
|
cartCount: 0,
|
|
currentShop: null,
|
|
permissions: [],
|
|
permissionCodes: [],
|
|
pageDirtyFlags: {},
|
|
activeSessionMode: '',
|
|
shopTheme: theme.DEFAULT_SHOP_THEME,
|
|
pendingProductFilter: null,
|
|
pendingWechatMiniLogin: null,
|
|
wechatMiniAutoLoginState: 'idle'
|
|
},
|
|
|
|
/**
|
|
* Purpose: checkPopupMessages globally mirrors Web popup-message behavior across mini-program pages.
|
|
* Usage: call it from onShow after login so pending popup messages are shown even outside the profile page.
|
|
*/
|
|
async checkPopupMessages() {
|
|
if (!auth.isLoggedIn() || this._checkingPopupMessages || !this._appShownOnce) {
|
|
return
|
|
}
|
|
this._checkingPopupMessages = true
|
|
try {
|
|
const messages = await userApi.listMyPopupMessages()
|
|
for (const item of messages || []) {
|
|
await new Promise(resolve => {
|
|
wx.showModal({
|
|
title: item.title || '消息通知',
|
|
content: item.content || '',
|
|
showCancel: false,
|
|
confirmText: navigation.buildMessageActionPath(item) ? '去查看' : '我知道了',
|
|
complete: resolve
|
|
})
|
|
})
|
|
await userApi.markMessagePopupShown(item.id)
|
|
const opened = await navigation.openSystemMessage(item)
|
|
if (opened) {
|
|
break
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('加载待弹窗消息失败', error)
|
|
} finally {
|
|
this._checkingPopupMessages = false
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: onLaunch supports app.js by holding or executing the on launch page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
onLaunch() {
|
|
idleSession.init({
|
|
timeoutMs: IDLE_TIMEOUT_MS,
|
|
warningMs: IDLE_WARNING_MS,
|
|
isLoggedIn: () => auth.isLoggedIn(),
|
|
onLogout: () => this.handleIdleLogout()
|
|
})
|
|
adminIdleSession.init({
|
|
timeoutMs: ADMIN_IDLE_TIMEOUT_MS,
|
|
warningMs: ADMIN_IDLE_WARNING_MS,
|
|
isLoggedIn: () => adminAuth.isLoggedIn(),
|
|
onLogout: () => this.handleAdminIdleLogout()
|
|
})
|
|
this.bootstrapSession()
|
|
this.bootstrapAdminSession()
|
|
if (!auth.isLoggedIn() && !adminAuth.isLoggedIn()) {
|
|
this.tryWechatMiniAutoLogin()
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: onShow supports app.js by holding or executing the on show page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
onShow() {
|
|
idleSession.onAppShow()
|
|
adminIdleSession.onAppShow()
|
|
this._appShownOnce = true
|
|
this.checkPopupMessages()
|
|
},
|
|
|
|
/**
|
|
* Purpose: onHide supports app.js by holding or executing the on hide page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
onHide() {
|
|
idleSession.onAppHide()
|
|
adminIdleSession.onAppHide()
|
|
},
|
|
|
|
/**
|
|
* Purpose: bootstrapSession supports app.js by holding or executing the bootstrap session page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
bootstrapSession() {
|
|
const userInfo = auth.getStoredUserInfo()
|
|
const isLoggedIn = auth.isLoggedIn()
|
|
this.globalData.userInfo = userInfo
|
|
this.globalData.isLoggedIn = isLoggedIn
|
|
this.globalData.activeSessionMode = isLoggedIn ? 'customer' : this.globalData.activeSessionMode
|
|
this.refreshShopTheme(userInfo)
|
|
|
|
if (userInfo && userInfo.shopId) {
|
|
auth.setPreferredMiniLoginShopId(userInfo.shopId)
|
|
}
|
|
|
|
if (isLoggedIn) {
|
|
this.refreshSessionUserInfo()
|
|
this.updateCartBadge()
|
|
return
|
|
}
|
|
|
|
this.clearCartBadge()
|
|
},
|
|
|
|
bootstrapAdminSession() {
|
|
resetAdminLoading()
|
|
|
|
if (!adminAuth.isLoggedIn()) {
|
|
this.globalData.currentShop = null
|
|
this.setAdminPermissionState([])
|
|
return
|
|
}
|
|
|
|
const userInfo = adminAuth.getStoredUserInfo()
|
|
const currentShop = adminAuth.getCurrentShop() || buildAdminCurrentShop(userInfo)
|
|
const permissions = adminAuth.getStoredPermissions()
|
|
|
|
if (currentShop) {
|
|
adminAuth.setCurrentShop(currentShop)
|
|
}
|
|
|
|
this.globalData.activeSessionMode = 'admin'
|
|
this.globalData.isLoggedIn = true
|
|
this.globalData.userInfo = userInfo
|
|
this.globalData.currentShop = currentShop
|
|
this.setAdminPermissionState(permissions)
|
|
|
|
if (!permissions.length) {
|
|
this.loadCurrentPermissions().catch(() => undefined)
|
|
}
|
|
},
|
|
|
|
setAdminPermissionState(permissions) {
|
|
const list = Array.isArray(permissions) ? permissions : []
|
|
this.globalData.permissions = list
|
|
this.globalData.permissionCodes = adminPermission.getPermissionCodes(list)
|
|
},
|
|
|
|
async setAdminSession(payload) {
|
|
adminAuth.setSession(payload)
|
|
this.bootstrapAdminSession()
|
|
await this.loadCurrentPermissions().catch(() => [])
|
|
},
|
|
|
|
async loadCurrentPermissions() {
|
|
if (!adminAuth.isLoggedIn()) {
|
|
adminAuth.clearPermissions()
|
|
this.setAdminPermissionState([])
|
|
return []
|
|
}
|
|
|
|
const permissions = await adminPermissionApi.getCurrentUserPermissions()
|
|
adminAuth.setPermissions(permissions)
|
|
this.setAdminPermissionState(permissions)
|
|
return permissions
|
|
},
|
|
|
|
refreshAdminUserInfo(userInfo) {
|
|
adminAuth.updateStoredUserInfo(userInfo)
|
|
|
|
if (userInfo && userInfo.shopId) {
|
|
adminAuth.setCurrentShop({
|
|
id: userInfo.shopId,
|
|
shopName: userInfo.shopName || ''
|
|
})
|
|
}
|
|
|
|
this.bootstrapAdminSession()
|
|
},
|
|
|
|
clearAdminSession() {
|
|
adminAuth.clearSession()
|
|
if (this.globalData.activeSessionMode === 'admin' || !auth.isLoggedIn()) {
|
|
this.globalData.isLoggedIn = false
|
|
this.globalData.userInfo = null
|
|
this.globalData.activeSessionMode = ''
|
|
}
|
|
this.globalData.currentShop = null
|
|
this.globalData.pageDirtyFlags = {}
|
|
this.setAdminPermissionState([])
|
|
},
|
|
|
|
async handleAdminIdleLogout() {
|
|
if (!adminAuth.isLoggedIn()) {
|
|
return
|
|
}
|
|
try {
|
|
await adminAuthApi.logout()
|
|
} catch (error) {}
|
|
this.clearAdminSession()
|
|
wx.showToast({
|
|
title: '长时间未操作,已退出后台登录',
|
|
icon: 'none'
|
|
})
|
|
setTimeout(() => {
|
|
wx.reLaunch({
|
|
url: '/admin/pages/login/index'
|
|
})
|
|
}, 260)
|
|
},
|
|
|
|
async switchAdminShop(shopId) {
|
|
const payload = await adminAuthApi.switchShop(shopId)
|
|
await this.setAdminSession(payload)
|
|
return payload
|
|
},
|
|
|
|
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
|
|
},
|
|
|
|
/**
|
|
* Purpose: handleWechatMiniLoginResult supports app.js by holding or executing the handle wechat mini login result page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
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
|
|
},
|
|
|
|
/**
|
|
* Purpose: requestWechatMiniCode supports app.js by holding or executing the request wechat mini code page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
requestWechatMiniCode() {
|
|
return new Promise((resolve, reject) => {
|
|
wx.login({
|
|
success: res => {
|
|
if (res && res.code) {
|
|
resolve(res.code)
|
|
return
|
|
}
|
|
reject(new Error('获取微信登录凭证失败'))
|
|
},
|
|
fail: reject
|
|
})
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: isWechatMiniAutoLoginPending supports app.js by holding or executing the is wechat mini auto login pending page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
isWechatMiniAutoLoginPending() {
|
|
return this.globalData.wechatMiniAutoLoginState === 'pending'
|
|
},
|
|
|
|
/**
|
|
* Purpose: waitForWechatMiniAutoLogin supports app.js by holding or executing the wait for wechat mini auto login page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
waitForWechatMiniAutoLogin() {
|
|
return this._wechatMiniAutoLoginPromise || Promise.resolve(auth.isLoggedIn())
|
|
},
|
|
|
|
/**
|
|
* Purpose: setPendingWechatMiniLogin supports app.js by holding or executing the set pending wechat mini login page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
setPendingWechatMiniLogin(payload) {
|
|
this.globalData.pendingWechatMiniLogin = payload || null
|
|
},
|
|
|
|
/**
|
|
* Purpose: consumePendingWechatMiniLogin supports app.js by holding or executing the consume pending wechat mini login page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
consumePendingWechatMiniLogin() {
|
|
const pending = this.globalData.pendingWechatMiniLogin
|
|
this.globalData.pendingWechatMiniLogin = null
|
|
return pending
|
|
},
|
|
|
|
/**
|
|
* Purpose: clearPendingWechatMiniLogin supports app.js by holding or executing the clear pending wechat mini login page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
clearPendingWechatMiniLogin() {
|
|
this.globalData.pendingWechatMiniLogin = null
|
|
},
|
|
|
|
/**
|
|
* Purpose: setUserSession supports app.js by holding or executing the set user session page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
setUserSession(payload) {
|
|
auth.setSession(payload)
|
|
this.globalData.userInfo = auth.getStoredUserInfo()
|
|
this.globalData.isLoggedIn = auth.isLoggedIn()
|
|
this.globalData.activeSessionMode = 'customer'
|
|
this.refreshShopTheme(this.globalData.userInfo)
|
|
|
|
if (this.globalData.userInfo && this.globalData.userInfo.shopId) {
|
|
auth.setPreferredMiniLoginShopId(this.globalData.userInfo.shopId)
|
|
}
|
|
|
|
this.updateCartBadge()
|
|
this.checkPopupMessages()
|
|
},
|
|
|
|
/**
|
|
* Purpose: refreshUserInfo supports app.js by holding or executing the refresh user info page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
refreshUserInfo(userInfo) {
|
|
auth.updateStoredUserInfo(userInfo)
|
|
this.globalData.userInfo = userInfo
|
|
this.globalData.isLoggedIn = auth.isLoggedIn()
|
|
this.globalData.activeSessionMode = auth.isLoggedIn() ? 'customer' : ''
|
|
this.refreshShopTheme(userInfo)
|
|
|
|
if (userInfo && userInfo.shopId) {
|
|
auth.setPreferredMiniLoginShopId(userInfo.shopId)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: clearUserSession supports app.js by holding or executing the clear user session page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
clearUserSession() {
|
|
auth.clearSession()
|
|
if (this.globalData.activeSessionMode === 'customer' || !adminAuth.isLoggedIn()) {
|
|
this.globalData.userInfo = null
|
|
this.globalData.isLoggedIn = false
|
|
this.globalData.activeSessionMode = ''
|
|
}
|
|
this.setShopTheme(null)
|
|
this.clearCartBadge()
|
|
},
|
|
|
|
/**
|
|
* Purpose: setShopTheme supports app.js by holding or executing the set shop theme page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
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)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: syncThemeToCurrentPages supports app.js by holding or executing the sync theme to current pages page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
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)
|
|
}
|
|
})
|
|
},
|
|
|
|
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()
|
|
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
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: clearCartBadge supports app.js by holding or executing the clear cart badge page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
clearCartBadge() {
|
|
this.globalData.cartCount = 0
|
|
wx.removeTabBarBadge({
|
|
index: 2,
|
|
fail() {}
|
|
})
|
|
}
|
|
})
|