网页端微信
This commit is contained in:
51
index.html
51
index.html
@@ -9,6 +9,9 @@
|
||||
<meta http-equiv="Expires" content="Thu, 01 Jan 1970 00:00:00 GMT">
|
||||
<title>新能源智能体</title>
|
||||
<link rel="icon" href="./img/logo.png" type="image/x-icon">
|
||||
<script src=" https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxopensdk.js">
|
||||
</script>
|
||||
|
||||
<!-- <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script> -->
|
||||
|
||||
<style>
|
||||
@@ -230,7 +233,7 @@
|
||||
padding: 20px;
|
||||
overflow-y: auto;
|
||||
position: relative;
|
||||
height: 400px
|
||||
/* height: 400px */
|
||||
}
|
||||
|
||||
.message {
|
||||
@@ -1596,6 +1599,50 @@
|
||||
window.location.href = authUrl;
|
||||
|
||||
}
|
||||
// loginWithWeChat();
|
||||
// function loginWithWeChat() {
|
||||
// const appId = "wx364b8ebf17fe45fe";
|
||||
// const appSecret = "9b7476020991a2c71f1416da7f011d31";
|
||||
// const redirectUri = encodeURIComponent("https://www.example.com/wechat/callback");
|
||||
// const state = "login_state";
|
||||
// const scope = "snsapi_userinfo";
|
||||
|
||||
// const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&secret=${appSecret}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
|
||||
|
||||
// window.location.href = url; // 跳转到微信授权页面
|
||||
// }
|
||||
simulateWechatLogin()
|
||||
// 页面加载时立即执行:检查是否有微信返回的 code
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const code = params.get('code');
|
||||
const state = params.get('state');
|
||||
|
||||
if (code) {
|
||||
console.log('微信返回 code:', code);
|
||||
// 发送 code 到后端换取用户信息(这里才会执行)
|
||||
fetch('/api/wechat-login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ code })
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
console.log('微信用户信息:', data.userInfo);
|
||||
})
|
||||
.catch(err => console.error(err));
|
||||
}
|
||||
});
|
||||
|
||||
// 触发登录时执行:跳转微信授权页
|
||||
function simulateWechatLogin() {
|
||||
const APP_ID = 'wx0eeb7556d60121db';
|
||||
const REDIRECT_URI = encodeURIComponent("https://www.satabot.com");
|
||||
const STATE = Math.random().toString(36).substring(2, 12);
|
||||
const SCOPE = 'snsapi_login';
|
||||
const authUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${APP_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}&state=${STATE}#wechat_redirect`;
|
||||
window.location.href = authUrl; // 跳转后页面刷新,后续代码不执行
|
||||
}
|
||||
|
||||
// if(from=="weixin"){
|
||||
// gotoWeiXin();
|
||||
@@ -1623,7 +1670,7 @@
|
||||
console.log(data, 8777)
|
||||
if (data.code == 300) {
|
||||
// window.location.href='http://localhost/#/login?goRemote=1 '
|
||||
window.location.href = data.error;
|
||||
// window.location.href = data.error;
|
||||
return;
|
||||
}
|
||||
sessIdList = data.sessions;
|
||||
|
||||
Reference in New Issue
Block a user