83 lines
2.2 KiB
HTML
83 lines
2.2 KiB
HTML
|
|
<!DOCTYPE html>
|
|||
|
|
<html lang="zh-CN">
|
|||
|
|
|
|||
|
|
<head>
|
|||
|
|
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
|
|||
|
|
<meta charset="UTF-8">
|
|||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|||
|
|
<title>微信授权回调</title>
|
|||
|
|
<style>
|
|||
|
|
body {
|
|||
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
|
|||
|
|
padding: 20px;
|
|||
|
|
color: #333;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.url-display {
|
|||
|
|
word-break: break-all;
|
|||
|
|
background-color: #f0f0f0;
|
|||
|
|
padding: 10px;
|
|||
|
|
border-radius: 5px;
|
|||
|
|
margin-bottom: 15px;
|
|||
|
|
font-size: 14px;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.status {
|
|||
|
|
font-weight: bold;
|
|||
|
|
}
|
|||
|
|
</style>
|
|||
|
|
</head>
|
|||
|
|
|
|||
|
|
<body>
|
|||
|
|
|
|||
|
|
<h3>微信公众号授权回调页</h3>
|
|||
|
|
|
|||
|
|
<p>当前页面地址:</p>
|
|||
|
|
<div id="url-display" class="url-display"></div>
|
|||
|
|
|
|||
|
|
<p>状态:</p>
|
|||
|
|
<div id="status" class="status">正在处理...</div>
|
|||
|
|
|
|||
|
|
<script>
|
|||
|
|
(function () {
|
|||
|
|
const urlDisplay = document.getElementById('url-display');
|
|||
|
|
const statusDiv = document.getElementById('status');
|
|||
|
|
|
|||
|
|
// 显示当前地址
|
|||
|
|
urlDisplay.textContent = location.href;
|
|||
|
|
|
|||
|
|
// 解析 code
|
|||
|
|
const params = new URLSearchParams(location.search);
|
|||
|
|
const code = params.get('code');
|
|||
|
|
|
|||
|
|
if (!code) {
|
|||
|
|
statusDiv.textContent = '错误:URL中未找到 code 参数。';
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
statusDiv.textContent = '成功获取到 code,等待小程序环境注入...';
|
|||
|
|
|
|||
|
|
function sendCodeToMiniProgram() {
|
|||
|
|
if (!wx || !wx.miniProgram) {
|
|||
|
|
statusDiv.textContent = '错误:当前不在小程序 web-view 环境中,无法回传 code。';
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
statusDiv.textContent = '检测到小程序环境,正在回传 code...';
|
|||
|
|
wx.miniProgram.postMessage({ data: { mpCode: code } });
|
|||
|
|
setTimeout(function () {
|
|||
|
|
wx.miniProgram.navigateBack();
|
|||
|
|
}, 200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 等 JS-SDK/WeixinJSBridge 准备好再发
|
|||
|
|
if (typeof WeixinJSBridge !== 'undefined') {
|
|||
|
|
sendCodeToMiniProgram();
|
|||
|
|
} else {
|
|||
|
|
document.addEventListener('WeixinJSBridgeReady', sendCodeToMiniProgram, false);
|
|||
|
|
}
|
|||
|
|
})();
|
|||
|
|
</script>
|
|||
|
|
|
|||
|
|
</body>
|
|||
|
|
|
|||
|
|
</html>
|