fix mine
This commit is contained in:
parent
17a02ea47e
commit
70e7a113e6
22 changed files with 967 additions and 285 deletions
|
@ -6,6 +6,8 @@ const {
|
|||
showLoading,
|
||||
hideLoading
|
||||
} = require('../../utils/util');
|
||||
const config = require('../../utils/config');
|
||||
const { hasValidTokens, clearAllTokens } = require('../../utils/cloud');
|
||||
|
||||
Page({
|
||||
/**
|
||||
|
@ -14,6 +16,7 @@ Page({
|
|||
data: {
|
||||
loading: false,
|
||||
uploading: false,
|
||||
clearing: false,
|
||||
isLoggedIn: false,
|
||||
userInfo: null,
|
||||
currentYear: new Date().getFullYear()
|
||||
|
@ -22,9 +25,27 @@ Page({
|
|||
onShow: function() {
|
||||
// 每次显示页面时检查登录状态
|
||||
const app = getApp();
|
||||
const isLoggedIn = hasValidTokens();
|
||||
|
||||
// 如果未登录,强制重置用户信息
|
||||
if (!isLoggedIn) {
|
||||
this.setData({
|
||||
isLoggedIn: false,
|
||||
userInfo: {
|
||||
avatarUrl: '/images/default-avatar.png',
|
||||
nickName: '微信用户'
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已登录,优先使用globalData中的用户信息
|
||||
this.setData({
|
||||
isLoggedIn: !!app.globalData.token,
|
||||
userInfo: app.globalData.userInfo
|
||||
isLoggedIn: true,
|
||||
userInfo: app.globalData.userInfo || {
|
||||
avatarUrl: '/images/default-avatar.png',
|
||||
nickName: '微信用户'
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -114,7 +135,10 @@ Page({
|
|||
showToast('数据恢复成功', 'success');
|
||||
|
||||
} catch (error) {
|
||||
console.error('数据恢复失败:', error);
|
||||
// 如果是404错误(云端无数据),不打印错误日志
|
||||
if (error.statusCode !== 404) {
|
||||
console.error('数据恢复失败:', error);
|
||||
}
|
||||
showToast('数据恢复失败,请重试');
|
||||
} finally {
|
||||
this.setData({ loading: false });
|
||||
|
@ -131,21 +155,33 @@ Page({
|
|||
try {
|
||||
showLoading('登录中...');
|
||||
const app = getApp();
|
||||
await app.userLogin();
|
||||
const userInfo = await app.userLogin();
|
||||
|
||||
// 更新登录状态,使用默认值
|
||||
// 使用hasValidTokens检查完整的登录状态
|
||||
const isLoggedIn = hasValidTokens();
|
||||
|
||||
if (!isLoggedIn) {
|
||||
throw new Error('登录失败:未能获取有效的访问令牌');
|
||||
}
|
||||
|
||||
// 检查是否有自定义设置
|
||||
const customAvatar = wx.getStorageSync('customAvatar');
|
||||
const customNickName = wx.getStorageSync('customNickName');
|
||||
|
||||
// 更新登录状态,优先使用自定义设置
|
||||
this.setData({
|
||||
isLoggedIn: !!app.globalData.token,
|
||||
isLoggedIn: true,
|
||||
userInfo: {
|
||||
avatarUrl: wx.getStorageSync('userAvatar') || '/images/default-avatar.png',
|
||||
nickName: wx.getStorageSync('userNickName') || '微信用户'
|
||||
...(userInfo || {}),
|
||||
avatarUrl: customAvatar || wx.getStorageSync('userAvatar') || '/images/default-avatar.png',
|
||||
nickName: customNickName || wx.getStorageSync('userNickName') || '微信用户'
|
||||
}
|
||||
});
|
||||
|
||||
showToast('登录成功', 'success');
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error);
|
||||
showToast('登录失败,请重试');
|
||||
showToast(error.message || '登录失败,请重试');
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
|
@ -164,12 +200,25 @@ Page({
|
|||
'userInfo.avatarUrl': avatarUrl
|
||||
});
|
||||
|
||||
// 保存到本地存储
|
||||
wx.setStorageSync('userAvatar', avatarUrl);
|
||||
// 使用异步存储操作
|
||||
await Promise.all([
|
||||
wx.setStorage({
|
||||
key: 'userAvatar',
|
||||
data: avatarUrl
|
||||
}),
|
||||
wx.setStorage({
|
||||
key: 'customAvatar',
|
||||
data: avatarUrl
|
||||
})
|
||||
]);
|
||||
|
||||
showToast('头像更新成功', 'success');
|
||||
} catch (error) {
|
||||
console.error('头像更新失败:', error);
|
||||
// 恢复默认头像
|
||||
this.setData({
|
||||
'userInfo.avatarUrl': '/images/default-avatar.png'
|
||||
});
|
||||
showToast('头像更新失败,请重试');
|
||||
} finally {
|
||||
hideLoading();
|
||||
|
@ -191,6 +240,8 @@ Page({
|
|||
|
||||
// 保存到本地存储
|
||||
wx.setStorageSync('userNickName', nickName);
|
||||
// 额外保存为自定义昵称
|
||||
wx.setStorageSync('customNickName', nickName);
|
||||
} catch (error) {
|
||||
console.error('昵称更新失败:', error);
|
||||
showToast('昵称更新失败,请重试');
|
||||
|
@ -204,6 +255,107 @@ Page({
|
|||
wx.stopPullDownRefresh();
|
||||
},
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
onLoad: function() {
|
||||
const app = getApp();
|
||||
// 监听全局logout事件
|
||||
app.globalData.eventEmitter.on('logout', this.handleLogout.bind(this));
|
||||
},
|
||||
|
||||
onUnload: function() {
|
||||
const app = getApp();
|
||||
// 移除事件监听
|
||||
if (app.globalData.eventEmitter && this.handleLogout) {
|
||||
app.globalData.eventEmitter.off('logout', this.handleLogout);
|
||||
}
|
||||
},
|
||||
|
||||
handleLogout: function() {
|
||||
// 重置页面数据
|
||||
this.setData({
|
||||
isLoggedIn: false,
|
||||
userInfo: {
|
||||
avatarUrl: '/images/default-avatar.png',
|
||||
nickName: '微信用户'
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 清空云端数据
|
||||
*/
|
||||
clearCloudData: async function() {
|
||||
// 检查登录状态
|
||||
if (!this.data.isLoggedIn) {
|
||||
showToast('请先登录');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 显示确认对话框
|
||||
const confirmed = await new Promise(resolve => {
|
||||
wx.showModal({
|
||||
title: '清空云端数据',
|
||||
content: '确定清空云端所有备份数据?此操作不可恢复!',
|
||||
confirmText: '确定',
|
||||
confirmColor: '#ff9c10',
|
||||
success: res => resolve(res.confirm)
|
||||
});
|
||||
});
|
||||
|
||||
if (!confirmed) return;
|
||||
|
||||
this.setData({ clearing: true });
|
||||
showLoading('正在清空...');
|
||||
|
||||
// 调用清空接口
|
||||
await clearAllTokens();
|
||||
|
||||
showToast('云端数据已清空', 'success');
|
||||
} catch (error) {
|
||||
console.error('清空云端数据失败:', error);
|
||||
showToast(error.message || '清空失败,请重试');
|
||||
} finally {
|
||||
this.setData({ clearing: false });
|
||||
hideLoading();
|
||||
}
|
||||
},
|
||||
|
||||
logout: async function() {
|
||||
try {
|
||||
// 显示确认对话框
|
||||
const confirmed = await new Promise(resolve => {
|
||||
wx.showModal({
|
||||
title: '退出登录',
|
||||
content: '确定要退出登录吗?',
|
||||
confirmText: '确定',
|
||||
confirmColor: '#ff9c10',
|
||||
success: res => resolve(res.confirm)
|
||||
});
|
||||
});
|
||||
|
||||
if (!confirmed) return;
|
||||
|
||||
showLoading('正在退出...');
|
||||
|
||||
// 调用全局logout方法
|
||||
const app = getApp();
|
||||
app.logout();
|
||||
|
||||
// 本地UI更新
|
||||
this.handleLogout();
|
||||
|
||||
showToast('已退出登录', 'success');
|
||||
} catch (error) {
|
||||
console.error('退出登录失败:', error);
|
||||
showToast('退出登录失败,请重试');
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 转发
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue