193 lines
5.1 KiB
Vue
193 lines
5.1 KiB
Vue
<script>
|
||
export default {
|
||
globalData: {
|
||
NativeEvent: true,
|
||
recentNativeEvent: "", // 初始化一个全局变量
|
||
recentNativeData: 0 // 初始化一个全局变量
|
||
},
|
||
onLaunch: function (options) {
|
||
// === wgt 包启动诊断日志 ===
|
||
console.log('=== App Launch 开始 ===')
|
||
console.log('启动参数:', JSON.stringify(options))
|
||
console.log('环境:', process.env.NODE_ENV)
|
||
|
||
uni.setStorageSync('onNativeEventReceive', "no")
|
||
const startTime = Date.now()
|
||
|
||
// 1. 获取并存储系统信息(只获取一次)
|
||
const systemInfo = uni.getSystemInfoSync()
|
||
uni.setStorageSync('systemInfo', {
|
||
platform: systemInfo.platform,
|
||
system: systemInfo.system,
|
||
osName: systemInfo.osName,
|
||
osVersion: systemInfo.osVersion,
|
||
statusBarHeight: systemInfo.statusBarHeight,
|
||
windowWidth: systemInfo.windowWidth,
|
||
windowHeight: systemInfo.windowHeight,
|
||
isIOS: systemInfo.platform === 'ios',
|
||
isAndroid: systemInfo.platform === 'android'
|
||
})
|
||
|
||
// 2. 立即设置宿主消息监听(避免错过消息)
|
||
this.setupNativeEventListener()
|
||
|
||
// 3. 同步初始化配置(必须完成)
|
||
this.initConfig(options)
|
||
|
||
// 启动完成
|
||
console.log(`=== App 启动完成,耗时: ${Date.now() - startTime}ms ===`)
|
||
|
||
// 初始化埋点,进入支付宝模拟器首页
|
||
this.$apiUserEvent('all', {
|
||
type: 'event',
|
||
key: 'index',
|
||
value: "进入支付宝模拟器首页",
|
||
})
|
||
},
|
||
|
||
onShow: function () {
|
||
console.log('App Show')
|
||
// 监听已在 onLaunch 中设置,这里不再重复
|
||
},
|
||
|
||
onHide: function () {
|
||
console.log('App Hide')
|
||
},
|
||
onExit: function () {
|
||
// 关闭数据库
|
||
// #ifdef APP
|
||
// plus.sqlite.closeDatabase({ name: 'zyds' })
|
||
uni.sendNativeEvent('unimp_stop_alipay', "stop", ret => {
|
||
// console.log('宿主App回传的数据:' + ret);
|
||
});
|
||
// #endif
|
||
console.log('App onExit')
|
||
},
|
||
|
||
methods: {
|
||
/**
|
||
* 设置宿主消息监听(在 onLaunch 中调用)
|
||
*/
|
||
setupNativeEventListener() {
|
||
if (this.globalData.NativeEvent) {
|
||
this.globalData.NativeEvent = false
|
||
console.log('开始监听宿主消息')
|
||
|
||
uni.onNativeEventReceive((event, data) => {
|
||
if (event) {
|
||
console.log('接收到宿主消息:', event, data)
|
||
|
||
if (event == "token") {
|
||
let header = uni.getStorageSync('header') || {}
|
||
header["x-token"] = data
|
||
uni.setStorageSync('header', header)
|
||
console.log('已更新 token')
|
||
|
||
//获取宿主用户信息
|
||
try {
|
||
this.$getUserInfo()
|
||
} catch (error) {
|
||
console.error('获取用户信息失败:', error)
|
||
}
|
||
} else if (event == "jump") {
|
||
if (data) {
|
||
let pages = getCurrentPages();
|
||
let currentPage = pages[pages.length - 1];
|
||
let currentUrl = currentPage.route;
|
||
if (currentUrl != data) {
|
||
uni.navigateTo({
|
||
url: '/' + data
|
||
});
|
||
}
|
||
}
|
||
} else if (event == 'wx_pay_result' || event == 'ios_pay_result') {
|
||
this.globalData.recentNativeEvent = event
|
||
this.globalData.recentNativeData = data
|
||
}
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 初始化应用配置
|
||
*/
|
||
initConfig(options) {
|
||
console.log('=== 配置初始化 ===')
|
||
|
||
// 检查是否有外部传入的配置数据
|
||
const hasExtraData = options?.referrerInfo?.extraData &&
|
||
JSON.stringify(options.referrerInfo.extraData) !== '{}'
|
||
|
||
console.log('宿主是否传递配置:', hasExtraData)
|
||
|
||
if (hasExtraData) {
|
||
console.log('→ 使用宿主 extraData')
|
||
this.initFromExtraData(options.referrerInfo.extraData)
|
||
} else {
|
||
console.log('→ 宿主未传递配置,使用默认配置')
|
||
// ⚠️ 关键修改:不再强制退出,使用默认配置
|
||
this.initDevelopmentConfig()
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 从外部数据初始化配置
|
||
*/
|
||
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])
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 初始化开发环境配置(也作为默认配置)
|
||
*/
|
||
initDevelopmentConfig() {
|
||
console.log('初始化默认配置')
|
||
|
||
// 批量设置开发环境配置
|
||
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])
|
||
})
|
||
|
||
console.log('默认配置初始化完成')
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style>
|
||
@import "./common/color.css";
|
||
</style> |