131 lines
No EOL
3.8 KiB
JavaScript
131 lines
No EOL
3.8 KiB
JavaScript
//app.js
|
||
const { syncTokens, showToast } = require('./utils/util');
|
||
const config = require('./utils/config');
|
||
const cloud = require('./utils/cloud');
|
||
|
||
App({
|
||
onLaunch: function (options) {
|
||
// 获取本地OTP信息
|
||
this.getLocalOtpInfo();
|
||
// 不再自动登录,只在用户点击登录按钮时才登录
|
||
},
|
||
|
||
getLocalOtpInfo() {
|
||
try {
|
||
const otpList = wx.getStorageSync('tokens');
|
||
if (otpList) {
|
||
this.globalData.otpList = otpList;
|
||
}
|
||
} catch (e) {
|
||
console.error("获取本地OTP信息失败:", e);
|
||
}
|
||
},
|
||
|
||
|
||
async userLogin(options) {
|
||
try {
|
||
// 获取微信登录凭证
|
||
const loginResult = await new Promise((resolve, reject) => {
|
||
wx.login({
|
||
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 response = await cloud.request(config.API_ENDPOINTS.AUTH.LOGIN, {
|
||
method: 'POST',
|
||
data: { code: loginResult.code },
|
||
header: {
|
||
'Content-Type': 'application/json'
|
||
}
|
||
});
|
||
|
||
// 检查响应数据
|
||
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 = {
|
||
avatarUrl: wx.getStorageSync('userAvatar') || '/images/default-avatar.png',
|
||
nickName: wx.getStorageSync('userNickName') || '微信用户'
|
||
};
|
||
this.globalData.userInfo = userInfo;
|
||
|
||
return userInfo;
|
||
} catch (error) {
|
||
console.error('登录失败:', error);
|
||
showToast('登录失败,请重试', 'none');
|
||
throw 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,
|
||
// 添加简单的事件发射器
|
||
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));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}) |