824 lines
30 KiB
HTML
824 lines
30 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN" data-theme="light">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>OpenList Token 获取工具</title>
|
||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||
<style>
|
||
:root {
|
||
--bg-color-light: #ffffff;
|
||
--text-color-light: #000000;
|
||
--bg-color-dark: #1e1e2f;
|
||
--text-color-dark: #f0f0f0;
|
||
--accent-color: #00d1b2;
|
||
}
|
||
|
||
[data-theme="light"] body {
|
||
background-color: var(--bg-color-light);
|
||
color: var(--text-color-light);
|
||
}
|
||
|
||
[data-theme="dark"] body {
|
||
background-color: var(--bg-color-dark);
|
||
color: var(--text-color-dark);
|
||
}
|
||
|
||
.form-container {
|
||
max-width: 800px;
|
||
margin: 3rem auto;
|
||
padding: 2rem;
|
||
border-radius: 1rem;
|
||
box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
|
||
background: inherit;
|
||
backdrop-filter: blur(10px);
|
||
}
|
||
|
||
label {
|
||
font-weight: 600;
|
||
}
|
||
|
||
.form-control:read-only {
|
||
background-color: #f5f5f5;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.toggle-theme {
|
||
position: fixed;
|
||
top: 1rem;
|
||
right: 1rem;
|
||
}
|
||
|
||
a {
|
||
text-decoration: none;
|
||
}
|
||
|
||
[data-theme="dark"] body {
|
||
background-color: var(--bg-color-dark);
|
||
color: var(--text-color-dark);
|
||
}
|
||
|
||
[data-theme="dark"] .form-control {
|
||
background-color: #2a2a3b;
|
||
color: #f0f0f0;
|
||
border-color: #444;
|
||
}
|
||
|
||
[data-theme="dark"] .form-control:read-only {
|
||
background-color: #2a2a3b;
|
||
color: #ccc;
|
||
border-color: #444;
|
||
}
|
||
|
||
[data-theme="dark"] .form-select {
|
||
background-color: #2a2a3b;
|
||
color: #f0f0f0;
|
||
border-color: #444;
|
||
}
|
||
|
||
[data-theme="dark"] .btn {
|
||
background-color: #00bfa5;
|
||
color: #ffffff;
|
||
border: none;
|
||
}
|
||
|
||
[data-theme="dark"] .btn:hover {
|
||
background-color: #00a58e;
|
||
color: #ffffff;
|
||
}
|
||
|
||
[data-theme="dark"] p {
|
||
color: #ffffff;
|
||
}
|
||
|
||
/* 阿里云盘扫码v2样式 */
|
||
.qr-modal {
|
||
display: none;
|
||
position: fixed;
|
||
z-index: 1000;
|
||
left: 0;
|
||
top: 0;
|
||
width: 100%;
|
||
height: 100%;
|
||
background-color: rgba(0,0,0,0.5);
|
||
}
|
||
|
||
.qr-modal-content {
|
||
background-color: #fefefe;
|
||
margin: 5% auto;
|
||
padding: 20px;
|
||
border-radius: 10px;
|
||
width: 90%;
|
||
max-width: 500px;
|
||
text-align: center;
|
||
}
|
||
|
||
[data-theme="dark"] .qr-modal-content {
|
||
background-color: #2a2a3b;
|
||
color: #f0f0f0;
|
||
}
|
||
|
||
.qr-code-container {
|
||
margin: 20px 0;
|
||
padding: 20px;
|
||
background: #f8f9fa;
|
||
border-radius: 10px;
|
||
}
|
||
|
||
[data-theme="dark"] .qr-code-container {
|
||
background: #1a1a2e;
|
||
}
|
||
|
||
.qr-code-img {
|
||
max-width: 200px;
|
||
max-height: 200px;
|
||
border-radius: 8px;
|
||
}
|
||
|
||
.qr-status {
|
||
margin: 15px 0;
|
||
padding: 10px;
|
||
border-radius: 5px;
|
||
font-weight: 500;
|
||
}
|
||
|
||
.qr-status.waiting {
|
||
background: #fff3cd;
|
||
color: #856404;
|
||
border: 1px solid #ffeaa7;
|
||
}
|
||
|
||
.qr-status.scaned {
|
||
background: #d1ecf1;
|
||
color: #0c5460;
|
||
border: 1px solid #bee5eb;
|
||
}
|
||
|
||
.qr-status.success {
|
||
background: #d4edda;
|
||
color: #155724;
|
||
border: 1px solid #c3e6cb;
|
||
}
|
||
|
||
.qr-status.error {
|
||
background: #f8d7da;
|
||
color: #721c24;
|
||
border: 1px solid #f5c6cb;
|
||
}
|
||
|
||
[data-theme="dark"] .qr-status.waiting {
|
||
background: #3d3d00;
|
||
color: #ffeb3b;
|
||
border: 1px solid #ffeb3b;
|
||
}
|
||
|
||
[data-theme="dark"] .qr-status.scaned {
|
||
background: #003d4d;
|
||
color: #00bcd4;
|
||
border: 1px solid #00bcd4;
|
||
}
|
||
|
||
[data-theme="dark"] .qr-status.success {
|
||
background: #1b5e20;
|
||
color: #4caf50;
|
||
border: 1px solid #4caf50;
|
||
}
|
||
|
||
[data-theme="dark"] .qr-status.error {
|
||
background: #5d1a1a;
|
||
color: #f44336;
|
||
border: 1px solid #f44336;
|
||
}
|
||
|
||
.close-btn {
|
||
color: #aaa;
|
||
float: right;
|
||
font-size: 28px;
|
||
font-weight: bold;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.close-btn:hover {
|
||
color: black;
|
||
}
|
||
|
||
[data-theme="dark"] .close-btn:hover {
|
||
color: white;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="toggle-theme">
|
||
<button class="btn btn-outline-secondary" onclick="toggleTheme()">切换主题</button>
|
||
</div>
|
||
<div class="container form-container">
|
||
<h2 class="text-center mb-4">🔐 OpenList Token 获取工具</h2>
|
||
<div class="mb-3">
|
||
<label for="site-select" class="form-label">网盘名称</label>
|
||
<select id="site-select" class="form-select">
|
||
<!-- <option value="onedrive_pr" selected>OneDrive 个人账户</option>-->
|
||
<option value="onedrive_go" selected>OneDrive 企业版本</option>
|
||
<option value="onedrive_cn">OneDrive 世纪互联</option>
|
||
<option value="onedrive_us">OneDrive 美国版本</option>
|
||
<option value="onedrive_de">OneDrive 德国版本</option>
|
||
<option value="alicloud_qr">阿里网盘 扫码登录</option>
|
||
<option value="alicloud_qr2">阿里云盘 扫码登录v2</option>
|
||
<option value="baiduyun_go">百度网盘 验证登录</option>
|
||
<option value="115cloud_go">115 网盘 验证登录</option>
|
||
<option value="123cloud_go">123 网盘 直接登录</option>
|
||
<option value="googleui_go">Google Drive Team</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="mb-3" style="margin-top: 15px">
|
||
<input type="checkbox" id="server_use" class="form-check-input">
|
||
<label for="server_use" class="form-check-label">使用 OpenList 提供的API</label>
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="client-id" class="form-label">客户端ID</label>
|
||
<input type="text" id="client-id" class="form-control">
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="app-secret" class="form-label">应用机密</label>
|
||
<input type="text" id="app-secret" class="form-control">
|
||
</div>
|
||
|
||
<div class="mb-3" id="secret-key-view">
|
||
<label for="secret-key" class="form-label">访问密钥(SecretKey)</label>
|
||
<input type="text" id="secret-key" class="form-control">
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="callback-url" class="form-label">回调地址</label>
|
||
<input type="text" id="callback-url" class="form-control" value="https://api.oplist.org/onedrive/callback"
|
||
readonly
|
||
onclick="autoCopy(this)">
|
||
</div>
|
||
|
||
<div class="d-grid gap-2 mb-3">
|
||
<button class="btn btn-primary" onclick="getLogin()">获取 Token</button>
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="access-token" class="form-label">访问秘钥</label>
|
||
<textarea id="access-token" class="form-control" rows="3" readonly onclick="autoCopy(this)"></textarea>
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="refresh-token" class="form-label">刷新秘钥</label>
|
||
<textarea id="refresh-token" class="form-control" rows="3" readonly onclick="autoCopy(this)"></textarea>
|
||
</div>
|
||
|
||
<div class="text-muted text-center">
|
||
<p style="text-align:center">
|
||
本工具所有信息只以Cookie形式存储于浏览器本地<br>
|
||
开源于 <a href="https://github.com/OpenListTeam/cf-worker-api" target="_blank">GitHub</a> | By <a
|
||
href="https://github.com/OpenListTeam"
|
||
target="_blank">OpenListTeam</a>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 阿里云盘扫码v2模态框 -->
|
||
<div id="qr-modal" class="qr-modal">
|
||
<div class="qr-modal-content">
|
||
<span class="close-btn" onclick="closeQRModal()">×</span>
|
||
<h4>阿里云盘扫码登录v2</h4>
|
||
<div id="qr-code-container" class="qr-code-container" style="display: none;">
|
||
<div id="qr-code-display"></div>
|
||
</div>
|
||
<div id="qr-status" class="qr-status" style="display: none;"></div>
|
||
<div class="mt-3">
|
||
<button id="refresh-qr-btn" class="btn btn-secondary" onclick="refreshQRCode()" style="display: none;">刷新二维码</button>
|
||
<button class="btn btn-secondary" onclick="closeQRModal()">关闭</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
let intervalId;
|
||
|
||
// 获取登录秘钥 #######################################################
|
||
async function getLogin() {
|
||
let server_use = document.getElementById("server_use").checked;
|
||
let apps_uuid = document.getElementById("client-id").value;
|
||
let apps_keys = document.getElementById("app-secret").value;
|
||
let apps_type = document.getElementById("site-select").value;
|
||
let secret_key = document.getElementById("secret-key").value;
|
||
console.log(server_use);
|
||
// 阿里云盘扫码登录v2不需要验证客户端ID和应用机密
|
||
if (apps_type !== "alicloud_qr2" && !server_use && (apps_uuid === "" || apps_keys === "")) {
|
||
Swal.fire({
|
||
position: 'top',
|
||
icon: 'info',
|
||
title: '获取失败',
|
||
text: '请先填写客户端ID和应用机密',
|
||
showConfirmButton: true,
|
||
});
|
||
return;
|
||
}
|
||
// 阿里云盘扫码v2直接调用专用API,不需要构建传统的requests路径
|
||
if (apps_type === "alicloud_qr2") {
|
||
await startAlicloud2Login();
|
||
return;
|
||
}
|
||
|
||
let apps_subs = apps_type.split("_")[0]
|
||
let post_urls = "/" + apps_subs + "/requests?client_uid=" + apps_uuid
|
||
+ "&client_key=" + apps_keys + "&apps_types=" + apps_type
|
||
+ "&server_use=" + server_use
|
||
if (apps_subs === "baiduyun") post_urls += "&secret_key=" + secret_key
|
||
try {
|
||
const response = await fetch(post_urls, {
|
||
method: 'GET', headers: {'Content-Type': 'application/json'}
|
||
});
|
||
// 解析响应内容 ===============================================
|
||
const response_data = await response.json();
|
||
if (response.status === 200) {
|
||
if (apps_subs === "onedrive" || apps_subs === "115cloud"
|
||
|| apps_subs === "baiduyun" || apps_subs === "googleui") {
|
||
window.location.href = response_data.text;
|
||
}
|
||
if (apps_subs === "123cloud") {
|
||
document.getElementById("access-token").value = response_data.text;
|
||
return;
|
||
}
|
||
if (apps_type === "alicloud_qr") {
|
||
let sid = response_data.sid;
|
||
await Swal.fire({
|
||
position: 'top',
|
||
icon: 'info',
|
||
title: '扫码登录',
|
||
html: `<div>请扫码登录,完成后点确定</div>` +
|
||
`<img src="${response_data.text}" alt="">`,
|
||
showConfirmButton: true
|
||
});
|
||
post_urls = "/alicloud/callback" +
|
||
"?client_id=" + apps_uuid +
|
||
"&client_secret=" + apps_keys +
|
||
"&grant_type=" + "authorization_code" +
|
||
"&code=" + sid
|
||
let auth_post = await fetch(post_urls, {method: 'GET'});
|
||
let auth_data = await auth_post.json();
|
||
if (auth_post.status === 200) {
|
||
window.location.href = `/?access_token=${auth_data.access_token}`
|
||
+ `&refresh_token=${auth_data.refresh_token}`
|
||
+ `&client_uid=${apps_uuid}`
|
||
+ `&client_key=${apps_keys}`;
|
||
}
|
||
}
|
||
|
||
} else Swal.fire({
|
||
icon: 'error',
|
||
title: "获取秘钥失败: " + response_data.text,
|
||
showConfirmButton: true,
|
||
timer: 1000
|
||
});
|
||
} catch (error) {
|
||
Swal.fire({
|
||
icon: 'error',
|
||
title: '获取秘钥失败: ' + error,
|
||
showConfirmButton: true,
|
||
timer: 1000
|
||
});
|
||
}
|
||
}
|
||
|
||
// 自动复制内容 #####################################################
|
||
function autoCopy(on_element) {
|
||
// if (on_element.innerText === "") return;
|
||
navigator.clipboard.writeText(on_element.value).then(() => {
|
||
// 显示复制成功的提示
|
||
Swal.fire({
|
||
position: 'top',
|
||
icon: 'success',
|
||
title: '内容已复制',
|
||
showConfirmButton: false,
|
||
timer: 700
|
||
});
|
||
}).catch(err => {
|
||
// 复制失败时的处理
|
||
console.error('无法复制文本:', err);
|
||
Swal.fire({
|
||
position: 'top',
|
||
icon: 'error',
|
||
title: '复制失败',
|
||
text: '请手动复制',
|
||
showConfirmButton: true,
|
||
});
|
||
})
|
||
}
|
||
|
||
async function getToken() {
|
||
const strSearch = window.location.search;
|
||
const urlParams = new URLSearchParams(strSearch);
|
||
const server_use = urlParams.get("server_use");
|
||
const client_uid = urlParams.get("client_uid");
|
||
const secret_key = urlParams.get("secret_key");
|
||
const driver_txt = urlParams.get("driver_txt");
|
||
const client_key = urlParams.get("client_key");
|
||
const access_token = urlParams.get("access_token");
|
||
const refresh_token = urlParams.get("refresh_token");
|
||
const message_err = urlParams.get("message_err");
|
||
document.getElementById("site-select").value = driver_txt;
|
||
if (!driver_txt || driver_txt === "")
|
||
document.getElementById("site-select").value = "onedrive_go";
|
||
document.getElementById("app-secret").value = client_key;
|
||
document.getElementById("client-id").value = client_uid;
|
||
document.getElementById("access-token").value = access_token;
|
||
document.getElementById("refresh-token").value = refresh_token;
|
||
if (secret_key)
|
||
document.getElementById("secret-key").value = secret_key;
|
||
// 获取select元素和输入框元素
|
||
const siteSelect = document.getElementById('site-select');
|
||
const callbackUrlInput = document.getElementById('callback-url');
|
||
|
||
// 监听select的变化
|
||
siteSelect.addEventListener('change', function () {
|
||
const selectedValue = this.value.split("_")[0]; // 获取选中的value
|
||
// 更新输入框的值
|
||
callbackUrlInput.value = `https://api.oplist.org/${selectedValue}/callback`;
|
||
document.getElementById('secret-key-view').hidden = true;
|
||
|
||
const clientIdInput = document.getElementById('client-id');
|
||
const appSecretInput = document.getElementById('app-secret');
|
||
const serverUseCheckbox = document.getElementById('server_use');
|
||
const clientIdContainer = clientIdInput.closest('.mb-3');
|
||
const appSecretContainer = appSecretInput.closest('.mb-3');
|
||
const serverUseContainer = serverUseCheckbox.closest('.mb-3');
|
||
const callbackContainer = callbackUrlInput.closest('.mb-3');
|
||
|
||
// 阿里云盘扫码登录v2不需要客户端ID、应用机密和回调地址
|
||
if (siteSelect.value === "alicloud_qr2") {
|
||
// 隐藏整个字段容器
|
||
clientIdContainer.style.display = 'none';
|
||
appSecretContainer.style.display = 'none';
|
||
serverUseContainer.style.display = 'none';
|
||
callbackContainer.style.display = 'none';
|
||
|
||
// 清空值
|
||
clientIdInput.value = '';
|
||
appSecretInput.value = '';
|
||
serverUseCheckbox.checked = false;
|
||
} else {
|
||
// 恢复显示
|
||
clientIdContainer.style.display = 'block';
|
||
appSecretContainer.style.display = 'block';
|
||
serverUseContainer.style.display = 'block';
|
||
callbackContainer.style.display = 'block';
|
||
|
||
// 恢复正常状态
|
||
if (!serverUseCheckbox.checked) {
|
||
clientIdInput.disabled = false;
|
||
appSecretInput.disabled = false;
|
||
}
|
||
clientIdInput.placeholder = '';
|
||
appSecretInput.placeholder = '';
|
||
serverUseCheckbox.disabled = false;
|
||
}
|
||
|
||
if (siteSelect.value === "baiduyun_go") {
|
||
document.getElementById('secret-key-view').hidden = false;
|
||
}
|
||
|
||
});
|
||
document.getElementById('server_use').addEventListener('change', function () {
|
||
const clientIdInput = document.getElementById('client-id');
|
||
const appSecretInput = document.getElementById('app-secret');
|
||
const secretKeyInput = document.getElementById('secret-key');
|
||
const server_flag = document.getElementById('server_use');
|
||
if ((siteSelect.value === "alicloud_qr"
|
||
|| siteSelect.value === "alicloud_qr2"
|
||
|| siteSelect.value === "123cloud_go"
|
||
|| siteSelect.value === "onedrive_cn"
|
||
|| siteSelect.value === "onedrive_us"
|
||
|| siteSelect.value === "onedrive_de"
|
||
) && server_flag.checked) {
|
||
server_flag.checked = false;
|
||
Swal.fire({
|
||
position: 'top',
|
||
icon: 'error',
|
||
title: '暂不支持',
|
||
html: "阿里云、123云盘、OneDrive非官方区域暂不支持使用官方密钥",
|
||
showConfirmButton: true,
|
||
});
|
||
return;
|
||
}
|
||
if (this.checked) {
|
||
// 禁用输入框并清空内容
|
||
clientIdInput.disabled = true;
|
||
clientIdInput.value = '';
|
||
appSecretInput.disabled = true;
|
||
appSecretInput.value = '';
|
||
secretKeyInput.disabled = true;
|
||
secretKeyInput.value = '';
|
||
} else {
|
||
// 启用输入框
|
||
clientIdInput.disabled = false;
|
||
appSecretInput.disabled = false;
|
||
secretKeyInput.disabled = false;
|
||
}
|
||
});
|
||
// 页面加载时初始化回调地址
|
||
window.onload = function () {
|
||
siteSelect.dispatchEvent(new Event('change'));
|
||
|
||
};
|
||
|
||
if (message_err) {
|
||
Swal.fire({
|
||
position: 'top',
|
||
icon: 'error',
|
||
title: '授权失败',
|
||
html: message_err,
|
||
showConfirmButton: true,
|
||
});
|
||
}
|
||
}
|
||
|
||
//手动切换主题模式
|
||
function toggleTheme() {
|
||
const html = document.documentElement;
|
||
const current = html.getAttribute("data-theme");
|
||
html.setAttribute("data-theme", current === "dark" ? "light" : "dark");
|
||
}
|
||
|
||
// 自动切换暗黑模式
|
||
(function () {
|
||
const hour = new Date().getHours();
|
||
if (hour < 6 || hour >= 18) document.documentElement.setAttribute("data-theme", "dark");
|
||
getToken();
|
||
})();
|
||
|
||
getToken();
|
||
|
||
// 阿里云盘扫码v2相关变量
|
||
let alicloud2SessionId = null;
|
||
let alicloud2CheckInterval = null;
|
||
let alicloud2StartTime = null;
|
||
let clientFingerprint = null;
|
||
|
||
// 生成客户端指纹
|
||
function generateClientFingerprint() {
|
||
if (clientFingerprint) return clientFingerprint;
|
||
|
||
const canvas = document.createElement('canvas');
|
||
const ctx = canvas.getContext('2d');
|
||
ctx.textBaseline = 'top';
|
||
ctx.font = '14px Arial';
|
||
ctx.fillText('Client fingerprint', 2, 2);
|
||
|
||
const fingerprint = [
|
||
navigator.userAgent,
|
||
navigator.language,
|
||
screen.width + 'x' + screen.height,
|
||
new Date().getTimezoneOffset(),
|
||
canvas.toDataURL(),
|
||
navigator.hardwareConcurrency || 'unknown',
|
||
navigator.deviceMemory || 'unknown'
|
||
].join('|');
|
||
|
||
// 生成简单的哈希
|
||
let hash = 0;
|
||
for (let i = 0; i < fingerprint.length; i++) {
|
||
const char = fingerprint.charCodeAt(i);
|
||
hash = ((hash << 5) - hash) + char;
|
||
hash = hash & hash; // 转换为32位整数
|
||
}
|
||
|
||
clientFingerprint = Math.abs(hash).toString(36);
|
||
console.log('客户端指纹生成:', clientFingerprint);
|
||
return clientFingerprint;
|
||
}
|
||
|
||
// 发送带有客户端指纹的请求
|
||
async function fetchWithFingerprint(url, options = {}) {
|
||
const fingerprint = generateClientFingerprint();
|
||
const headers = {
|
||
'X-Client-Fingerprint': fingerprint,
|
||
...options.headers
|
||
};
|
||
|
||
return fetch(url, {
|
||
...options,
|
||
headers
|
||
});
|
||
}
|
||
|
||
// 启动阿里云盘扫码v2登录
|
||
async function startAlicloud2Login() {
|
||
try {
|
||
// 显示模态框
|
||
document.getElementById('qr-modal').style.display = 'block';
|
||
setQRStatus('正在生成二维码...', 'waiting');
|
||
|
||
// 生成二维码 - 使用带指纹的请求
|
||
const response = await fetchWithFingerprint('/alicloud2/generate_qr');
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
alicloud2SessionId = result.session_id;
|
||
alicloud2StartTime = Date.now();
|
||
showQRCode(result.qr_code_url);
|
||
setQRStatus('请使用阿里云盘App扫描二维码', 'waiting');
|
||
|
||
// 显示过期时间信息
|
||
if (result.expires_in) {
|
||
const expireMinutes = Math.floor(result.expires_in / 60);
|
||
console.log(`会话将在 ${expireMinutes} 分钟后过期`);
|
||
}
|
||
|
||
startStatusCheck();
|
||
} else {
|
||
setQRStatus(result.error || '生成二维码失败', 'error');
|
||
document.getElementById('refresh-qr-btn').style.display = 'inline-block';
|
||
}
|
||
} catch (error) {
|
||
setQRStatus('网络错误,请重试', 'error');
|
||
document.getElementById('refresh-qr-btn').style.display = 'inline-block';
|
||
console.error('生成二维码失败:', error);
|
||
}
|
||
}
|
||
|
||
// 显示二维码
|
||
function showQRCode(qrUrl) {
|
||
const qrApiUrl = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(qrUrl)}`;
|
||
document.getElementById('qr-code-display').innerHTML = `<img src="${qrApiUrl}" alt="二维码" class="qr-code-img">`;
|
||
document.getElementById('qr-code-container').style.display = 'block';
|
||
}
|
||
|
||
// 设置状态
|
||
function setQRStatus(message, type) {
|
||
const statusEl = document.getElementById('qr-status');
|
||
statusEl.textContent = message;
|
||
statusEl.className = `qr-status ${type}`;
|
||
statusEl.style.display = 'block';
|
||
}
|
||
|
||
// 开始状态检查
|
||
function startStatusCheck() {
|
||
stopStatusCheck();
|
||
alicloud2CheckInterval = setInterval(checkAlicloud2Status, 2000);
|
||
}
|
||
|
||
// 停止状态检查
|
||
function stopStatusCheck() {
|
||
if (alicloud2CheckInterval) {
|
||
clearInterval(alicloud2CheckInterval);
|
||
alicloud2CheckInterval = null;
|
||
}
|
||
}
|
||
|
||
// 检查登录状态
|
||
async function checkAlicloud2Status() {
|
||
if (!alicloud2SessionId) return;
|
||
|
||
// 检查是否超过3分钟(二维码可能过期)
|
||
const elapsed = Date.now() - alicloud2StartTime;
|
||
if (elapsed > 180000) { // 3分钟
|
||
setQRStatus('二维码可能已过期,建议点击刷新重新生成', 'error');
|
||
document.getElementById('refresh-qr-btn').style.display = 'inline-block';
|
||
stopStatusCheck();
|
||
return;
|
||
}
|
||
|
||
try {
|
||
// 使用带指纹的请求
|
||
const response = await fetchWithFingerprint(`/alicloud2/check_login?session_id=${alicloud2SessionId}`);
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
switch (result.status) {
|
||
case 'WAITING':
|
||
const waitTime = Math.floor(elapsed / 1000);
|
||
setQRStatus(`等待扫描... (${waitTime}s) 请使用阿里云盘App扫码`, 'waiting');
|
||
break;
|
||
case 'SCANED':
|
||
setQRStatus('已扫描,请在手机上确认登录', 'scaned');
|
||
break;
|
||
case 'CONFIRMED':
|
||
setQRStatus('登录成功!正在获取用户信息...', 'success');
|
||
stopStatusCheck();
|
||
// 稍等一下确保token已保存
|
||
setTimeout(async () => {
|
||
await getAlicloud2UserInfo();
|
||
}, 1000);
|
||
break;
|
||
case 'EXPIRED':
|
||
setQRStatus('二维码已过期,请点击刷新重新生成', 'error');
|
||
stopStatusCheck();
|
||
document.getElementById('refresh-qr-btn').style.display = 'inline-block';
|
||
break;
|
||
}
|
||
} else {
|
||
// 处理会话验证失败的情况
|
||
if (response.status === 403) {
|
||
setQRStatus('会话验证失败,请重新生成二维码', 'error');
|
||
document.getElementById('refresh-qr-btn').style.display = 'inline-block';
|
||
stopStatusCheck();
|
||
} else {
|
||
setQRStatus('检查状态失败: ' + (result.error || '未知错误'), 'error');
|
||
document.getElementById('refresh-qr-btn').style.display = 'inline-block';
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error('检查登录状态失败:', error);
|
||
setQRStatus('网络连接失败,请检查网络后重试', 'error');
|
||
document.getElementById('refresh-qr-btn').style.display = 'inline-block';
|
||
}
|
||
}
|
||
|
||
// 获取用户信息
|
||
async function getAlicloud2UserInfo() {
|
||
if (!alicloud2SessionId) return;
|
||
|
||
try {
|
||
// 使用带指纹的请求
|
||
const response = await fetchWithFingerprint(`/alicloud2/get_user_info?session_id=${alicloud2SessionId}`);
|
||
const result = await response.json();
|
||
|
||
if (result.success && result.user_info) {
|
||
// 关闭模态框
|
||
closeQRModal();
|
||
|
||
// 显示成功消息
|
||
await Swal.fire({
|
||
position: 'top',
|
||
icon: 'success',
|
||
title: '登录成功',
|
||
html: `<div>用户: ${result.user_info.nick_name || result.user_info.user_id}</div>`,
|
||
showConfirmButton: true
|
||
});
|
||
|
||
// 填充token字段(使用真实的tokens)
|
||
if (result.access_token) {
|
||
document.getElementById("access-token").value = result.access_token;
|
||
}
|
||
if (result.refresh_token) {
|
||
document.getElementById("refresh-token").value = result.refresh_token;
|
||
}
|
||
|
||
// 清理会话
|
||
await fetchWithFingerprint(`/alicloud2/logout?session_id=${alicloud2SessionId}`);
|
||
alicloud2SessionId = null;
|
||
} else {
|
||
// 处理会话验证失败的情况
|
||
if (response.status === 403) {
|
||
setQRStatus('会话验证失败,请重新登录', 'error');
|
||
} else {
|
||
setQRStatus('获取用户信息失败: ' + (result.error || '未知错误'), 'error');
|
||
}
|
||
}
|
||
} catch (error) {
|
||
setQRStatus('获取用户信息失败', 'error');
|
||
console.error('获取用户信息失败:', error);
|
||
}
|
||
}
|
||
|
||
// 刷新二维码
|
||
async function refreshQRCode() {
|
||
document.getElementById('refresh-qr-btn').style.display = 'none';
|
||
// 清理旧会话
|
||
if (alicloud2SessionId) {
|
||
try {
|
||
await fetchWithFingerprint(`/alicloud2/logout?session_id=${alicloud2SessionId}`);
|
||
} catch (e) {
|
||
console.log('清理旧会话失败:', e);
|
||
}
|
||
alicloud2SessionId = null;
|
||
}
|
||
await startAlicloud2Login();
|
||
}
|
||
|
||
// 关闭模态框
|
||
function closeQRModal() {
|
||
document.getElementById('qr-modal').style.display = 'none';
|
||
stopStatusCheck();
|
||
|
||
// 清理会话
|
||
if (alicloud2SessionId) {
|
||
fetchWithFingerprint(`/alicloud2/logout?session_id=${alicloud2SessionId}`);
|
||
alicloud2SessionId = null;
|
||
}
|
||
|
||
// 重置界面
|
||
document.getElementById('qr-code-container').style.display = 'none';
|
||
document.getElementById('qr-status').style.display = 'none';
|
||
document.getElementById('refresh-qr-btn').style.display = 'none';
|
||
}
|
||
|
||
// 点击模态框外部关闭
|
||
window.onclick = function(event) {
|
||
const modal = document.getElementById('qr-modal');
|
||
if (event.target === modal) {
|
||
closeQRModal();
|
||
}
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|