Skip to content

Let's use AI to Earn!

feat: 小红书登录接口接入完成,用户信息获取完成

gaoSirf71f08477a2f
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 f76b26ed..507da46e 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.type.ts
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.type.ts
@@ -1,6 +1,8 @@
+import { SocialAccount } from "@/api/types/account.type";
+
export interface IAccountLoginPlatProps {
// 登录成功
- onLoginSuccess: (cookie: string) => void;
+ onLoginSuccess: (accountInfo: null | Partial<SocialAccount>) => void;
// 代理地址
proxy?: string;
}
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.utils.ts b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.utils.ts
new file mode 100644
index 00000000..55557565
--- /dev/null
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.utils.ts
@@ -0,0 +1,22 @@
+import { Cookie } from "undici-types";
+
+export function parseCookieString(cookieString: string): Cookie[] {
+ return cookieString
+ .split(";")
+ .map((pair) => pair.trim())
+ .filter(Boolean)
+ .map((pair) => {
+ const eqIndex = pair.indexOf("=");
+ let name = "",
+ value = "";
+ if (eqIndex !== -1) {
+ name = pair.slice(0, eqIndex).trim();
+ value = pair.slice(eqIndex + 1).trim();
+ } else {
+ name = pair.trim();
+ value = "";
+ }
+ return { name, value } as Cookie;
+ })
+ .filter((cookie) => cookie.value !== ""); // 过滤掉 value 为空字符串的
+}
diff --git a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/index.tsx b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/index.tsx
index 1f3d123a..549cdc80 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/index.tsx
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/index.tsx
@@ -123,6 +123,7 @@ const AccountLogin = memo(
<Button
type="primary"
style={{ width: "200px", padding: "16px 0" }}
+ loading={verifyProxyLoading}
onClick={async () => {
setVerifyProxyLoading(true);
const verifyRes = await verifyProxy(proxy);
@@ -142,8 +143,10 @@ const AccountLogin = memo(
<>
<Plat
proxy={isAddAccountPorxy ? proxy : ""}
- onLoginSuccess={() => {
- console.log("登录");
+ onLoginSuccess={(accountInfo) => {
+ if (accountInfo) {
+ console.log("登录", accountInfo);
+ }
}}
/>
</>
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 02b4626d..8c8da8b4 100644
--- a/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/XhsLogin.tsx
+++ b/src/app/[lng]/accounts/components/AddAccountModal/AccountLogin/plats/XhsLogin.tsx
@@ -14,21 +14,24 @@ import { requestPlatApi } from "@/utils/otherRequest";
import AccountLoginLyaout from "@/app/[lng]/accounts/components/AddAccountModal/AccountLogin/AccountLoginLyaout";
import { message } from "antd";
import { sleep } from "@/utils/general";
+import { parseCookieString } from "@/app/[lng]/accounts/components/AddAccountModal/AccountLogin/accountLogin.utils";
+import { PlatXhs } from "@/app/plat/platChildren/xhs/PlatXhs";
export interface IXhsLoginRef {}
const XhsLogin = memo(
forwardRef(
- ({ proxy }: IAccountLoginPlatProps, ref: ForwardedRef<IXhsLoginRef>) => {
+ (
+ { proxy, onLoginSuccess }: 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>(() => {
- console.log("2222222222")
- });
+ const stopPolling = useRef<() => void>(() => {});
const [getUserInfoLoding, setGetUserInfoLoding] = useState(false);
useEffect(() => {
@@ -41,6 +44,7 @@ const XhsLogin = memo(
// 获取登录二维码
const getLoginQRCode = async () => {
+ setIsQRCodeExpire(false);
stopPolling.current();
setQRCodeLoading(true);
@@ -64,11 +68,9 @@ const XhsLogin = memo(
let isStopPolling = false;
stopPolling.current = () => {
- console.log("----------")
isStopPolling = true;
};
while (true) {
- console.log(isStopPolling);
if (isStopPolling) break;
const res = await requestPlatApi({
url: "xhs/check_login",
@@ -95,8 +97,15 @@ const XhsLogin = memo(
}
};
- const loginSuccess = (cookie: string) => {
- console.log(cookie);
+ const loginSuccess = async (cookie: string) => {
+ setGetUserInfoLoding(true);
+ const platXhs = new PlatXhs({
+ cookieList: parseCookieString(cookie),
+ proxy: proxy,
+ });
+ const accountInfo = await platXhs.getAccountInfo();
+ setGetUserInfoLoding(false);
+ onLoginSuccess(accountInfo);
};
return (
@@ -153,13 +162,15 @@ const XhsLogin = memo(
setLoginLoading(false);
return message.error(loginCodeRes.msg);
}
- console.log(loginCodeRes);
+ setLoginLoading(false);
+ loginSuccess(loginCodeRes.data.response_body.cookie);
},
loading: loginLoading,
}}
qRCodeTips="可用小红书或微信扫码"
isQRCodeExpire={isQRCodeExpire}
onFlushQRCode={getLoginQRCode}
+ loading={getUserInfoLoding}
/>
</div>
);
diff --git a/src/app/[lng]/page.tsx b/src/app/[lng]/page.tsx
index 13799221..8ff3cbba 100644
--- a/src/app/[lng]/page.tsx
+++ b/src/app/[lng]/page.tsx
@@ -1,144 +1,5 @@
"use client";
-import styles from "@/app/styles/page.module.scss";
-import React, {useEffect, useRef, useState} from "react";
-import { Button, Input } from "antd";
-
-// 等待n毫秒
-function sleep(ms: number) {
- return new Promise((resolve) => setTimeout(resolve, ms));
-}
-
export default function Home() {
- const [phone, setPhone] = useState("");
- const [code, setCode] = useState("");
- const [qsImg, setQsImg] = useState("");
- const flag = useRef(false);
-
- useEffect(() => {
- (async () => {
- if (flag.current) return;
- flag.current = true;
- const req = await fetch(
- `https://platapi.yikart.cn/api/xhs/login_qr_code`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json;charset=UTF-8",
- },
- },
- );
- const res = await req.json();
- setQsImg(
- "data:image/png;base64," + res.data.response_body.data.base64_qr_code,
- );
- document.cookie = res.data.response_body.data.cookie;
- // console.log()
-
- while (true) {
- const req2 = await fetch(
- `https://platapi.yikart.cn/api/xhs/check_login`,
- {
- method: "POST",
- // headers: {
- // "Content-Type": "application/json",
- // },
- body: JSON.stringify({
- code: res.data.response_body.data.code,
- qr_id: res.data.response_body.data.qr_id,
- cookie: res.data.response_body.data.cookie,
- }),
- },
- );
- await sleep(3000);
- const res2 = await req2.json();
- if (res2.data.response_body.data.code_status === 2) {
- console.log(res.data.response_body.data);
- break;
- }
- }
- })();
- }, []);
-
- return (
- <div className={styles.page}>
- <img src={qsImg} />
-
- <Input placeholder="手机号" onChange={(e) => setPhone(e.target.value)} />
- <Input placeholder="验证码" onChange={(e) => setCode(e.target.value)} />
- <Button
- onClick={async () => {
- const req = await fetch(`/xhsXS/`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json;charset=UTF-8",
- },
- body: JSON.stringify({
- url: `/api/sns/web/v2/login/send_code?phone=${phone}&zone=86&type=login`,
- cookie: document.cookie,
- }),
- });
- const xsRes = await req.json();
-
- const req1 = await fetch(
- `/xiaohongshu/sns/web/v2/login/send_code?phone=${phone}&zone=86&type=login`,
- {
- method: "GET",
- headers: {
- "x-s":
- "XYS_2UQhPsHCH0c1PjhIHjIj2erjwjQhyoPTqBPT49pjHjIj2eHjwjQgynEDJ74AHjIj2ePjwjQTJdPIP/ZlgMrU4SmH4ebwGFqE4pzEzrk9weHMnLMea0zH4Dk0//W3PMrUaDMeGnMzJgkO+gz+GdiI/9RynBRYzAZF8fuI/DSMG/zC+LkPaoW7cMP6PMkB+fS98Dl9N9FM+/zS+A8CnpP78BRdGMkxJBbS8omec0bfaLz3PrlhwnEhyLTIGnkeaDzPLnkSP7G38fT64A8j+dmTzflfLBT6+L+0zMb8+7zS4BL98fTfPBY+HjIj2ecjwjHjKc==",
- },
- },
- );
- console.log(req1);
-
- // const req = await fetch(
- // `https://platapi.yikart.cn/api/xhs/send_code`,
- // {
- // method: "POST",
- // body: JSON.stringify({
- // mobile: phone,
- // }),
- // },
- // );
- const res = await req.json();
- console.log(res);
- }}
- >
- 发送验证码
- </Button>
- <Button
- onClick={async () => {
- const req = await fetch(`/xhsXS/`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json;charset=UTF-8",
- },
- body: JSON.stringify({
- url: `/api/sns/web/v1/login/check_code?phone=17600917357&zone=86&code=123`,
- a1: `a1=1953cfa200duqccg24dtw9u1y7ubred8l9iyg1y0150000466896; webId=76f7ec5c74e78c5756fabe7b987c66bf; gid=yj2qSi0JD41Syj2qSi0J8jyv8f7ASSkJ4f9E38yj7yvW7v28DFdfYk8884KKYjK8qWDiq08i; customerClientId=412120289436764; abRequestId=76f7ec5c74e78c5756fabe7b987c66bf; x-user-id-ad-market.xiaohongshu.com=678df12c0000000004030ab9; x-user-id-creator.xiaohongshu.com=682851d50000000007000633; webBuild=4.64.0; customer-sso-sid=68c517509396918886050316tyy59dfmmpqjfyuw; access-token-creator.xiaohongshu.com=customer.creator.AT-68c517509396918886366307e9ezes1g34kgpku9; galaxy_creator_session_id=PljCqPggTFa0xI1sODUoiGq1qTakQPVXYthl; galaxy.creator.beaker.session.id=1748417718742000985340; xsecappid=xhs-pc-web; web_session=030037a0f3697c3b8debfc944e2f4a079bd40a; acw_tc=0a4adcd017484235263232839e9108cd2acbb581623e024122e2088ccbe8fe; websectiga=a9bdcaed0af874f3a1431e94fbea410e8f738542fbb02df1e8e30c29ef3d91ac; sec_poison_id=9ea1d95c-e767-4e55-8729-1ed51458d67e; loadts=1748424559788; unread={%22ub%22:%2268118988000000001201c272%22%2C%22ue%22:%2268169150000000002101ad9a%22%2C%22uc%22:29}`,
- }),
- });
-
- const xsRes = await req.json();
- const req2 = await fetch(
- `/xiaohongshu/sns/web/v1/login/check_code?phone=${phone}&zone=86&code=${code}`,
- {
- method: "GET",
- headers: {
- "x-s": xsRes["X-s"],
- },
- },
- );
-
- const req3 = await fetch(`/xiaohongshu/sns/web/v2/login/code`, {
- method: "POST",
- headers: {},
- });
- }}
- >
- 登录
- </Button>
- </div>
- );
+ return <div style={{ minHeight: "80vh" }}>首页</div>;
}
diff --git a/src/app/plat/PlatBase.ts b/src/app/plat/PlatBase.ts
index 2f97607c..58de3e29 100644
--- a/src/app/plat/PlatBase.ts
+++ b/src/app/plat/PlatBase.ts
@@ -1,5 +1,29 @@
import { SocialAccount } from "@/api/types/account.type";
+import { Cookie } from "undici-types";
+import { IPlatConstrParams } from "@/app/plat/plat.type";
export abstract class PlatBase {
- abstract login(): Promise<SocialAccount>;
+ cookieStr: string;
+ cookieList: Cookie[];
+ proxy?: string;
+
+ // 用户账户信息
+ abstract getAccountInfo(): Promise<Partial<SocialAccount> | null>;
+
+ constructor(params: IPlatConstrParams) {
+ this.cookieList = params.cookieList;
+ this.cookieStr = this.convertCookieToJson(params.cookieList);
+ this.proxy = params.proxy;
+ }
+
+ convertCookieToJson(cookieJson: any) {
+ if (typeof cookieJson === "string") {
+ cookieJson = JSON.parse(cookieJson);
+ }
+ let cookieStr = "";
+ cookieJson.forEach((cookie: any) => {
+ cookieStr += `${cookie.name}=${cookie.value}; `;
+ });
+ return cookieStr;
+ }
}
diff --git a/src/app/plat/PlatManage.ts b/src/app/plat/PlatManage.ts
index 2e8bc026..b1919f44 100644
--- a/src/app/plat/PlatManage.ts
+++ b/src/app/plat/PlatManage.ts
@@ -1,3 +1,16 @@
+import { PlatType } from "@/app/config/platConfig";
+import { IPlatConstrParams } from "@/app/plat/plat.type";
+import { PlatDouyin } from "@/app/plat/platChildren/douyin/PlatDouyin";
+import { PlatXhs } from "@/app/plat/platChildren/xhs/PlatXhs";
+
class PlatManage {
- getPlat() {}
+ private getPlat(type: PlatType, params: IPlatConstrParams) {
+ switch (type) {
+ case PlatType.Douyin:
+ return new PlatDouyin(params);
+ case PlatType.Xhs:
+ return new PlatXhs(params);
+ }
+ }
}
+export const platManage = new PlatManage();
diff --git a/src/app/plat/plat.type.ts b/src/app/plat/plat.type.ts
index e69de29b..12ab9a5f 100644
--- a/src/app/plat/plat.type.ts
+++ b/src/app/plat/plat.type.ts
@@ -0,0 +1,9 @@
+// plat构造器参数
+import { Cookie } from "undici-types";
+
+export interface IPlatConstrParams {
+ // cookie
+ cookieList: Cookie[];
+ // 代理
+ proxy?: string;
+}
diff --git a/src/app/plat/platChildren/douyin/PlatDouyin.ts b/src/app/plat/platChildren/douyin/PlatDouyin.ts
new file mode 100644
index 00000000..7ed844b2
--- /dev/null
+++ b/src/app/plat/platChildren/douyin/PlatDouyin.ts
@@ -0,0 +1,8 @@
+import { PlatBase } from "@/app/plat/PlatBase";
+import { SocialAccount } from "@/api/types/account.type";
+
+export class PlatDouyin extends PlatBase {
+ getAccountInfo(): Promise<Partial<SocialAccount> | null> {
+ return Promise.resolve(null);
+ }
+}
diff --git a/src/app/plat/platChildren/xhs/PlatXhs.ts b/src/app/plat/platChildren/xhs/PlatXhs.ts
new file mode 100644
index 00000000..720ee24b
--- /dev/null
+++ b/src/app/plat/platChildren/xhs/PlatXhs.ts
@@ -0,0 +1,33 @@
+import {PlatBase} from "@/app/plat/PlatBase";
+import {requestPlatApi} from "@/utils/otherRequest";
+import {SocialAccount} from "@/api/types/account.type";
+import {PlatType} from "@/app/config/platConfig";
+
+export class PlatXhs extends PlatBase {
+ async getAccountInfo(): Promise<Partial<SocialAccount> | null> {
+ try {
+ const res = await requestPlatApi({
+ url: "xhs/me",
+ method: "POST",
+ data: {
+ proxy: this.proxy,
+ cookie: this.cookieStr,
+ },
+ });
+ const { basic_info, interactions } = res.data.response_body.data;
+
+ return {
+ loginCookie: JSON.stringify(this.cookieList),
+ type: PlatType.Xhs,
+ uid: basic_info.red_id,
+ account: basic_info.red_id,
+ nickname: basic_info.nickname,
+ avatar: basic_info.images,
+ fansCount: interactions.find((v: any) => v.type === "fans").count,
+ };
+ } catch (e) {
+ console.warn(e);
+ return null;
+ }
+ }
+}
diff --git a/src/utils/otherRequest.ts b/src/utils/otherRequest.ts
index 14f09edd..298ce94a 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;