公众号授权成功!!!!!!!!
This commit is contained in:
48
index.html
48
index.html
@@ -13,7 +13,7 @@
|
||||
<!-- </script> -->
|
||||
<script src="./marked.min.js"></script>
|
||||
|
||||
<!-- <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script> -->
|
||||
<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
|
||||
|
||||
<style>
|
||||
* {
|
||||
@@ -1376,12 +1376,12 @@
|
||||
|
||||
</div>
|
||||
|
||||
<!-- <script>
|
||||
<script>
|
||||
// 开启 vConsole 调试面板
|
||||
var vConsole = new VConsole();
|
||||
|
||||
console.log("✅ vConsole 已经启动");
|
||||
</script> -->
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
@@ -1411,14 +1411,28 @@
|
||||
const encodedUserId = params.get("userId");
|
||||
const from = params.get("from");
|
||||
const code = params.get("code");
|
||||
if (code) {
|
||||
console.log(code, "code")
|
||||
// console.log(window.location);
|
||||
fetch(`/api/wechat/oauth/access-token?code=${code}`).then(res => res.json()).then(res => {
|
||||
console.log(res, '获取accesstoken')
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
function handleCode(code) {
|
||||
if (code) {
|
||||
console.log(code, "code");
|
||||
fetch(`https://servicestream.satabot.com/api/wechat/access_token?code=${code}`)
|
||||
.then(res => res.json())
|
||||
.then(res => {
|
||||
console.log(res.nickname)
|
||||
document.querySelectorAll('.username').forEach((e) => {
|
||||
e.textContent = res.nickname;
|
||||
})
|
||||
openId = res.openid;
|
||||
userId = res.openid;
|
||||
getUserByOpenId(userId)
|
||||
|
||||
});
|
||||
} else {
|
||||
return; // ✅ 没问题,直接退出函数
|
||||
}
|
||||
}
|
||||
const appid = "wx364b8ebf17fe45fe";
|
||||
// 回调地址(必须和公众号后台配置的网页授权域名一致)
|
||||
const redirect_uri = encodeURIComponent("http://ai.satabot.com");
|
||||
@@ -1430,16 +1444,21 @@
|
||||
|
||||
|
||||
|
||||
|
||||
// ✅ 先 URL 解码,再 Base64 解码,再 UTF-8 解码
|
||||
let userId;
|
||||
if (from == "weixin") {
|
||||
userId = encodedUserId;
|
||||
openId = encodedUserId;
|
||||
setCookie('openId', openId)
|
||||
document.querySelector('.userinfo-panel .logout').style.display = 'none';
|
||||
document.querySelectorAll('.logout').forEach(el => {
|
||||
el.style.display = 'none';
|
||||
});
|
||||
} else {
|
||||
userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
|
||||
}
|
||||
// 微信授权的码
|
||||
handleCode(code);
|
||||
if (userId && userId != "NaN") {
|
||||
setCookie("userId", userId);
|
||||
}
|
||||
@@ -1475,7 +1494,8 @@
|
||||
// }
|
||||
|
||||
// } else {
|
||||
if (getCookie("userId") && getCookie("userId") != "NaN") {
|
||||
if (getCookie("userId") && getCookie("userId") != "NaN" && !code) {
|
||||
|
||||
if (from == "weixin") {
|
||||
getUserByOpenId(userId)
|
||||
} else {
|
||||
@@ -1483,6 +1503,7 @@
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// deleteCookie('userId');
|
||||
|
||||
if (from == "weixin") {
|
||||
@@ -1769,6 +1790,7 @@
|
||||
const questions = document.querySelectorAll(".question");
|
||||
// 获取到用户名称
|
||||
function getUserName(userId) {
|
||||
if (code) return;
|
||||
if (from == "weixin") {
|
||||
document.querySelectorAll('.username').forEach((e) => {
|
||||
e.textContent = userId;
|
||||
@@ -2187,7 +2209,7 @@
|
||||
sessId = sendParam.sessionId;
|
||||
sendParam.imageBase64 = img;
|
||||
// sendParam.userId= encodedUserId;
|
||||
if (from != "weixin") {
|
||||
if (from != "weixin" && !code) {
|
||||
sendParam.userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
|
||||
} else {
|
||||
sendParam.userId = userId;
|
||||
|
||||
Reference in New Issue
Block a user