feat: worksheet发布功能开发

This commit is contained in:
R524809
2026-04-24 17:49:11 +08:00
parent 5106dbbd7c
commit bb69d5c49d
24 changed files with 2540 additions and 1014 deletions
+165
View File
@@ -3,6 +3,12 @@ import { downloadPrint } from '../utils/downloadPrint';
import { BaseDrawService } from '../core/draw/baseDraw';
import tracker from '../utils/tracker';
import { defaultShareConfig } from '../config/config';
import {
buildWorksheetPreviewCloudPath,
isDebugPublishEnabled,
type DebugPublishConfirmDetail,
type DebugPublishMeta,
} from '../utils/debugPublish';
/**
* Canvas 相关的页面实例属性
@@ -17,6 +23,8 @@ export interface PageCanvasInstance {
setData(data: any, callback?: () => void): void;
route: string;
getShareOptions(): ShareOptions;
selectComponent(selector: string): any;
getPublishMeta?(): DebugPublishMeta;
}
/**
@@ -34,6 +42,10 @@ export interface CanvasDataState {
data: any;
setData(data: any, callback?: () => void): void;
route: string;
isDevEnv?: boolean;
debugPublishVisible?: boolean;
debugPublishLoading?: boolean;
debugPublishMeta?: DebugPublishMeta | null;
}
/**
@@ -275,6 +287,159 @@ export function getPageCommonMethods(config: PageCommonMethodsConfig = {}) {
}
},
syncDebugPublishEnv(this: PageCanvasInstance) {
this.setData({ isDevEnv: isDebugPublishEnabled() });
},
onOpenDebugPublish(this: PageCanvasInstance) {
if (!isDebugPublishEnabled()) return;
if (typeof this.getPublishMeta !== 'function') {
wx.showToast({
title: '页面未实现发布元数据',
icon: 'none',
});
return;
}
try {
const meta = this.getPublishMeta();
this.setData({
isDevEnv: true,
debugPublishVisible: true,
debugPublishMeta: meta,
});
} catch (error) {
wx.showToast({
title:
error instanceof Error
? error.message
: '准备发布数据失败',
icon: 'none',
});
}
},
onCloseDebugPublish(this: PageCanvasInstance) {
this.setData({ debugPublishVisible: false });
},
/**
* 确认发布
*/
async onConfirmDebugPublish(
this: PageCanvasInstance,
e: WechatMiniprogram.CustomEvent<DebugPublishConfirmDetail>,
) {
const previewCard = this.selectComponent('#previewCard');
const debugPublishTools =
this.selectComponent('#debugPublishTools');
const baseMeta =
this.data.debugPublishMeta ||
(typeof this.getPublishMeta === 'function'
? this.getPublishMeta()
: null);
if (!previewCard?.exportToTempFile) {
wx.showToast({
title: '预览组件不可用',
icon: 'none',
});
return;
}
if (!debugPublishTools?.processImage) {
wx.showToast({
title: '发布工具不可用',
icon: 'none',
});
return;
}
if (!baseMeta) {
wx.showToast({
title: '缺少发布元数据',
icon: 'none',
});
return;
}
const detail = e.detail;
const publishMeta: DebugPublishMeta = {
...baseMeta,
title: detail.meta.title,
subtitle: detail.meta.subtitle,
tags: detail.meta.tags,
status: detail.meta.status,
};
this.setData({ debugPublishLoading: true });
wx.showLoading({ title: '发布中...' });
try {
const sourcePath = await previewCard.exportToTempFile({
fileType: 'jpg',
quality: 1,
});
console.log('sourcePath', sourcePath);
const processed = await debugPublishTools.processImage({
sourcePath,
settings: detail.settings,
});
console.log('processed', processed);
const cloudPath = buildWorksheetPreviewCloudPath(
publishMeta.category,
publishMeta.id,
);
console.log('cloudPath', cloudPath);
const uploadRes = await wx.cloud.uploadFile({
cloudPath,
filePath: processed.tempFilePath,
});
console.log('uploadRes', uploadRes);
const cloudCall = (await wx.cloud.callFunction({
name: 'worksheetsPublish',
data: {
...publishMeta,
previewImg: uploadRes.fileID,
},
})) as {
result?: { success?: boolean; message?: string };
};
console.log('cloudCall', cloudCall);
if (!cloudCall.result?.success) {
throw new Error(
cloudCall.result?.message || '云端写入失败',
);
}
console.log('success');
this.setData({
debugPublishVisible: false,
debugPublishMeta: {
...publishMeta,
previewImg: uploadRes.fileID,
},
});
wx.showToast({
title: `发布成功 ${Math.round(processed.size / 1024)}KB`,
icon: 'success',
});
} catch (error) {
console.log('onConfirmDebugPublish error', error);
wx.showToast({
title: error instanceof Error ? error.message : '发布失败',
icon: 'none',
});
} finally {
wx.hideLoading();
this.setData({ debugPublishLoading: false });
}
},
/**
* 初始化页面信息
*