Skip to content

Let's use AI to Earn!

feat: 1. 添加账号代理流程添加。 2. 添加账号-小红书,扫码、手机号登录接入完成。

gaoSir055523130c39
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout/accountLoginLyaout.module.scss b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout/accountLoginLyaout.module.scss
index 7ce8b0a1..e7c8e039 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout/accountLoginLyaout.module.scss
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout/accountLoginLyaout.module.scss
@@ -1,6 +1,8 @@
.accountLoginLyaout {
- display: flex;
:global {
+ .ant-spin-container {
+ display: flex;
+ }
.accountLoginLyaout-right, .accountLoginLyaout-left {
height: 400px;
display: flex;
@@ -48,7 +50,7 @@
}
}
.accountLoginLyaout-right {
- padding: 40px 50px 0;
+ padding: 40px 40px 0;
border-left: 1px solid var(--grayColor2);
h2 {
font-size: var(--fs-lg);
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout/index.tsx b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout/index.tsx
index 9b986022..0e047b41 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout/index.tsx
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout/index.tsx
@@ -14,6 +14,8 @@ export interface IAccountLoginInfo {
export interface IAccountLoginLyaoutRef {}
export interface IAccountLoginLyaoutProps {
+ // 整体loading
+ loading?: boolean;
// 获取二维码loading
qRCodeLoading: boolean;
// 二维码是否过期
@@ -47,13 +49,14 @@ const AccountLoginLyaout = memo(
isQRCodeExpire = false,
qRCodeLoading,
onFlushQRCode,
+ loading = false,
}: IAccountLoginLyaoutProps,
ref: ForwardedRef<IAccountLoginLyaoutRef>,
) => {
const [form] = Form.useForm();
return (
- <div className={styles.accountLoginLyaout}>
+ <Spin wrapperClassName={styles.accountLoginLyaout} spinning={loading}>
<div className="accountLoginLyaout-left">
<Spin
spinning={qRCodeLoading}
@@ -128,7 +131,7 @@ const AccountLoginLyaout = memo(
</Form>
</div>
)}
- </div>
+ </Spin>
);
},
),
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.type.ts b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.type.ts
index 9574957b..f76b26ed 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.type.ts
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.type.ts
@@ -1,3 +1,6 @@
export interface IAccountLoginPlatProps {
+ // 登录成功
onLoginSuccess: (cookie: string) => void;
+ // 代理地址
+ proxy?: string;
}
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/index.tsx b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/index.tsx
index d18dc2d7..1f3d123a 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/index.tsx
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/index.tsx
@@ -1,12 +1,26 @@
"use client";
-import { ForwardedRef, forwardRef, memo, useMemo } from "react";
+import {
+ ForwardedRef,
+ forwardRef,
+ memo,
+ useEffect,
+ useMemo,
+ useState,
+} from "react";
import { AccountPlatInfoMap, PlatType } from "@/app/config/platConfig";
import DouyinLogin from "@/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/DouyinLogin";
import WxSphLogin from "@/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/WxSphLogin";
import XhsLogin from "@/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/XhsLogin";
-import { Modal } from "antd";
+import { Button, message, Modal, Steps, Switch, Tooltip } from "antd";
import KwaiLogin from "@/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/KwaiLogin";
+import styles from "./styles/accountLogin.module.scss";
+import { useShallow } from "zustand/react/shallow";
+import { useUserStore } from "@/store/user";
+import { QuestionCircleOutlined } from "@ant-design/icons";
+import ProxyInput, {
+ verifyProxy,
+} from "@/app/[lng]/accounts/components/ProxyInput";
export interface IAccountLoginRef {}
@@ -23,12 +37,31 @@ const AccountLogin = memo(
{ plat, open, onLoginSuccess, onCancel }: IAccountLoginProps,
ref: ForwardedRef<IAccountLoginRef>,
) => {
+ const { setIsAddAccountPorxy, isAddAccountPorxy } = useUserStore(
+ useShallow((state) => ({
+ setIsAddAccountPorxy: state.setIsAddAccountPorxy,
+ isAddAccountPorxy: state.isAddAccountPorxy,
+ })),
+ );
const platInfo = useMemo(() => {
return (
AccountPlatInfoMap.get(plat) ||
AccountPlatInfoMap.get(PlatType.Douyin)!
);
}, [plat]);
+ // 代理地址
+ const [proxy, setProxy] = useState("");
+ // 步骤
+ const [currStep, setCurrStep] = useState(0);
+ const [verifyProxyLoading, setVerifyProxyLoading] = useState(false);
+
+ useEffect(() => {
+ if (isAddAccountPorxy) {
+ setCurrStep(0);
+ } else {
+ setCurrStep(1);
+ }
+ }, [isAddAccountPorxy]);
const Plat = useMemo(() => {
switch (plat) {
@@ -46,17 +79,75 @@ const AccountLogin = memo(
return (
<>
<Modal
- title={`${platInfo.name}登录`}
+ title={
+ <div className={styles.ALModaltitle}>
+ <span className="ALModaltitle-title">{platInfo.name}登录</span>
+ <div className="ALModaltitle-switch">
+ <span>
+ 是否开启代理
+ <Tooltip title="开启后下次进入页面默认会为开启状态">
+ <QuestionCircleOutlined />
+ </Tooltip>
+ </span>
+ <Switch
+ value={isAddAccountPorxy}
+ onChange={(e) => setIsAddAccountPorxy(e)}
+ />
+ </div>
+ </div>
+ }
open={open}
onCancel={onCancel}
footer={null}
width={800}
>
- <Plat
- onLoginSuccess={() => {
- console.log("登录");
- }}
- />
+ {isAddAccountPorxy && (
+ <Steps
+ style={{ marginTop: "20px" }}
+ current={currStep}
+ items={[
+ {
+ title: "设置代理地址",
+ },
+ {
+ title: "登录",
+ },
+ ]}
+ />
+ )}
+
+ {currStep === 0 ? (
+ <>
+ <ProxyInput onChange={(e) => setProxy(e)} />
+ <div style={{ display: "flex", justifyContent: "right" }}>
+ <Button
+ type="primary"
+ style={{ width: "200px", padding: "16px 0" }}
+ onClick={async () => {
+ setVerifyProxyLoading(true);
+ const verifyRes = await verifyProxy(proxy);
+ setVerifyProxyLoading(false);
+ if (verifyRes) {
+ setCurrStep(1);
+ } else {
+ return message.error("代理地址不可用,无法添加账户!");
+ }
+ }}
+ >
+ 下一步
+ </Button>
+ </div>
+ </>
+ ) : (
+ <>
+ <Plat
+ proxy={isAddAccountPorxy ? proxy : ""}
+ onLoginSuccess={() => {
+ console.log("登录");
+ }}
+ />
+ </>
+ )}
</Modal>
</>
);
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/XhsLogin.tsx b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/XhsLogin.tsx
index 52af6249..6de5364a 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/XhsLogin.tsx
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/XhsLogin.tsx
@@ -1,58 +1,166 @@
"use client";
-import { ForwardedRef, forwardRef, memo, useEffect, useState } from "react";
+import {
+ ForwardedRef,
+ forwardRef,
+ memo,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
import { IAccountLoginPlatProps } from "@/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.type";
import styles from "../styles/xhsLogin.module.scss";
import { requestPlatApi } from "@/utils/otherRequest";
import AccountLoginLyaout from "@/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout";
+import { message } from "antd";
+import { sleep } from "@/utils/general";
export interface IXhsLoginRef {}
const XhsLogin = memo(
- forwardRef(({}: IAccountLoginPlatProps, ref: ForwardedRef<IXhsLoginRef>) => {
- const [qsImg, setQsImg] = useState("");
- const [qRCodeLoading, setQRCodeLoading] = useState(false);
- const [isQRCodeExpire, setIsQRCodeExpire] = useState(false);
- const [loginLoading, setLoginLoading] = useState(false);
-
- useEffect(() => {
- getLoginQRCode();
- }, []);
-
- const getLoginQRCode = () => {
- setQRCodeLoading(true);
- requestPlatApi({
- url: "xhs/login_qr_code",
- method: "POST",
- }).then((res) => {
+ forwardRef(
+ ({ proxy }: IAccountLoginPlatProps, ref: ForwardedRef<IXhsLoginRef>) => {
+ const [qsImg, setQsImg] = useState("");
+ const [qRCodeLoading, setQRCodeLoading] = useState(false);
+ const [isQRCodeExpire, setIsQRCodeExpire] = useState(false);
+ const [loginLoading, setLoginLoading] = useState(false);
+ const sendCodeResRef = useRef<any>(null);
+ // 停止轮询的方法
+ const stopPolling = useRef<() => void>(() => {});
+ const [getUserInfoLoding, setGetUserInfoLoding] = useState(false);
+
+ useEffect(() => {
+ getLoginQRCode();
+
+ return () => {
+ stopPolling.current();
+ };
+ }, []);
+
+ // 获取登录二维码
+ const getLoginQRCode = async () => {
+ stopPolling.current();
+
+ setQRCodeLoading(true);
+ const loginQrCodeRes = await requestPlatApi({
+ url: "xhs/login_qr_code",
+ method: "POST",
+ data: {
+ proxy: proxy,
+ },
+ }).catch((err) => {
+ setQRCodeLoading(false);
+ message.error(`${err}`);
+ });
+
if (qRCodeLoading) return;
setQRCodeLoading(false);
- setQsImg("data:image/png;base64," + res.data.base64_qr_code);
- });
- };
-
- return (
- <div className={styles.xhsLogin}>
- <AccountLoginLyaout
- qRCodeLoading={qRCodeLoading}
- qRCodeUrl={qsImg}
- phoneConfig={{
- codeSendTime: 180,
- onCodeSend: (phone: string) => {
- console.log(phone);
- },
- onLogin: (params) => {
- console.log(params);
+ setQsImg(
+ "data:image/png;base64," +
+ loginQrCodeRes.data.response_body.data.base64_qr_code,
+ );
+
+ let isStopPolling = false;
+ stopPolling.current = () => {
+ isStopPolling = true;
+ };
+ while (true) {
+ if (isStopPolling) break;
+ const res = await requestPlatApi({
+ url: "xhs/check_login",
+ method: "POST",
+ data: {
+ proxy: proxy,
+ qr_id: loginQrCodeRes.data.response_body.data.qr_id,
+ code: loginQrCodeRes.data.response_body.data.code,
+ cookie: loginQrCodeRes.data.response_body.data.cookie,
},
- loading: loginLoading,
- }}
- qRCodeTips="可用小红书或微信扫码"
- isQRCodeExpire={isQRCodeExpire}
- onFlushQRCode={getLoginQRCode}
- />
- </div>
- );
- }),
+ });
+ const status = res.data.response_body.data.code_status;
+ // 验证码过期
+ if (status === 3) {
+ setIsQRCodeExpire(true);
+ break;
+ }
+ // 验证码登录成功
+ if (status === 2) {
+ loginSuccess(res.data.response_body.cookie);
+ break;
+ }
+ await sleep(1500);
+ }
+ };
+
+ const loginSuccess = (cookie: string) => {
+ console.log(cookie);
+ };
+
+ return (
+ <div className={styles.xhsLogin}>
+ <AccountLoginLyaout
+ qRCodeLoading={qRCodeLoading}
+ qRCodeUrl={qsImg}
+ phoneConfig={{
+ codeSendTime: 180,
+ onCodeSend: async (phone: string) => {
+ sendCodeResRef.current = await requestPlatApi({
+ url: "xhs/send_code",
+ method: "POST",
+ data: {
+ proxy: proxy,
+ mobile: phone,
+ },
+ });
+ if (sendCodeResRef.current.data.response_body.code === -1) {
+ message.error(sendCodeResRef.current.data.response_body.msg);
+ }
+ },
+ onLogin: async (params) => {
+ setLoginLoading(true);
+
+ const checkCodeRes = await requestPlatApi({
+ url: "xhs/check_code",
+ method: "POST",
+ data: {
+ proxy: proxy,
+ code: params.code,
+ mobile: params.phone,
+ cookie: sendCodeResRef.current?.data?.cookie || "",
+ },
+ });
+
+ if (checkCodeRes?.data?.response_body?.code !== 0) {
+ setLoginLoading(false);
+ return message.error("验证码错误,请稍后重试!");
+ }
+
+ const loginCodeRes = await requestPlatApi({
+ url: "xhs/login_code",
+ method: "POST",
+ data: {
+ proxy: proxy,
+ mobile_token:
+ checkCodeRes.data.response_body.data.mobile_token,
+ mobile: params.phone,
+ cookie: sendCodeResRef.current?.data?.cookie || "",
+ },
+ });
+ if (loginCodeRes.data.response_body.code !== 0) {
+ setLoginLoading(false);
+ return message.error(loginCodeRes.msg);
+ }
+ console.log(loginCodeRes);
+ },
+ loading: loginLoading,
+ }}
+ qRCodeTips="可用小红书或微信扫码"
+ isQRCodeExpire={isQRCodeExpire}
+ onFlushQRCode={getLoginQRCode}
+ />
+ </div>
+ );
+ },
+ ),
);
export default XhsLogin;
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/styles/accountLogin.module.scss b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/styles/accountLogin.module.scss
index e69de29b..7f660e56 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/styles/accountLogin.module.scss
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/styles/accountLogin.module.scss
@@ -0,0 +1,20 @@
+.ALModaltitle {
+ display: flex;
+ align-items: center;
+ :global {
+ .ALModaltitle-title {
+ display: block;
+ margin-right: 10px;
+ }
+ .ALModaltitle-switch {
+ display: flex;
+ align-items: center;
+ & > span {
+ font-weight: 400;
+ font-size: var(--fs-xs);
+ display: block;
+ margin-right: 5px;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/app/[lng]/accounts/components/AddAccountModal.module.scss b/src/app/[lng]/accounts/components/AddAccountModal/AddAccountModal.module.scss
similarity index 100%
rename from src/app/[lng]/accounts/components/AddAccountModal.module.scss
rename to src/app/[lng]/accounts/components/AddAccountModal/AddAccountModal.module.scss
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/index.tsx b/src/app/[lng]/accounts/components/AddAccountModal/index.tsx
index 7c1d6f27..821862f6 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/index.tsx
+++ b/src/app/[lng]/accounts/components/AddAccountModal/index.tsx
@@ -1,5 +1,5 @@
import { ForwardedRef, forwardRef, memo, useState } from "react";
-import styles from "../AddAccountModal.module.scss";
+import styles from "./AddAccountModal.module.scss";
import { Button, Modal, Tooltip } from "antd";
import { AccountPlatInfoArr, PlatType } from "@/app/config/platConfig";
import { SocialAccount } from "@/api/types/account.type";
diff --git a/src/app/[lng]/accounts/components/ProxyInput/index.tsx b/src/app/[lng]/accounts/components/ProxyInput/index.tsx
new file mode 100644
index 00000000..48adafd0
--- /dev/null
+++ b/src/app/[lng]/accounts/components/ProxyInput/index.tsx
@@ -0,0 +1,104 @@
+import { ForwardedRef, forwardRef, memo, useEffect, useState } from "react";
+import styles from "./proxyInput.module.scss";
+import { Button, Input, message } from "antd";
+
+export interface IProxyInputRef {}
+
+export interface IProxyInputProps {
+ onChange: (name: string) => void;
+}
+
+export interface ProxyInfo {
+ // 协议: 如 http, socks5
+ protocol: string;
+ // IP+端口: 如 192.168.0.1:8000
+ ipAndPort: string;
+ // 代理账号: 可选
+ username?: string;
+ // 代理密码: 可选
+ password?: string;
+ // 刷新URL: 可选
+ refreshUrl?: string;
+ // 备注: 可选
+ remark?: string;
+}
+
+export function parseProxyString(proxyString: string): ProxyInfo | false {
+ const regex =
+ /^(?:(\w+):\/\/)?([\d.]+:\d+)(?::([^:]+):([^{}\s]+))?(?:\[(.*?)\])?(?:{(.*?)})?$/;
+
+ const match = proxyString.match(regex);
+ if (!match) {
+ return false; // 无法解析则返回 false
+ }
+
+ const [, protocol, ipAndPort, username, password, refreshUrl, remark] = match;
+
+ return {
+ protocol: protocol || "http", // 如果未提供协议,默认为 http
+ ipAndPort,
+ username,
+ password,
+ refreshUrl,
+ remark,
+ };
+}
+
+// 验证代理地址
+export async function verifyProxy(proxyName: string) {
+ const proxyInfo = parseProxyString(proxyName);
+ if (proxyInfo === false) return false;
+ return true;
+}
+
+const ProxyInput = memo(
+ forwardRef(
+ ({ onChange }: IProxyInputProps, ref: ForwardedRef<IProxyInputRef>) => {
+ const [proxyLoading, setProxyLoading] = useState(false);
+ const [proxyName, setProxyName] = useState("");
+
+ useEffect(() => {
+ onChange(proxyName);
+ }, [onChange, proxyName]);
+
+ return (
+ <>
+ <div
+ className={`${styles.createGroup} ${styles.proxyInput}`}
+ style={{ marginBottom: "20px" }}
+ >
+ <label>代理地址</label>
+ <Input
+ value={proxyName}
+ placeholder="请输入代理地址"
+ onChange={(e) => setProxyName(e.target.value)}
+ />
+ <Button
+ disabled={proxyName.length === 0}
+ loading={proxyLoading}
+ onClick={async () => {
+ const verifyRes = await verifyProxy(proxyName);
+ if (verifyRes) {
+ message.success("代理地址可用");
+ } else {
+ message.error("代理地址不可用");
+ }
+ }}
+ >
+ 验证连接
+ </Button>
+ </div>
+ <div className={styles.proxyHint}>
+ <p>1. 代理类型仅支持HTTP、HTTPS、Socks5;</p>
+ <p>支持以下填写格式</p>
+ <p>192.168.0.1:8000{`{备注}`}</p>
+ <p>192.168.0.1:8000:代理账号:代理密码{`{备注}`}</p>
+ <p>socks5://192.168.0.1:8000:代理账号:代理密码{`{备注}`}</p>
+ </div>
+ </>
+ );
+ },
+ ),
+);
+
+export default ProxyInput;
diff --git a/src/app/[lng]/accounts/components/ProxyInput/proxyInput.module.scss b/src/app/[lng]/accounts/components/ProxyInput/proxyInput.module.scss
new file mode 100644
index 00000000..27eb2c6f
--- /dev/null
+++ b/src/app/[lng]/accounts/components/ProxyInput/proxyInput.module.scss
@@ -0,0 +1,27 @@
+.proxyInput {
+ input {
+ border-top-right-radius: 0;
+ border-bottom-right-radius: 0;
+ }
+ button {
+ border-top-left-radius: 0;
+ border-bottom-left-radius: 0;
+ border-left: none;
+ }
+}
+
+.proxyHint {
+ margin: 20px 0 30px 0;
+ font-size: var(--fs-sm);
+ color: var(--grayColor8);
+}
+
+.createGroup {
+ display: flex;
+ align-items: center;
+ margin: 30px 0 50px 0;
+ label {
+ white-space: nowrap;
+ margin-right: 10px;
+ }
+}
\ No newline at end of file
diff --git a/src/store/user.ts b/src/store/user.ts
index 5151efed..627462d0 100644
--- a/src/store/user.ts
+++ b/src/store/user.ts
@@ -17,11 +17,14 @@ export interface UserInfo {
export interface IUserStore {
token?: string;
userInfo?: Partial<UserInfo>;
+ isAddAccountPorxy: boolean;
}
const state: IUserStore = {
token: undefined,
userInfo: {},
+ // 添加账户是否默认开启代理
+ isAddAccountPorxy: false,
};
export const useUserStore = createPersistStore(
@@ -30,12 +33,17 @@ export const useUserStore = createPersistStore(
},
(set, _get) => {
const methods = {
+ setIsAddAccountPorxy(isAddAccountPorxy: boolean) {
+ set({ isAddAccountPorxy });
+ },
setToken: (token: string) => {
set({ token });
},
setUserInfo: (userInfo: UserInfo) => {
set({ userInfo });
},
+
+ // 清除登录状态
clearLoginStatus: () => {
set({ token: undefined, userInfo: undefined });
},
diff --git a/src/utils/general.ts b/src/utils/general.ts
index 6e454173..4a2f1f9c 100644
--- a/src/utils/general.ts
+++ b/src/utils/general.ts
@@ -24,3 +24,7 @@ export async function getMetadata(
title,
};
}
+
+export function sleep(ms: number) {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
diff --git a/src/utils/otherRequest.ts b/src/utils/otherRequest.ts
index a676d06e..14f09edd 100644
--- a/src/utils/otherRequest.ts
+++ b/src/utils/otherRequest.ts
@@ -1,5 +1,5 @@
import FetchService from "@/utils/FetchService/FetchService";
-import { RequestParams } from "@/utils/FetchService/types";
+import {RequestParams} from "@/utils/FetchService/types";
class OtherRequest {
fetchService;
@@ -24,6 +24,5 @@ class OtherRequest {
const requestPlatapi = new OtherRequest("https://platapi.yikart.cn/api/");
export async function requestPlatApi<T = any>(params: RequestParams) {
- const res = await requestPlatapi.request(params);
- return res.data.response_body as T;
+ return await requestPlatapi.request(params);
}