121 lines
4.0 KiB
Markdown
121 lines
4.0 KiB
Markdown
## 微信 Code 授权
|
|
|
|
> index.html
|
|
```html
|
|
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>微信 Code 授权</title>
|
|
</head>
|
|
<body>
|
|
<h2>点击按钮获取微信 Code</h2>
|
|
<button id="getCodeBtn">获取 Code</button>
|
|
|
|
<script>
|
|
document.getElementById("getCodeBtn").addEventListener("click", function() {
|
|
// 获取授权页面的 URL
|
|
const appId = 'wx50611fcd613febd6'; // 替换为你的微信AppID
|
|
const redirectUri = encodeURIComponent('https://ekt1.suzhou.edu.cn/young-admin/wechat/wechat-callback.html'); // 替换为>你自己的回调URL
|
|
const wechatAuthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`;
|
|
|
|
// 跳转到微信授权页面
|
|
window.location.href = wechatAuthUrl;
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|
|
```
|
|
> wechat-callback.html
|
|
```html
|
|
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>微信授权回调</title>
|
|
</head>
|
|
<body>
|
|
<h2>授权成功!</h2>
|
|
<p id="codeDisplay">正在获取code...</p>
|
|
|
|
<script>
|
|
// 获取URL中的code参数
|
|
function getQueryVariable(variable) {
|
|
var query = window.location.search.substring(1);
|
|
var vars = query.split("&");
|
|
for (var i = 0; i < vars.length; i++) {
|
|
var pair = vars[i].split("=");
|
|
if (pair[0] === variable) {
|
|
return pair[1];
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 显示code
|
|
const code = getQueryVariable("code");
|
|
if (code) {
|
|
document.getElementById("codeDisplay").textContent = "授权码 (code): " + code;
|
|
} else {
|
|
document.getElementById("codeDisplay").textContent = "未获取到code";
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
```
|
|
|
|
> all in one get_code.html
|
|
```html
|
|
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>微信 Code 授权</title>
|
|
</head>
|
|
<body>
|
|
<h2 id="title">点击按钮获取微信 Code</h2>
|
|
<button id="getCodeBtn">获取 Code</button>
|
|
<p id="codeDisplay">正在获取code...</p>
|
|
|
|
<script>
|
|
// 获取授权页面的 URL
|
|
const appId = 'wx50611fcd613febd6'; // 替换为你的微信AppID
|
|
const redirectUri = encodeURIComponent('https://ekt1.suzhou.edu.cn/young-admin/wechat/get_code.html'); // 替换为你自己的回调URL
|
|
const wechatAuthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`;
|
|
|
|
// 处理按钮点击事件
|
|
document.getElementById("getCodeBtn").addEventListener("click", function() {
|
|
// 跳转到微信授权页面
|
|
window.location.href = wechatAuthUrl;
|
|
});
|
|
|
|
// 获取URL中的code参数
|
|
function getQueryVariable(variable) {
|
|
var query = window.location.search.substring(1);
|
|
var vars = query.split("&");
|
|
for (var i = 0; i < vars.length; i++) {
|
|
var pair = vars[i].split("=");
|
|
if (pair[0] === variable) {
|
|
return pair[1];
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 显示code
|
|
const code = getQueryVariable("code");
|
|
if (code) {
|
|
document.getElementById("title").textContent = "授权成功!";
|
|
document.getElementById("codeDisplay").textContent = "授权码 (code): " + code;
|
|
document.getElementById("getCodeBtn").style.display = 'none'; // 隐藏按钮
|
|
} else {
|
|
document.getElementById("codeDisplay").textContent = "未获取到code";
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|
|
``` |