jiaqingjiayi-xiaochengxu/甲情_甲意/miniprogram/pages/denglu/denglu.js
2024-12-17 19:46:10 +08:00

95 lines
2.6 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.

import {url} from '../request'
Page({
data: {
authCode: '',
intervalId: null, // 定时器ID用于后续清除
},
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,
},
success: (res) => {
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();
},
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,
});
},
});