Skip to content

Commit

Permalink
feat: 更新登录接口
Browse files Browse the repository at this point in the history
  • Loading branch information
orangelckc committed Dec 8, 2023
1 parent b1e2298 commit 2b94ffd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
12 changes: 4 additions & 8 deletions src/api/bilibili.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

Expand Down
2 changes: 1 addition & 1 deletion src/constants/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
52 changes: 25 additions & 27 deletions src/views/Login/qr-code.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
Expand All @@ -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
}
};
Expand All @@ -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();
};
Expand Down

0 comments on commit 2b94ffd

Please sign in to comment.