otp/app.js
“xHuPo” 2b8870a40e init
2025-06-09 13:35:15 +08:00

89 lines
No EOL
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//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 { code } = await new Promise((resolve, reject) => {
wx.login({
success: resolve,
fail: reject
});
});
// 调用后端登录接口
const loginResult = await cloud.request({
url: config.API_ENDPOINTS.AUTH.LOGIN,
method: 'POST',
data: { code },
needToken: false
});
// 保存登录信息到storage
wx.setStorageSync(config.JWT_CONFIG.storage.access, loginResult.accessToken);
wx.setStorageSync(config.JWT_CONFIG.storage.refresh, loginResult.refreshToken);
// 初始化基本用户信息
const userInfo = {
avatarUrl: wx.getStorageSync('userAvatar') || '/images/default-avatar.png',
nickName: wx.getStorageSync('userNickName') || '微信用户'
};
this.globalData.userInfo = userInfo;
// 同步OTP数据
await this.syncOtpData();
return userInfo;
} catch (error) {
console.error('登录失败:', error);
showToast('登录失败,请重试', 'none');
throw error;
}
},
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);
// 同步失败不显示错误提示,因为这是自动同步过程
}
},
globalData: {
version: 103,
otpList: [],
userInfo: null
}
})