appointment_system/App.vue

161 lines
3.3 KiB
Vue

<script>
import Config from '@/modules/Config.js'
export default {
globalData: {
token: '',
user: null,
shouldRefresh: false,
loginTime: 0,
config: null // 应用配置(从后端获取)
},
onLaunch: function() {
console.log('App Launch')
// 初始化全局数据
this.initGlobalData()
// 加载应用配置
this.loadAppConfig()
this.setTabBarText()
},
onShow: function() {
console.log('App Show')
this.setTabBarText()
},
onHide: function() {
console.log('App Hide')
},
methods: {
/**
* 初始化全局数据
*/
initGlobalData() {
try {
// 从本地存储恢复 token
const token = uni.getStorageSync('token')
if (token) {
this.globalData.token = token
}
// 从本地存储恢复用户信息
const userStr = uni.getStorageSync('user')
if (userStr) {
try {
this.globalData.user = JSON.parse(userStr)
} catch (e) {
console.error('解析用户信息失败:', e)
}
}
} catch (e) {
console.error('初始化全局数据失败:', e)
}
},
setTabBarText() {
// 动态设置 TabBar 文本
const pages = getCurrentPages()
if (pages.length > 0) {
const currentPage = pages[pages.length - 1]
// 只在 TabBar 页面才更新 TabBar 文本
const tabBarPages = ['pages/index/index', 'pages/appointment/appointment-page', 'pages/me/me-page']
const currentRoute = currentPage.route || ''
if (!tabBarPages.includes(currentRoute)) {
return // 非 TabBar 页面,不更新
}
if (currentPage.$vm && currentPage.$vm.$t) {
uni.setTabBarItem({
index: 0,
text: currentPage.$vm.$t('tabbar.home')
})
uni.setTabBarItem({
index: 1,
text: currentPage.$vm.$t('tabbar.appointment')
})
uni.setTabBarItem({
index: 2,
text: currentPage.$vm.$t('tabbar.me')
})
}
}
},
/**
* 加载应用配置
*/
async loadAppConfig() {
try {
const config = await Config.getPublicConfig()
if (config) {
this.globalData.config = config
console.log('应用配置已加载到globalData:', config)
}
} catch (error) {
console.error('加载应用配置失败:', error)
}
},
/**
* 获取配置项
* @param {String} key 配置键名
* @param {*} defaultValue 默认值
*/
getConfig(key, defaultValue = '') {
if (this.globalData.config && this.globalData.config[key]) {
return this.globalData.config[key]
}
return defaultValue
},
/**
* 获取配置图片的完整URL
* @param {String} key 配置键名
*/
getConfigImageUrl(key) {
const value = this.getConfig(key)
if (value) {
return Config.getImageUrl(value)
}
return ''
}
},
watch: {
'$i18n.locale'() {
this.setTabBarText()
}
}
}
</script>
<style>
/*每个页面公共css */
.bg {
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.column {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.center {
display: flex;
align-items: center;
justify-content: center;
}
.single-overflow {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: inline-block;
}
</style>