189 lines
4.4 KiB
Vue
189 lines
4.4 KiB
Vue
<script>
|
||
import { get } from "@/utils/requests.js"
|
||
import { uiUtil } from "@/utils/common.js"
|
||
|
||
export default {
|
||
onLaunch: function (options) {
|
||
const startTime = Date.now()
|
||
console.log('App Launch', options)
|
||
|
||
// 1. 同步初始化配置(必须完成)
|
||
this.initConfig(options)
|
||
|
||
// 2. 异步获取用户数据(不阻塞启动)
|
||
this.fetchUserDataAsync()
|
||
|
||
// 启动完成
|
||
console.log(`App 启动耗时: ${Date.now() - startTime}ms`)
|
||
},
|
||
|
||
onShow: function () {
|
||
console.log('App Show')
|
||
},
|
||
|
||
onHide: function () {
|
||
console.log('App Hide')
|
||
},
|
||
|
||
methods: {
|
||
/**
|
||
* 初始化应用配置
|
||
*/
|
||
initConfig(options) {
|
||
// 检查是否有外部传入的配置数据
|
||
const hasExtraData = options?.referrerInfo?.extraData &&
|
||
JSON.stringify(options.referrerInfo.extraData) !== '{}'
|
||
|
||
if (hasExtraData) {
|
||
this.initFromExtraData(options.referrerInfo.extraData)
|
||
} else {
|
||
this.initFromEnvironment()
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 从外部数据初始化配置
|
||
*/
|
||
initFromExtraData(extraData) {
|
||
// 批量设置 Storage,减少 I/O 次数
|
||
const storageData = {
|
||
host: extraData.host,
|
||
header: {
|
||
"x-token": extraData['x-token'],
|
||
"x-version": extraData['x-version'],
|
||
"x-platform": extraData['x-platform'],
|
||
"x-device-id": extraData['x-device-id'],
|
||
"x-mobile-brand": extraData['x-mobile-brand'],
|
||
"x-mobile-model": extraData['x-mobile-model'],
|
||
"x-channel": extraData['x-channel'],
|
||
"x-package": extraData['x-package'],
|
||
"x-click-id": extraData['x-click-id'],
|
||
// #ifdef APP-PLUS
|
||
"header": {
|
||
"version": this.$version,
|
||
}
|
||
// #endif
|
||
},
|
||
encrypt: extraData['encrypt'],
|
||
decrypt: extraData['decrypt']
|
||
}
|
||
|
||
// 批量写入
|
||
Object.keys(storageData).forEach(key => {
|
||
uni.setStorageSync(key, storageData[key])
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 从环境配置初始化
|
||
*/
|
||
initFromEnvironment() {
|
||
if (process.env.NODE_ENV === 'development') {
|
||
this.initDevelopmentConfig()
|
||
} else {
|
||
// 生产环境警告延迟显示,避免阻塞启动
|
||
setTimeout(() => {
|
||
this.showProductionWarning()
|
||
}, 100)
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 初始化开发环境配置
|
||
*/
|
||
initDevelopmentConfig() {
|
||
// 批量设置开发环境配置
|
||
const devConfig = {
|
||
host: "https://flaunt.batiao8.com/",
|
||
header: { "x-token": "ebe14dab-1879-4c5d-9148-727b96b30aad" },
|
||
decrypt: "e4rOtnF8tJjtHO7ecZeJHN1rapED5ImB",
|
||
encrypt: "xn08hYoizXhZ1zHP8DVqfCm2yHxPmhil"
|
||
}
|
||
|
||
Object.keys(devConfig).forEach(key => {
|
||
uni.setStorageSync(key, devConfig[key])
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 显示生产环境警告
|
||
*/
|
||
showProductionWarning() {
|
||
uni.showModal({
|
||
title: '提示',
|
||
showCancel: false,
|
||
content: "将退出,请重新进入",
|
||
success: function (res) {
|
||
// #ifdef APP-PLUS
|
||
plus.runtime.quit()
|
||
// #endif
|
||
}
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 异步获取用户数据(不阻塞启动)
|
||
*/
|
||
fetchUserDataAsync() {
|
||
// 延迟到下一个事件循环,确保不阻塞启动
|
||
setTimeout(async () => {
|
||
const fetchStart = Date.now()
|
||
try {
|
||
// 并行获取用户信息和配置
|
||
const [userResult, configResult] = await Promise.allSettled([
|
||
this.fetchUserInfo(),
|
||
this.fetchUserConfig()
|
||
])
|
||
|
||
// 处理用户信息结果
|
||
if (userResult.status === 'rejected') {
|
||
console.error('获取用户信息失败:', userResult.reason)
|
||
}
|
||
|
||
// 处理用户配置结果
|
||
if (configResult.status === 'rejected') {
|
||
console.error('获取用户配置失败:', configResult.reason)
|
||
}
|
||
|
||
console.log(`用户数据获取耗时: ${Date.now() - fetchStart}ms`)
|
||
} catch (error) {
|
||
console.error('获取用户数据异常:', error)
|
||
// 不显示错误提示,避免打断用户
|
||
}
|
||
}, 0)
|
||
},
|
||
|
||
/**
|
||
* 获取用户信息
|
||
*/
|
||
async fetchUserInfo() {
|
||
const data = await get('', 'api/user', {})
|
||
if (data.code === 0) {
|
||
uni.setStorageSync('userInfo', data.data)
|
||
console.log('用户信息获取成功')
|
||
return data.data
|
||
} else {
|
||
throw new Error(data.message || '获取用户信息失败')
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 获取用户配置
|
||
*/
|
||
async fetchUserConfig() {
|
||
const data = await get('', 'api/user/config', {})
|
||
if (data.code === 0) {
|
||
uni.setStorageSync('config', data.data)
|
||
console.log('用户配置获取成功')
|
||
return data.data
|
||
} else {
|
||
throw new Error(data.message || '获取用户配置失败')
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style>
|
||
@import "./common/color.css";
|
||
</style> |