103 lines
2.8 KiB
JavaScript
103 lines
2.8 KiB
JavaScript
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;
|
||
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,
|
||
})
|
||
}
|
||
},
|
||
fail: (res) => {
|
||
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,
|
||
});
|
||
},
|
||
});
|