Files
uni-fans-score/App.vue
T

123 lines
4.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script>
import {
wxLogin,
getUserInfo
} from './util/index'
export default {
onLaunch: function() {
console.log('App Launch')
// 注意:语言初始化已移至 main.js,确保每次 reLaunch 都能正确加载新语言
},
onShow: async function() {
console.log('========================================')
console.log('=== App onShow 被调用 ===')
console.log('时间戳:', new Date().toLocaleTimeString())
// 检查启动路径,如果设置了启动路径则跳转
try {
const launchPath = uni.getStorageSync('launchPath')
if (launchPath) {
console.log('检测到启动路径:', launchPath)
// 获取当前页面栈
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
const currentRoute = currentPage ? ('/' + currentPage.route) : ''
// 规范化路径格式进行比较
const normalizedLaunchPath = launchPath.replace(/\.html$/, '')
// 只在「单页面栈」时生效,避免影响正常多页面跳转(如下单后跳详情)
if (pages.length === 1 && currentRoute !== normalizedLaunchPath) {
console.log('当前页面:', currentRoute, '目标页面:', normalizedLaunchPath)
// 清除启动路径标记(在跳转前清除,避免重复触发)
uni.removeStorageSync('launchPath')
// 跳转到启动路径(首页)
// 先尝试 switchTab(如果首页是 tabBar 页面)
uni.reLaunch({
url: normalizedLaunchPath,
success: () => {
console.log('成功跳转到启动路径(switchTab')
},
fail: (err) => {
console.log('switchTab 失败,使用 reLaunch:', err)
// 如果 switchTab 失败,使用 reLaunch(适用于非 tabBar 页面)
uni.reLaunch({
url: normalizedLaunchPath,
success: () => {
console.log('成功跳转到启动路径(reLaunch')
},
fail: (reLaunchErr) => {
console.error('跳转到启动路径失败:', reLaunchErr)
}
})
}
})
} else if (currentRoute === normalizedLaunchPath) {
// 如果已经在目标页面,清除启动路径标记
console.log('当前已在目标页面,清除启动路径标记')
uni.removeStorageSync('launchPath')
}
}
} catch (e) {
console.error('App onShow - 启动路径检查失败:', e)
}
// 检查并更新语言(uni.reLaunch 会触发 onShow
try {
const savedLang = uni.getStorageSync('language')
console.log('App onShow - 缓存中的语言:', savedLang)
// 获取当前 i18n 实例并检查语言
if (this.$i18n) {
const currentLocale = this.$i18n.locale
console.log('App onShow - 当前 i18n locale:', currentLocale)
if (savedLang && savedLang !== currentLocale) {
console.log('=== App onShow 检测到语言变化 ===')
console.log('旧语言:', currentLocale)
console.log('新语言:', savedLang)
// 强制更新语言
this.$i18n.locale = savedLang
console.log('App onShow - 语言已更新为:', this.$i18n.locale)
console.log('App onShow - 测试翻译:', this.$t('common.loading'))
}
}
} catch (e) {
console.error('App onShow - 语言检查失败:', e)
}
console.log('========================================')
},
onHide: function() {
console.log('App Hide')
},
methods: {
// 保留方法但不调用
async autoLogin() {
try {
const loginResult = await wxLogin()
// await getUserInfo()
} catch (error) {
console.error('自动登录失败:', error)
}
},
async getUserInfoData() {
try {
await getUserInfo()
} catch (error) {
console.error('获取用户信息失败:', error)
// 获取用户信息失败的处理逻辑
}
}
}
}
</script>
<style lang="scss">
@import "@climblee/uv-ui/index.scss"
/*每个页面公共css */
</style>