Skip to content

Let's use AI to Earn!

忘记密码和bili

daoyianb4272367093e
diff --git a/src/api/bilibili.ts b/src/api/bilibili.ts
index d8cd2927..21b862dd 100644
--- a/src/api/bilibili.ts
+++ b/src/api/bilibili.ts
@@ -10,3 +10,14 @@ export const apiGetBilibiliLoginUrl = (type: "h5" | "pc") => {
`plat/bilibili/auth/url/${type}`,
);
};
+
+/**
+ * 检查B站授权状态
+ * @param taskId 任务ID
+ * @returns
+ */
+export const apiCheckBilibiliAuth = (taskId: string) => {
+ return sxRequest.post<{ code: number; data: any }>(
+ `plat/bilibili/auth/create-account/${taskId}`,
+ );
+};
diff --git a/src/app/[lng]/accounts/plat/BilibiliLogin.ts b/src/app/[lng]/accounts/plat/BilibiliLogin.ts
index f394a11d..d2f13cf9 100644
--- a/src/app/[lng]/accounts/plat/BilibiliLogin.ts
+++ b/src/app/[lng]/accounts/plat/BilibiliLogin.ts
@@ -3,11 +3,22 @@ import { PlatType } from "@/app/config/platConfig";
import { requestPlatApi } from "@/utils/otherRequest";
import { KwaiPlat } from "@/app/plat/platChildren/kwai/KwaiPlat";
import { createOrUpdateAccountApi } from "@/api/account";
-import { apiGetBilibiliLoginUrl } from "@/api/bilibili";
+import { apiGetBilibiliLoginUrl, apiCheckBilibiliAuth } from "@/api/bilibili";
+import { message } from "antd";
export const kwaiAppId = "ks715790869885446758";
export const kwaiAppSecret = "cqSvJvBSPJjd-4pBH_4N0Q";
+interface BilibiliLoginData {
+ taskId: string;
+ url: string;
+}
+
+interface AuthResponse {
+ code: number;
+ data: any;
+}
+
/**
* b站被点击
* @param platType
@@ -15,20 +26,50 @@ export const kwaiAppSecret = "cqSvJvBSPJjd-4pBH_4N0Q";
export async function bilibiliSkip(platType: PlatType) {
if (platType !== PlatType.BILIBILI) return;
- const res = await bilibiliLogin();
- // 获取跳转地
- const url = res.url;
+ const res: any = await apiGetBilibiliLoginUrl('pc');
+ if (res?.code !== 0) return;
+ const url = res.data.url;
window.open(`${url}`);
+
+ const bilibiliLoginRes = await bilibiliLogin(res.data.taskId);
+
}
-export function bilibiliLogin(): Promise<any> {
+export function bilibiliLogin(taskId:any): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
- // TODO: 类型根据页面判断
- const res = await apiGetBilibiliLoginUrl('pc');
- console.log("-----bilibiliLogin--", res);
- resolve(res?.data);
+ // 开始轮询检查授权状态
+ const checkAuthStatus = async () => {
+ try {
+ const authRes = await apiCheckBilibiliAuth(taskId);
+ // if (authRes?.code === 0 && authRes?.data) {
+ // message.success('授权成功');
+ // resolve(authRes.data);
+ // return true;
+ // }
+ return false;
+ } catch (error) {
+ console.error('检查授权状态失败:', error);
+ return false;
+ }
+ };
+
+ // 设置轮询间隔
+ const interval = setInterval(async () => {
+ const isSuccess = await checkAuthStatus();
+ if (isSuccess) {
+ clearInterval(interval);
+ }
+ }, 2000);
+
+ // 5分钟后自动停止轮询
+ setTimeout(() => {
+ clearInterval(interval);
+ message.error('授权超时,请重试');
+ reject(new Error('授权超时'));
+ }, 5 * 60 * 1000);
+
} catch (e) {
reject(e);
}
diff --git a/src/app/[lng]/forgot-password/page.tsx b/src/app/[lng]/forgot-password/page.tsx
index 60d42e1b..02394885 100644
--- a/src/app/[lng]/forgot-password/page.tsx
+++ b/src/app/[lng]/forgot-password/page.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useState } from "react";
+import { useState, useEffect } from "react";
import { Form, Input, Button, message, Modal } from "antd";
import { useRouter } from "next/navigation";
import Link from "next/link";
@@ -14,8 +14,23 @@ export default function ForgotPasswordPage() {
const [isModalOpen, setIsModalOpen] = useState(false);
const [resetForm] = Form.useForm();
const [resetCode, setResetCode] = useState<string>('');
+ const [pollingInterval, setPollingInterval] = useState<NodeJS.Timeout | null>(null);
const [isPolling, setIsPolling] = useState(false);
- const [pollInterval, setPollInterval] = useState<NodeJS.Timeout | null>(null);
+
+ // 清除轮询
+ const clearPolling = () => {
+ if (pollingInterval) {
+ clearInterval(pollingInterval);
+ setPollingInterval(null);
+ }
+ setIsPolling(false);
+ };
+
+ // 处理Modal关闭
+ const handleModalClose = () => {
+ clearPolling();
+ setIsModalOpen(false);
+ };
// 处理发送重置密码邮件
const handleSubmit = async (values: { mail: string }) => {
@@ -40,6 +55,13 @@ export default function ForgotPasswordPage() {
}
};
+ // 组件卸载时清理轮询
+ useEffect(() => {
+ return () => {
+ clearPolling();
+ };
+ }, []);
+
// 处理重置密码
const handleResetPassword = async (values: { password: string }) => {
if (!resetCode) {
@@ -61,53 +83,64 @@ export default function ForgotPasswordPage() {
}
if (response.code === 0 && response.data?.token) {
+ clearPolling();
message.success('密码重置成功');
router.push('/login');
- } else {
- // 开始轮询检查用户是否点击邮箱链接
- const checkResetStatus = async () => {
- try {
- const statusResponse: any = await resetPasswordApi({
- code: resetCode,
- mail: form.getFieldValue('mail'),
- password: values.password
- });
-
- if (statusResponse?.code === 0 && statusResponse.data?.token) {
- message.success('密码重置成功');
- router.push('/login');
- return true;
- }
- return false;
- } catch (error) {
- return false;
- }
- };
-
- // 设置轮询间隔
- setIsPolling(true);
- const interval = setInterval(async () => {
- const isSuccess = await checkResetStatus();
- if (isSuccess) {
- if (pollInterval) {
- clearInterval(pollInterval);
- setPollInterval(null);
- }
- setIsPolling(false);
- }
- }, 2000);
- setPollInterval(interval);
-
- // 显示提示信息
- message.info('请点击邮件中的重置链接完成密码重置');
+ return;
}
+
+ // 开始轮询检查用户是否点击邮箱链接
+ setIsPolling(true);
+ const checkResetStatus = async () => {
+ try {
+ const statusResponse: any = await resetPasswordApi({
+ code: resetCode,
+ mail: form.getFieldValue('mail'),
+ password: values.password
+ });
+
+ if (statusResponse?.code === 0 && statusResponse.data?.token) {
+ clearPolling();
+ message.success('密码重置成功');
+ router.push('/login');
+ return true;
+ }
+ return false;
+ } catch (error) {
+ clearPolling();
+ return false;
+ }
+ };
+
+ // 设置轮询间隔
+ const interval = setInterval(async () => {
+ const isSuccess = await checkResetStatus();
+ if (isSuccess) {
+ clearPolling();
+ }
+ }, 2000);
+ setPollingInterval(interval);
+
+ // 显示提示信息
+ message.info('请点击邮件中的重置链接完成密码重置');
} catch (error) {
+ clearPolling();
message.error('重置失败');
} finally {
setLoading(false);
}
};
+ // 组件卸载时清理轮询
+ useEffect(() => {
+ return () => {
+ if (pollingInterval) {
+ clearInterval(pollingInterval);
+ }
+ setIsPolling(false);
+ };
+ }, [pollingInterval]);
+
return (
<div className={styles.container}>
<div className={styles.box}>
@@ -151,14 +184,7 @@ export default function ForgotPasswordPage() {
<Modal
title="重置密码"
open={isModalOpen}
- onCancel={() => {
- if (pollInterval) {
- clearInterval(pollInterval);
- setPollInterval(null);
- }
- setIsPolling(false);
- setIsModalOpen(false);
- }}
+ onCancel={handleModalClose}
maskClosable={false}
footer={null}
>
@@ -205,7 +231,7 @@ export default function ForgotPasswordPage() {
loading={loading || isPolling}
className={styles.submitButton}
>
- {isPolling ? '等待邮件确认...' : '确认重置'}
+ {isPolling ? '等待邮件确认中...' : '确认重置'}
</Button>
</Form.Item>
</Form>