Skip to content

Let's use AI to Earn!

feat: 快手发布api接入完成

gaoSirc6de3ceb249b
diff --git a/src/app/[lng]/publish/videoPage/components/VideoPubSetModal/children/VideoPubSetModal_KWAI.tsx b/src/app/[lng]/publish/videoPage/components/VideoPubSetModal/children/VideoPubSetModal_KWAI.tsx
index 28784567..19d6c1b6 100644
--- a/src/app/[lng]/publish/videoPage/components/VideoPubSetModal/children/VideoPubSetModal_KWAI.tsx
+++ b/src/app/[lng]/publish/videoPage/components/VideoPubSetModal/children/VideoPubSetModal_KWAI.tsx
@@ -19,7 +19,7 @@ const VideoPubSetModal_KWAI = memo(
maxLength={500}
/>
- <TopicSelect />
+ {/*<TopicSelect />*/}
{/*<UserSelect*/}
{/* maxCount={3}*/}
diff --git a/src/app/[lng]/publish/videoPage/videoPage.type.ts b/src/app/[lng]/publish/videoPage/videoPage.type.ts
index f15e4129..6c9bd109 100644
--- a/src/app/[lng]/publish/videoPage/videoPage.type.ts
+++ b/src/app/[lng]/publish/videoPage/videoPage.type.ts
@@ -1,8 +1,8 @@
-import {SocialAccount} from "@/api/types/account.type";
-import {PlatType} from "@/app/config/platConfig";
-import {VisibleTypeEnum} from "@/app/config/publishConfig";
-import {IVideoFile} from "@/app/[lng]/publish/components/Choose/VideoChoose";
-import {IImgFile} from "@/app/[lng]/publish/components/Choose/ImgChoose";
+import { SocialAccount } from "@/api/types/account.type";
+import { PlatType } from "@/app/config/platConfig";
+import { VisibleTypeEnum } from "@/app/config/publishConfig";
+import { IVideoFile } from "@/app/[lng]/publish/components/Choose/VideoChoose";
+import { IImgFile } from "@/app/[lng]/publish/components/Choose/ImgChoose";
// 微信视频号活动
export interface WxSphEvent {
@@ -118,4 +118,4 @@ export enum AccountChooseType {
Radio = 1,
// 替换
Replace = 2,
-}
\ No newline at end of file
+}
diff --git a/src/app/plat/plat.type.ts b/src/app/plat/plat.type.ts
index a98d9742..06980fd6 100644
--- a/src/app/plat/plat.type.ts
+++ b/src/app/plat/plat.type.ts
@@ -1,5 +1,7 @@
// plat构造器参数
import { SocialAccount } from "@/api/types/account.type";
+import {IPubParams} from "@/app/[lng]/publish/videoPage/videoPage.type";
+import {IVideoFile} from "@/app/[lng]/publish/components/Choose/VideoChoose";
export interface IPlatConstrParams {
access_token: string;
@@ -32,3 +34,16 @@ export interface PublishProgressRes {
account: SocialAccount;
id: number;
}
+
+// 视频发布返回值
+export interface PublishRes {
+ // 作品ID
+ worksId: string;
+ // 作品链接
+ worksUrl: string;
+}
+
+// 视频发布
+export interface IVideoPublishItem extends IPubParams {
+ video: IVideoFile;
+}
diff --git a/src/app/plat/plat.util.ts b/src/app/plat/plat.util.ts
new file mode 100644
index 00000000..e4be212c
--- /dev/null
+++ b/src/app/plat/plat.util.ts
@@ -0,0 +1,32 @@
+// 读取 blob 的文件流
+export function readBlobRange(
+ blob: Blob,
+ start: number,
+ end: number,
+): Promise<ArrayBuffer> {
+ return new Promise((resolve, reject) => {
+ const sliced = blob.slice(start, end);
+ const reader = new FileReader();
+ reader.onload = () => resolve(reader.result as ArrayBuffer);
+ reader.onerror = reject;
+ reader.readAsArrayBuffer(sliced);
+ });
+}
+
+type ChunkRange = { start: number; end: number };
+/**
+ * 根据总大小和每片大小,计算每一片的 start 和 end 位置
+ * @param size 视频的总字节数
+ * @param chunkSize 每一片的字节数
+ * @returns 每一片的 {start, end} 数组
+ */
+export function calculateChunks(size: number, chunkSize: number): ChunkRange[] {
+ const chunks: ChunkRange[] = [];
+ let start = 0;
+ while (start < size) {
+ const end = Math.min(start + chunkSize, size);
+ chunks.push({ start, end });
+ start = end;
+ }
+ return chunks;
+}
diff --git a/src/app/plat/platChildren/kwai/KwaiPlat.ts b/src/app/plat/platChildren/kwai/KwaiPlat.ts
index 78ab702d..ff4a8b2d 100644
--- a/src/app/plat/platChildren/kwai/KwaiPlat.ts
+++ b/src/app/plat/platChildren/kwai/KwaiPlat.ts
@@ -26,9 +26,9 @@ export class KwaiPlat extends PlatBase {
request(params: RequestParams) {
params.url = `${KwaiApiProxyUrl}${params.url}`;
params.params = {
- ...(params.params ?? {}),
access_token: this.access_token,
app_id: kwaiAppId,
+ ...(params.params ?? {}),
};
return requestPlatApi(params);
}
diff --git a/src/app/plat/platChildren/kwai/KwaiPubCore.ts b/src/app/plat/platChildren/kwai/KwaiPubCore.ts
new file mode 100644
index 00000000..c1311199
--- /dev/null
+++ b/src/app/plat/platChildren/kwai/KwaiPubCore.ts
@@ -0,0 +1,87 @@
+import { KwaiPlat } from "@/app/plat/platChildren/kwai/KwaiPlat";
+import { IVideoPublishItem, PublishRes } from "@/app/plat/plat.type";
+import { calculateChunks, readBlobRange } from "@/app/plat/plat.util";
+
+class KwaiPubCore {
+ kwaiPlat: KwaiPlat;
+ videoPubParams: IVideoPublishItem;
+ fileBlockSize = 4194304;
+
+ constructor(kwaiPlat: KwaiPlat, pubParams: IVideoPublishItem) {
+ this.kwaiPlat = kwaiPlat;
+ this.videoPubParams = pubParams;
+ }
+
+ // 视频发布
+ async publishVideo(): Promise<PublishRes> {
+ const startUploadInfo = await this.startUploadVideo();
+ // 获取分片的大小
+ const chunkRange = calculateChunks(
+ this.videoPubParams.video.size,
+ this.fileBlockSize,
+ );
+
+ // 分片上传
+ for (let i = 0; i < chunkRange.length; i++) {
+ const range = chunkRange[i];
+ // 获取要上传的视频分片
+ const chunk = await readBlobRange(
+ this.videoPubParams.video.file,
+ range.start,
+ range.end,
+ );
+ // 发起分片上传
+ const chunkUploadRes = await this.kwaiPlat.request({
+ url: `/kuaishoUploadApi/api/upload/fragment`,
+ method: "POST",
+ params: {
+ fragment_id: i,
+ upload_token: startUploadInfo.upload_token,
+ },
+ data: chunk,
+ });
+ if (chunkUploadRes.result !== 1) {
+ throw new Error("分片上传失败!");
+ }
+ console.log(
+ `${i} / ${chunkRange.length} 分片上传结果:${chunkUploadRes}`,
+ );
+ }
+
+ // 分片上传完成
+ const completeRes = await this.kwaiPlat.request({
+ url: `/kuaishoUploadApi/api/upload/complete`,
+ method: "POST",
+ params: {
+ fragment_count: chunkRange.length,
+ upload_token: startUploadInfo.upload_token,
+ },
+ });
+
+ const formData = new FormData();
+ const pubRes = await this.kwaiPlat.request({
+ url: "/kuaishoApi/openapi/photo/publish",
+ method: "POST",
+ headers: {
+ "Content-Type": "multipart/form-data",
+ },
+ params: {
+ upload_token: startUploadInfo.upload_token,
+ },
+ data: formData,
+ });
+ }
+
+ // 发起上传
+ async startUploadVideo() {
+ const res = await this.kwaiPlat.request({
+ url: "openapi/photo/start_upload",
+ method: "GET",
+ });
+ return res.data as {
+ result: number;
+ upload_token: string;
+ endpoint: string;
+ };
+ }
+}