This commit is contained in:
Ling53666
2025-08-18 09:11:51 +08:00
commit 02554225da
2516 changed files with 133155 additions and 0 deletions

View File

@ -0,0 +1,106 @@
import {url} from '../request'
Page({
data: {
authCode: '',
intervalId: null, // 定时器ID用于后续清除
sid:0
},
Login() {
my.getAuthCode({
scopes: 'auth_user',
success: res => {
const authcode = res.authCode;
console.log(typeof authcode);
console.log(authcode);
// 请求后端接口进行用户登录
my.request({
url: url + '/api/Alipay/parseCode',
data: {
authcode,
severId:this.data.sid
},
success: (res) => {
if(res.data.code==0){
const { username, avatarUrl, id } = res.data.data.userVO;
const setCookie = res.header['set-cookie'] || res.header['Set-Cookie'];
console.log('Set-Cookie:', setCookie + '这是这个码');
// 存储用户信息到本地存储
my.setStorage({
key: 'userInfo',
data: {
username: username,
avatarUrl: avatarUrl,
cookie: setCookie,
id: id,
timestamp: new Date().getTime(),
},
success: function () {
console.log('用户信息已存储');
},
fail: function (err) {
console.error('存储失败:', err);
}
});
// 启动定时器清除缓存
this.startClearingStorage();
// 登录成功后的处理逻辑
console.log(res);
my.alert({
title: '登录成功',
});
my.navigateBack();
}else{
this.setData({
sid:1,
})
console.log(this.data.sid);
}
},
fail: (res) => {
this.setData({
sid:1,
})
console.log("登录失败:", res);
}
});
}
});
},
// 启动定时器清除缓存
startClearingStorage() {
// 清除缓存定时器,每小时检查一次
const intervalId = setInterval(() => {
my.getStorage({
key: 'userInfo',
success: (res) => {
const userInfo = res.data;
if (userInfo) {
my.removeStorage({
key: 'userInfo',
success: function () {
console.log('用户信息已删除');
},
fail: function (err) {
console.error('删除失败:', err);
}
});
} else {
console.log('缓存中没有用户信息,停止定时器');
clearInterval(intervalId); // 清除定时器
}
},
});
}, 86400*1000); // 每小时检查一次单位是毫秒3600秒 = 1小时
// 保存定时器ID便于后续清除定时器
this.setData({
intervalId: intervalId,
});
},
});