86 lines
2.2 KiB
Vue
86 lines
2.2 KiB
Vue
<style lang="scss">
|
||
/* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
|
||
@import "@/uni_modules/uview-ui/index.scss";
|
||
</style>
|
||
<template>
|
||
<div>
|
||
<router-view />
|
||
</div>
|
||
</template>
|
||
|
||
<script>
|
||
export default {
|
||
onLaunch() {
|
||
console.log('App Launch')
|
||
// 检查登录状态
|
||
this.checkLoginStatus()
|
||
// 设置全局下拉刷新
|
||
this.setGlobalRefresh()
|
||
},
|
||
onShow() {
|
||
console.log('App Show')
|
||
// 切换到前台时检查登录状态
|
||
this.checkLoginStatus()
|
||
},
|
||
onHide() {
|
||
console.log('App Hide')
|
||
},
|
||
methods: {
|
||
// 设置全局下拉刷新
|
||
setGlobalRefresh() {
|
||
// 设置全局下拉刷新
|
||
uni.setBackgroundColor({
|
||
backgroundColor: '#F5F5F5' // 下拉背景色
|
||
})
|
||
|
||
// 设置下拉刷新的样式(仅支持iOS)
|
||
uni.setBackgroundTextStyle({
|
||
textStyle: 'dark' // 下拉loading的样式
|
||
})
|
||
},
|
||
|
||
// 全局下拉刷新触发
|
||
async onPullDownRefresh() {
|
||
try {
|
||
// 重新检查登录状态
|
||
await this.checkLoginStatus()
|
||
// 获取当前页面实例
|
||
const pages = getCurrentPages()
|
||
const currentPage = pages[pages.length - 1]
|
||
|
||
// 如果当前页面有自己的刷新方法,则调用
|
||
if (currentPage.$vm.refresh) {
|
||
await currentPage.$vm.refresh()
|
||
}
|
||
|
||
// 停止下拉刷新
|
||
uni.stopPullDownRefresh()
|
||
} catch (error) {
|
||
console.error('刷新失败:', error)
|
||
uni.stopPullDownRefresh()
|
||
}
|
||
},
|
||
|
||
async checkLoginStatus() {
|
||
try {
|
||
// 检查本地存储的登录状态
|
||
const isLoggedIn = await this.$store.dispatch('auth/checkLogin')
|
||
if (!isLoggedIn) {
|
||
const token = uni.getStorageSync('token')
|
||
const userInfo = uni.getStorageSync('userInfo')
|
||
if (token && userInfo) {
|
||
// 恢复登录状态
|
||
this.$store.commit('auth/SET_TOKEN', token)
|
||
this.$store.commit('auth/SET_USER_INFO', userInfo)
|
||
this.$store.commit('auth/SET_LOGIN_STATUS', true)
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error('检查登录状态失败:', error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|