fix bugs
This commit is contained in:
@@ -49,6 +49,7 @@
|
||||
|
||||
<script>
|
||||
import userAPI from '@/api/user.js'
|
||||
import { getUserInfo as getStoredUserInfo, isLogin, clearAuth } from '@/utils/auth.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
@@ -62,13 +63,40 @@ export default {
|
||||
};
|
||||
},
|
||||
onLoad() {
|
||||
this.loadUserInfo()
|
||||
this.checkLoginAndLoadInfo()
|
||||
},
|
||||
onShow() {
|
||||
// 每次显示时刷新用户信息
|
||||
this.loadUserInfo()
|
||||
this.checkLoginAndLoadInfo()
|
||||
},
|
||||
methods: {
|
||||
/**
|
||||
* 检查登录状态并加载用户信息
|
||||
*/
|
||||
checkLoginAndLoadInfo() {
|
||||
if (!isLogin()) {
|
||||
// 未登录,跳转到登录页
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 先从本地存储加载用户信息
|
||||
const storedInfo = getStoredUserInfo()
|
||||
if (storedInfo) {
|
||||
this.userInfo = {
|
||||
name: storedInfo.userName || storedInfo.account || '用户',
|
||||
id: storedInfo.userId || '',
|
||||
phone: storedInfo.phone || '',
|
||||
username: storedInfo.account || ''
|
||||
}
|
||||
}
|
||||
|
||||
// 然后从服务器刷新用户信息
|
||||
this.loadUserInfo()
|
||||
},
|
||||
|
||||
/**
|
||||
* 加载用户信息
|
||||
*/
|
||||
@@ -84,7 +112,13 @@ export default {
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('加载用户信息失败:', err)
|
||||
// 失败时不显示错误提示,使用默认值
|
||||
// 如果是401错误,说明token过期,跳转到登录页
|
||||
if (err.statusCode === 401) {
|
||||
clearAuth()
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -109,30 +143,39 @@ export default {
|
||||
icon: 'none'
|
||||
});
|
||||
},
|
||||
handleLogout() {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '确定要退出登录吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
// 清除本地存储的token
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync('userInfo')
|
||||
async handleLogout() {
|
||||
const confirmRes = await new Promise((resolve) => {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '确定要退出登录吗?',
|
||||
success: (res) => resolve(res)
|
||||
})
|
||||
})
|
||||
|
||||
uni.showToast({
|
||||
title: '退出成功',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
// 延迟跳转到登录页
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
}, 1500)
|
||||
}
|
||||
if (confirmRes.confirm) {
|
||||
try {
|
||||
// 调用退出登录接口
|
||||
await userAPI.logout()
|
||||
} catch (err) {
|
||||
console.error('退出登录接口调用失败:', err)
|
||||
// 即使接口失败也继续清除本地数据
|
||||
}
|
||||
});
|
||||
|
||||
// 清除本地认证信息
|
||||
clearAuth()
|
||||
|
||||
uni.showToast({
|
||||
title: '退出成功',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
// 延迟跳转到登录页
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
}, 1500)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user