jiaqingjiayi-xiaochengxu/甲情_甲意/miniprogram/pages/denglu/denglu.js

95 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-11-15 03:51:28 +00:00
import {url} from '../request'
2024-11-10 07:01:22 +00:00
Page({
2024-11-19 10:12:40 +00:00
data: {
authCode: '',
2024-12-17 11:46:10 +00:00
intervalId: null, // 定时器ID用于后续清除
2024-11-10 07:01:22 +00:00
},
2024-12-17 11:46:10 +00:00
2024-11-19 10:12:40 +00:00
Login() {
2024-11-10 07:01:22 +00:00
my.getAuthCode({
2024-11-15 03:51:28 +00:00
scopes: 'auth_user',
success: res => {
const authCode = res.authCode;
console.log(typeof authCode);
2024-11-19 10:12:40 +00:00
console.log(authCode);
2024-12-17 11:46:10 +00:00
2024-11-19 10:12:40 +00:00
// 请求后端接口进行用户登录
2024-11-15 03:51:28 +00:00
my.request({
2024-11-19 10:12:40 +00:00
url: url + '/api/Alipay/parseCode',
data: {
2024-11-15 03:51:28 +00:00
authCode,
},
2024-11-19 10:12:40 +00:00
success: (res) => {
2024-12-17 11:46:10 +00:00
const { username, avatarUrl, id } = res.data.data;
2024-12-10 10:35:22 +00:00
const setCookie = res.header['set-cookie'] || res.header['Set-Cookie'];
2024-12-17 11:46:10 +00:00
console.log('Set-Cookie:', setCookie + '这是这个码');
2024-11-19 10:12:40 +00:00
// 存储用户信息到本地存储
my.setStorage({
key: 'userInfo',
2024-11-15 03:51:28 +00:00
data: {
2024-11-19 10:12:40 +00:00
username: username,
avatarUrl: avatarUrl,
2024-12-17 11:46:10 +00:00
cookie: setCookie,
id: id,
timestamp: new Date().getTime(),
2024-11-15 03:51:28 +00:00
},
2024-11-19 10:12:40 +00:00
success: function () {
2024-12-17 11:46:10 +00:00
console.log('用户信息已存储');
2024-11-19 10:12:40 +00:00
},
fail: function (err) {
console.error('存储失败:', err);
}
2024-11-15 03:51:28 +00:00
});
2024-11-19 10:12:40 +00:00
2024-12-17 11:46:10 +00:00
// 启动定时器清除缓存
this.startClearingStorage();
2024-11-19 10:12:40 +00:00
// 登录成功后的处理逻辑
console.log(res);
2024-11-15 03:51:28 +00:00
my.alert({
title: '登录成功',
});
2024-11-19 10:12:40 +00:00
my.navigateBack();
},
fail: (res) => {
console.log("登录失败:", res);
2024-11-15 03:51:28 +00:00
}
2024-11-19 10:12:40 +00:00
});
2024-11-10 07:01:22 +00:00
}
2024-11-19 10:12:40 +00:00
});
2024-11-15 03:51:28 +00:00
},
2024-12-17 11:46:10 +00:00
// 启动定时器清除缓存
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,
});
},
});