121 lines
3.4 KiB
JavaScript
121 lines
3.4 KiB
JavaScript
const { wxLogin } = require('./utils/auth')
|
||
const request = require('./utils/request')
|
||
const { DEFAULT_API_BASE_URL, setApiBaseUrl, getApiBaseUrl, getCustomerServiceWechat, setCustomerServiceWechat } = require('./utils/constants')
|
||
|
||
App({
|
||
globalData: {
|
||
userInfo: null,
|
||
token: wx.getStorageSync('token') || '',
|
||
refreshToken: wx.getStorageSync('refresh_token') || '',
|
||
baseUrl: getApiBaseUrl(),
|
||
launchError: null,
|
||
customerServiceWechat: getCustomerServiceWechat()
|
||
},
|
||
|
||
loginPromise: null,
|
||
|
||
async onLaunch() {
|
||
wx.showShareMenu({
|
||
menus: ['shareAppMessage', 'shareTimeline']
|
||
})
|
||
|
||
await this.syncPublicConfig().catch((err) => {
|
||
console.warn('Sync public config failed:', err)
|
||
})
|
||
|
||
try {
|
||
await this.ensureLogin()
|
||
} catch (err) {
|
||
// Do not block page rendering when local backend or wx login is unavailable.
|
||
console.error('App launch login failed:', err)
|
||
this.globalData.launchError = err
|
||
}
|
||
},
|
||
|
||
async ensureLogin() {
|
||
if (this.loginPromise) {
|
||
return this.loginPromise
|
||
}
|
||
|
||
this.loginPromise = this.doEnsureLogin()
|
||
try {
|
||
return await this.loginPromise
|
||
} catch (err) {
|
||
// 登录失败时清除缓存的 Promise,允许下次重试
|
||
this.loginPromise = null
|
||
throw err
|
||
} finally {
|
||
this.loginPromise = null
|
||
}
|
||
},
|
||
|
||
async doEnsureLogin() {
|
||
const token = wx.getStorageSync('token')
|
||
if (!token) {
|
||
const loginData = await wxLogin()
|
||
this.globalData.token = loginData.access_token
|
||
this.globalData.refreshToken = loginData.refresh_token || ''
|
||
this.globalData.userInfo = loginData.user_info || null
|
||
return loginData
|
||
}
|
||
|
||
this.globalData.token = token
|
||
try {
|
||
const userInfo = await request({ url: '/users/me' })
|
||
this.globalData.userInfo = userInfo
|
||
return userInfo
|
||
} catch (err) {
|
||
wx.removeStorageSync('token')
|
||
wx.removeStorageSync('refresh_token')
|
||
const loginData = await wxLogin()
|
||
this.globalData.token = loginData.access_token
|
||
this.globalData.refreshToken = loginData.refresh_token || ''
|
||
this.globalData.userInfo = loginData.user_info || null
|
||
return loginData
|
||
}
|
||
},
|
||
|
||
async syncPublicConfig() {
|
||
const currentBaseUrl = getApiBaseUrl()
|
||
const origin = currentBaseUrl.replace(/\/api\/v1$/, '')
|
||
|
||
const config = await new Promise((resolve, reject) => {
|
||
const timer = setTimeout(() => {
|
||
reject(new Error('public-config timeout'))
|
||
}, 5000)
|
||
|
||
wx.request({
|
||
url: `${origin}/api/v1/auth/public-config`,
|
||
method: 'GET',
|
||
success(res) {
|
||
clearTimeout(timer)
|
||
if (!res.data || res.data.code !== 0) {
|
||
reject(res.data || new Error('Load public config failed'))
|
||
return
|
||
}
|
||
resolve(res.data.data)
|
||
},
|
||
fail(err) {
|
||
clearTimeout(timer)
|
||
reject(err)
|
||
}
|
||
})
|
||
})
|
||
|
||
if (config && config.api_base_url) {
|
||
setApiBaseUrl(config.api_base_url)
|
||
this.globalData.baseUrl = config.api_base_url
|
||
}
|
||
|
||
if (config && config.customer_service_wechat) {
|
||
setCustomerServiceWechat(config.customer_service_wechat)
|
||
this.globalData.customerServiceWechat = config.customer_service_wechat
|
||
}
|
||
|
||
if (!config || !config.api_base_url) {
|
||
setApiBaseUrl(DEFAULT_API_BASE_URL)
|
||
this.globalData.baseUrl = DEFAULT_API_BASE_URL
|
||
}
|
||
}
|
||
})
|