From 2b94ffdb1a3e1dd1659a64459378fb8b50526f03 Mon Sep 17 00:00:00 2001 From: orangelckc <276126767@qq.com> Date: Fri, 8 Dec 2023 15:04:51 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/bilibili.ts | 12 +++------ src/constants/url.ts | 2 +- src/views/Login/qr-code.vue | 52 ++++++++++++++++++------------------- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/api/bilibili.ts b/src/api/bilibili.ts index 83febad..374b1f2 100644 --- a/src/api/bilibili.ts +++ b/src/api/bilibili.ts @@ -6,18 +6,14 @@ import { isLogin } from "@/utils/auth"; // 获取登录url const getLoginUrlApi = async () => - await getQueryData(`${LOGIN_URL_PREFIX}/qrcode/getLoginUrl`, { + await getQueryData(`${LOGIN_URL_PREFIX}/qrcode/generate`, { returnError: true }); // 验证二维码是否被扫描 -const verifyQrCodeApi = async (oauthKey: string) => - await getQueryData(`${LOGIN_URL_PREFIX}/qrcode/getLoginInfo`, { - method: "POST", - body: Body.form({ - oauthKey, - gourl: "https://www.bilibili.com/" - }), +const verifyQrCodeApi = async (qrcode_key: string) => + await getQueryData(`${LOGIN_URL_PREFIX}/qrcode/poll`, { + query: { qrcode_key }, returnError: true }); diff --git a/src/constants/url.ts b/src/constants/url.ts index 980e1fc..c461d0f 100644 --- a/src/constants/url.ts +++ b/src/constants/url.ts @@ -2,7 +2,7 @@ const BASE_URL_PREFIX = "https://api.bilibili.com"; // 小破站登录 api 前缀 -const LOGIN_URL_PREFIX = "https://passport.bilibili.com"; +const LOGIN_URL_PREFIX = "https://passport.bilibili.com/x/passport-login/web"; // 小破站直播 api 前缀 const LIVE_URL_PREFIX = "https://api.live.bilibili.com"; diff --git a/src/views/Login/qr-code.vue b/src/views/Login/qr-code.vue index dbe51ea..16d73a9 100644 --- a/src/views/Login/qr-code.vue +++ b/src/views/Login/qr-code.vue @@ -7,6 +7,13 @@ import { getLoginUrlApi, verifyQrCodeApi } from "@/api"; import { LOGIN_INFO } from "@/constants"; import { setStore } from "@/store"; +const enum EQRCodeState { + '成功登陆' = 0, + '已失效' = 86038, + '未扫码' = 86101, + '已扫码未确认' = 86090, +} + // 二维码图片 const qrCodeImage = ref(); @@ -25,45 +32,37 @@ const getQRCode = async () => { return; } - const { oauthKey, url } = result; + const { qrcode_key, url } = result; qrCodeImage.value = await QRCode.toDataURL(url); - verifyQrCode(oauthKey); + verifyQrCode(qrcode_key); }; // 验证扫码信息 -const verifyQrCode = async (oauthKey: string) => { - const result = await verifyQrCodeApi(oauthKey); +const verifyQrCode = async (qrcode_key: string) => { + const result = await verifyQrCodeApi(qrcode_key); if (!result) { - setTimeout(() => verifyQrCode(oauthKey), 1000 * 3); + setTimeout(() => verifyQrCode(qrcode_key), 1000 * 3); return; } - switch (result) { - // 二维码已过期 - case -2: - qrCodeStatus.value = 2; - break; - - // 未扫码 - case -4: - setTimeout(() => verifyQrCode(oauthKey), 1000 * 3); - break; + switch (result.code) { + case EQRCodeState.已失效: + getQRCode() + break - // 已扫码,之后再次请求获取登录信息 - case -5: - qrCodeStatus.value = 1; + case EQRCodeState.未扫码: + setTimeout(() => verifyQrCode(qrcode_key), 1000 * 3) + break - setTimeout(() => verifyQrCode(oauthKey), 1000 * 3); - break; + case EQRCodeState.已扫码未确认: + setTimeout(() => verifyQrCode(qrcode_key), 1000 * 3) + break - // 扫码并登录 - default: - qrCodeStatus.value = 3; - - saveLoginInfo(result.url); - break; + case EQRCodeState.成功登陆: + saveLoginInfo(result.url) + break } }; @@ -74,7 +73,6 @@ const saveLoginInfo = async (data: string) => { await setStore(LOGIN_INFO.uid, DedeUserID!.toString()); await setStore(LOGIN_INFO.cookie, `SESSDATA=${SESSDATA}`); await setStore(LOGIN_INFO.csrf, bili_jct!.toString()); - window.location.reload(); };