fix mine
This commit is contained in:
parent
17a02ea47e
commit
70e7a113e6
22 changed files with 967 additions and 285 deletions
102
app.js
102
app.js
|
@ -25,24 +25,45 @@ App({
|
|||
async userLogin(options) {
|
||||
try {
|
||||
// 获取微信登录凭证
|
||||
const { code } = await new Promise((resolve, reject) => {
|
||||
const loginResult = await new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success: resolve,
|
||||
fail: reject
|
||||
success: (res) => {
|
||||
if (res.code) {
|
||||
resolve(res);
|
||||
} else {
|
||||
reject(new Error('获取微信登录凭证失败:' + (res.errMsg || '未知错误')));
|
||||
}
|
||||
},
|
||||
fail: (error) => reject(new Error('微信登录失败:' + (error.errMsg || '未知错误')))
|
||||
});
|
||||
});
|
||||
|
||||
if (!loginResult.code) {
|
||||
throw new Error('未获取到微信登录凭证');
|
||||
}
|
||||
|
||||
// 调用后端登录接口
|
||||
const loginResult = await cloud.request({
|
||||
url: config.API_ENDPOINTS.AUTH.LOGIN,
|
||||
const response = await cloud.request(config.API_ENDPOINTS.AUTH.LOGIN, {
|
||||
method: 'POST',
|
||||
data: { code },
|
||||
needToken: false
|
||||
data: { code: loginResult.code },
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
// 保存登录信息到storage
|
||||
wx.setStorageSync(config.JWT_CONFIG.storage.access, loginResult.accessToken);
|
||||
wx.setStorageSync(config.JWT_CONFIG.storage.refresh, loginResult.refreshToken);
|
||||
// 检查响应数据
|
||||
if (!response || typeof response !== 'object') {
|
||||
throw new Error('登录响应格式错误');
|
||||
}
|
||||
|
||||
// 检查并保存登录信息到storage
|
||||
if (!response.access_token || !response.refresh_token) {
|
||||
console.error('登录响应数据:', response);
|
||||
throw new Error('登录响应缺少必要的token信息');
|
||||
}
|
||||
|
||||
wx.setStorageSync(config.JWT_CONFIG.storage.access, response.access_token);
|
||||
wx.setStorageSync(config.JWT_CONFIG.storage.refresh, response.refresh_token);
|
||||
|
||||
// 初始化基本用户信息
|
||||
const userInfo = {
|
||||
|
@ -51,9 +72,6 @@ App({
|
|||
};
|
||||
this.globalData.userInfo = userInfo;
|
||||
|
||||
// 同步OTP数据
|
||||
await this.syncOtpData();
|
||||
|
||||
return userInfo;
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error);
|
||||
|
@ -62,28 +80,52 @@ App({
|
|||
}
|
||||
},
|
||||
|
||||
async syncOtpData() {
|
||||
try {
|
||||
// 获取本地数据
|
||||
const localTokens = this.globalData.otpList;
|
||||
|
||||
// 使用util.js中的syncTokens函数进行同步
|
||||
// 注意:syncTokens函数已经被更新为使用cloud模块
|
||||
const syncedTokens = await syncTokens(localTokens);
|
||||
|
||||
// 更新本地存储和全局数据
|
||||
wx.setStorageSync('tokens', syncedTokens);
|
||||
this.globalData.otpList = syncedTokens;
|
||||
|
||||
} catch (error) {
|
||||
console.error('同步OTP数据失败:', error);
|
||||
// 同步失败不显示错误提示,因为这是自动同步过程
|
||||
// 全局退出登录方法
|
||||
logout() {
|
||||
// 清除token相关storage数据(保留tokens存储)
|
||||
wx.removeStorageSync(config.JWT_CONFIG.storage.access);
|
||||
wx.removeStorageSync(config.JWT_CONFIG.storage.refresh);
|
||||
|
||||
// 保留用户自定义头像和昵称
|
||||
const customAvatar = wx.getStorageSync('customAvatar');
|
||||
const customNickName = wx.getStorageSync('customNickName');
|
||||
|
||||
// 清除其他用户数据
|
||||
wx.removeStorageSync('userAvatar');
|
||||
wx.removeStorageSync('userNickName');
|
||||
|
||||
// 恢复自定义设置
|
||||
if (customAvatar) wx.setStorageSync('userAvatar', customAvatar);
|
||||
if (customNickName) wx.setStorageSync('userNickName', customNickName);
|
||||
|
||||
// 重置全局数据(保留otpList)
|
||||
this.globalData.userInfo = null;
|
||||
|
||||
// 触发全局事件通知
|
||||
if (this.globalData.eventEmitter) {
|
||||
this.globalData.eventEmitter.emit('logout');
|
||||
}
|
||||
},
|
||||
|
||||
globalData: {
|
||||
version: 103,
|
||||
otpList: [],
|
||||
userInfo: null
|
||||
userInfo: null,
|
||||
// 添加简单的事件发射器
|
||||
eventEmitter: {
|
||||
listeners: {},
|
||||
on(event, callback) {
|
||||
if (!this.listeners[event]) {
|
||||
this.listeners[event] = [];
|
||||
}
|
||||
this.listeners[event].push(callback);
|
||||
},
|
||||
emit(event, ...args) {
|
||||
const callbacks = this.listeners[event];
|
||||
if (callbacks) {
|
||||
callbacks.forEach(cb => cb(...args));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue