微信头像显示
This commit is contained in:
25
index.html
25
index.html
@@ -1648,18 +1648,18 @@
|
|||||||
|
|
||||||
// ✅ 先 URL 解码,再 Base64 解码,再 UTF-8 解码
|
// ✅ 先 URL 解码,再 Base64 解码,再 UTF-8 解码
|
||||||
let userId;
|
let userId;
|
||||||
if (params.get("nickname")) {
|
if (params.get("nickname") || getcookie("nickname")) {
|
||||||
console.log(1)
|
console.log(getcookie("headingimg"), 888888, document.cookie, params.get("nickname"))
|
||||||
|
|
||||||
document.querySelectorAll('.logout1').forEach(el => {
|
document.querySelectorAll('.logout1').forEach(el => {
|
||||||
el.style.display = 'none';
|
el.style.display = 'none';
|
||||||
});
|
});
|
||||||
console.log(3)
|
console.log(3)
|
||||||
document.querySelectorAll('.username').forEach((e) => {
|
document.querySelectorAll('.username').forEach((e) => {
|
||||||
e.textContent = params.get("nickname");
|
e.textContent = params.get("nickname") || getcookie("nickname");
|
||||||
})
|
})
|
||||||
|
|
||||||
document.querySelector(".userinfo-wrapper img").src = params.get("headingimg");
|
document.querySelector(".userinfo-wrapper img").src = getcookie("headimgurl") || params.get("headingimg");
|
||||||
userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
|
userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
|
||||||
console.log(4)
|
console.log(4)
|
||||||
|
|
||||||
@@ -1839,7 +1839,7 @@
|
|||||||
const questions = document.querySelectorAll(".question");
|
const questions = document.querySelectorAll(".question");
|
||||||
// 获取到用户名称
|
// 获取到用户名称
|
||||||
function getUserName(userId) {
|
function getUserName(userId) {
|
||||||
if (params.get("nickname")) return;
|
if (params.get("nickname") || getcookie("nickname")) return;
|
||||||
try {
|
try {
|
||||||
fetch("https://serviceai.satabot.com/user", {
|
fetch("https://serviceai.satabot.com/user", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -1913,6 +1913,13 @@
|
|||||||
const expires = "expires=" + d.toUTCString();
|
const expires = "expires=" + d.toUTCString();
|
||||||
document.cookie = `${name}=${encodeURIComponent(value)}; ${expires}; path=/`;
|
document.cookie = `${name}=${encodeURIComponent(value)}; ${expires}; path=/`;
|
||||||
}
|
}
|
||||||
|
function getcookie(name, cookieStr = document.cookie) {
|
||||||
|
const match = cookieStr.match(
|
||||||
|
new RegExp('(?:^|; )' + name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1') + '=([^;]*)')
|
||||||
|
);
|
||||||
|
return match ? decodeURIComponent(match[1]) : null;
|
||||||
|
}
|
||||||
|
|
||||||
function getCookie(name) {
|
function getCookie(name) {
|
||||||
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
|
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
|
||||||
if (match) return decodeURIComponent(match[2]);
|
if (match) return decodeURIComponent(match[2]);
|
||||||
@@ -2115,14 +2122,6 @@
|
|||||||
d.setFullYear(d.getFullYear() + 100); // +100 年
|
d.setFullYear(d.getFullYear() + 100); // +100 年
|
||||||
document.cookie = `${name}=${value};expires=${d.toUTCString()};path=/`;
|
document.cookie = `${name}=${value};expires=${d.toUTCString()};path=/`;
|
||||||
}
|
}
|
||||||
function getcookie(name) {
|
|
||||||
const cookies = document.cookie.split("; ");
|
|
||||||
for (let c of cookies) {
|
|
||||||
const [key, val] = c.split("=");
|
|
||||||
if (key === name) return val;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function showPop(userId) {
|
function showPop(userId) {
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
|
|||||||
25
server2.js
25
server2.js
@@ -543,12 +543,23 @@ app.get('/webCallback', async (req, res) => {
|
|||||||
if (loginRes.data.code === 200) {
|
if (loginRes.data.code === 200) {
|
||||||
// 登录成功,设置 token 并跳转
|
// 登录成功,设置 token 并跳转
|
||||||
const { token, userId, username } = loginRes.data;
|
const { token, userId, username } = loginRes.data;
|
||||||
res.cookie('Admin-Token', token, {
|
const cookieConfig = {
|
||||||
maxAge: 900000, httpOnly: true, sameSite: 'None', // 或 'Strict' / 'Lax',具体根据需要选择
|
maxAge: 7 * 24 * 60 * 60 * 1000, // Cookie有效期:7天(毫秒),可自行调整
|
||||||
});
|
httpOnly: false, // ☢️ 核心!必须为false,前端JS才能读取这个Cookie!!!
|
||||||
res.cookie('userId', userId, {
|
path: '/', // 浏览器的所有页面路径都能访问该Cookie
|
||||||
maxAge: 900000, httpOnly: true, sameSite: 'None', // 或 'Strict' / 'Lax',具体根据需要选择
|
domain: '.satabot.com', // ✅ 核心!所有子域名共享Cookie,前面必须加【点】
|
||||||
});
|
sameSite: 'Lax', // ✅ 推荐Lax,兼顾安全+前端读取,比None更安全
|
||||||
|
secure: true // ✅ 你的域名都是HTTPS,必须true,Cookie仅在HTTPS下生效
|
||||||
|
}
|
||||||
|
// 写入Token Cookie(前端读:Admin-Token)
|
||||||
|
res.cookie('Admin-Token', token, cookieConfig);
|
||||||
|
// 写入用户ID Cookie(前端读:userId)
|
||||||
|
res.cookie('userId', userId, cookieConfig);
|
||||||
|
// 可选:写入用户名Cookie(前端读:username)
|
||||||
|
res.cookie('username', username, cookieConfig);
|
||||||
|
// 可选:写入微信昵称/头像(前端可直接用,不用解析URL参数)
|
||||||
|
res.cookie('nickname', userInfo.nickname, cookieConfig);
|
||||||
|
res.cookie('headimgurl', userInfo.headimgurl, cookieConfig);
|
||||||
|
|
||||||
const encodedUserId = btoa(unescape(encodeURIComponent(userId)));
|
const encodedUserId = btoa(unescape(encodeURIComponent(userId)));
|
||||||
// 跳转到指定的页面
|
// 跳转到指定的页面
|
||||||
@@ -561,7 +572,7 @@ app.get('/webCallback', async (req, res) => {
|
|||||||
} else {
|
} else {
|
||||||
const encodedToken = btoa(unescape(encodeURIComponent(token)));
|
const encodedToken = btoa(unescape(encodeURIComponent(token)));
|
||||||
const encodedUsername = btoa(unescape(encodeURIComponent(username)));
|
const encodedUsername = btoa(unescape(encodeURIComponent(username)));
|
||||||
res.redirect(`http://ri.satabot.com/#/index?djjdbsjdwjhedbh=${encodedToken}&username=${encodedUsername}`);
|
res.redirect(`http://ri.satabot.com/#/index`);
|
||||||
}
|
}
|
||||||
// res.redirect(`https://ai.satabot.com?userId=${encodedUserId}&headingimg=${userInfo.headimgurl}&nickname=${userInfo.nickname}&localtoken=${token}&t=${t}`);
|
// res.redirect(`https://ai.satabot.com?userId=${encodedUserId}&headingimg=${userInfo.headimgurl}&nickname=${userInfo.nickname}&localtoken=${token}&t=${t}`);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user