Skip to content

Let's use AI to Earn!

集成完成

DESKTOP-EMF2SH4\Administrator9712c20d1979
diff --git a/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts b/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts
index 5eb8d479..573eaef4 100644
--- a/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts
+++ b/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts
@@ -6,6 +6,13 @@ import {
} from "@/app/plat/plat.type";
import { calculateChunks, readBlobRange } from "@/app/plat/plat.util";
import { KwaiApiUploadProxyUrl } from "@/constant";
+import {
+ apiInitBilibiliVideo,
+ apiUploadBilibiliCover,
+ apiSubmitBilibiliArchive,
+ apiUploadBilibiliVideoPart,
+ apiCompleteBilibiliVideo,
+} from "@/api/bilibili";
export class BilibiliPubCore {
private kwaiPlat: BilibiliPlat;
@@ -47,16 +54,122 @@ export class BilibiliPubCore {
// 视频发布
async publishVideo(): Promise<IPublishResult> {
+
console.log("视频发布");
console.log(this.videoPubParams);
console.log(this.kwaiPlat.account);
- return {
- worksId: "",
- worksUrl: "",
- success: true,
- };
+ try {
+ // 1. 获取上传token
+ const initRes = await apiInitBilibiliVideo({
+ accountId: this.kwaiPlat.account.id,
+ name: this.videoPubParams.video.filename,
+ });
+
+ if (!initRes?.data?.data) {
+ throw new Error("获取上传token失败");
+ }
+
+ const uploadToken = initRes.data.data;
+ console.log("获取上传token成功:", uploadToken);
+ this.onProgress?.(0.1, "获取上传token成功");
+
+ // 2. 上传视频(分片上传)
+ if (!this.videoPubParams.video.file) {
+ throw new Error("视频文件不存在");
+ }
+
+ const file = this.videoPubParams.video.file;
+ const chunkRange = calculateChunks(file.size, this.fileBlockSize);
+ console.log(`文件将被分为 ${chunkRange.length} 个分片`);
+
+ // 分片上传
+ for (let i = 0; i < chunkRange.length; i++) {
+ const range = chunkRange[i];
+ const sliced = file.slice(range.start, range.end);
+ console.log(`上传分片 ${i + 1}/${chunkRange.length}`);
+
+ const res = await apiUploadBilibiliVideoPart(
+ this.kwaiPlat.account.id,
+ uploadToken,
+ i + 1,
+ sliced
+ );
+
+ if (res?.code !== 0) {
+ throw new Error(`分片 ${i + 1} 上传失败`);
+ }
+ // 更新进度
+ const progress = 0.1 + (0.6 * (i + 1)) / chunkRange.length;
+ this.onProgress?.(progress, `上传分片 ${i + 1}/${chunkRange.length}`);
+ }
+
+ // 合并分片
+ console.log("开始合并分片...");
+ this.onProgress?.(0.7, "开始合并分片");
+ const completeRes = await apiCompleteBilibiliVideo(
+ this.kwaiPlat.account.id,
+ uploadToken
+ );
+ if (completeRes?.code !== 0) {
+ throw new Error("分片合并失败");
+ }
+ this.onProgress?.(0.8, "分片合并完成");
+
+ // 3. 上传封面
+ let coverUrl:any = "";
+ if (this.videoPubParams.cover?.file) {
+ const formData = new FormData();
+ formData.append("file", this.videoPubParams.cover.file);
+ const coverRes = await apiUploadBilibiliCover(
+ this.kwaiPlat.account.id,
+ formData
+ );
+ if (coverRes?.data) {
+ coverUrl = coverRes.data;
+ console.log("封面上传成功:", coverUrl);
+ this.onProgress?.(0.9, "封面上传成功");
+ }
+ }
+
+ // 4. 提交稿件
+ const archiveData = {
+ accountId: this.kwaiPlat.account.id,
+ uploadToken,
+ title: this.videoPubParams.title,
+ cover: coverUrl,
+ tid: 21, // 默认使用日常分区
+ // noReprint: this.videoPubParams.noReprint || 0,
+ desc: this.videoPubParams.describe,
+ tag: this.videoPubParams.topics || [],
+ copyright: 1,
+ };
+
+ this.onProgress?.(0.95, "正在提交稿件");
+ const submitRes:any = await apiSubmitBilibiliArchive(archiveData);
+ console.log("稿件提交结果:", submitRes);
+
+ if (submitRes?.code !== 0) {
+ throw new Error("稿件提交失败");
+ }
+
+ this.onProgress?.(1, "发布成功");
+ return {
+ worksId: submitRes.data?.data || "",
+ worksUrl: `https://www.bilibili.com/video/${submitRes.data?.data}`,
+ success: true,
+ };
+ } catch (error:any) {
+ console.error("发布视频失败:", error);
+ this.onProgress?.(0, "发布失败");
+ return {
+ worksId: "",
+ worksUrl: "",
+ success: false,
+ failMsg: error instanceof Error ? error.message : "发布失败",
+ };
+ }
}
// 发起上传