修改了页面样式。
This commit is contained in:
@@ -2,6 +2,9 @@ const AIChatSocket = require('./utils/chatmsg.js')
|
||||
const API = require('./utils/api.js')
|
||||
const request = require('./utils/request.js')
|
||||
|
||||
// 服务列表缓存:只服务首屏秒开,命中后仍会请求最新数据覆盖
|
||||
const SERVICES_CACHE_KEY = 'servicesCacheV1'
|
||||
|
||||
App({
|
||||
onLaunch() {
|
||||
// request 层遇到 code 401(token 已失效)时回调这里:静默重登后自动重试原请求。
|
||||
@@ -9,12 +12,20 @@ App({
|
||||
request.setUnauthorizedHandler(() => this.silentSignin())
|
||||
|
||||
this.loadServices()
|
||||
this.loadAgreement()
|
||||
// 服务协议只有协议页用,改成该页首次进入时再拉(页面里已有 loadAgreement),
|
||||
// 启动期少一次并发请求
|
||||
|
||||
this.checkUpdate();
|
||||
},
|
||||
|
||||
// 已有登录态时不再重签:每次 wxsignin 都会把 token 轮换一遍,
|
||||
// 既多一次网络往返(真机实测 ~105ms),也会让在飞的请求拿着旧 token 收到 401。
|
||||
// token 真正失效时由 request 层的 401 兜底触发 silentSignin。
|
||||
// 用户主动退出过(has_logout)时 silentSignin 本身会直接返回 false
|
||||
onShow() {
|
||||
if (this.globalData.user?._id) {
|
||||
return
|
||||
}
|
||||
this.globalData.authReady = this.silentSignin()
|
||||
},
|
||||
|
||||
@@ -25,6 +36,7 @@ App({
|
||||
.then((res) => {
|
||||
if (res.code !== 0 || !Array.isArray(res.data?.services)) throw new Error(res.msg || '服务列表加载失败')
|
||||
this.globalData.services = res.data.services
|
||||
this.cacheServices(this.globalData.services)
|
||||
return this.globalData.services
|
||||
})
|
||||
.catch((err) => {
|
||||
@@ -35,6 +47,26 @@ App({
|
||||
return this.globalData.servicesReady
|
||||
},
|
||||
|
||||
// 服务列表只影响展示,落一份本地缓存让首页首屏先有内容:
|
||||
// 缓存失败不影响主链路,下一帧网络数据会覆盖
|
||||
cacheServices(services) {
|
||||
try {
|
||||
wx.setStorageSync(SERVICES_CACHE_KEY, services)
|
||||
} catch (err) {
|
||||
console.warn('缓存服务列表失败', err)
|
||||
}
|
||||
},
|
||||
|
||||
// 读缓存用于首屏占位;数据形态与后端一致,写入前已校验过是数组
|
||||
getCachedServices() {
|
||||
try {
|
||||
const cached = wx.getStorageSync(SERVICES_CACHE_KEY)
|
||||
return Array.isArray(cached) && cached.length ? cached : null
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
},
|
||||
|
||||
loadAgreement() {
|
||||
if (this.globalData.agreementReady) return this.globalData.agreementReady
|
||||
this.globalData.agreementReady = API.resource.getAgreement()
|
||||
@@ -52,13 +84,15 @@ App({
|
||||
},
|
||||
|
||||
// 已经拿到用户(如「我的」页手机号授权登录成功)就直接返回成功。
|
||||
// authReady 是 onShow 启动期静默登录的 Promise,登录/退出都不会刷新它:
|
||||
// 直接把旧 Promise 的 false 回放给调用方,会误判「已登录用户未登录」
|
||||
// 但若此刻有静默登录在飞(token 可能正在轮换),要先等它落定:
|
||||
// 抢跑会让页面拿旧 token 发请求、收到 401 再触发一次重登 + 重试。
|
||||
// 登录失败时旧用户/旧 token 仍在,仍然返回 true,由 request 层的 401 兜底决定成败
|
||||
waitForAuth() {
|
||||
if (this.globalData.user?._id) {
|
||||
return Promise.resolve(true)
|
||||
const hasUser = () => !!this.globalData.user?._id
|
||||
if (this.signinPromise) {
|
||||
return this.signinPromise.then(hasUser)
|
||||
}
|
||||
return this.globalData.authReady || this.silentSignin()
|
||||
return hasUser() ? Promise.resolve(true) : this.silentSignin()
|
||||
},
|
||||
|
||||
// 静默重登:app.onShow 与 request 层的 401 兜底共用这一个入口
|
||||
|
||||
Reference in New Issue
Block a user