自定义api插件
约 366 字大约 1 分钟
...
2026-09-29
说明
api插件 用于扩展模板实例的 API.
例如: 导出图片, 导出pdf等等. 又或者加密导出模板json, 连接 MQTT 发送打印数据等等.
编写api主体
toJsonBase64.ts
import { designerUtils } from '@sv-print/vue3';
const toJsonBase64 = async function () {
// 这里的 this 指向 模板对象, 模板对象提供 getJson 方法, 获取模板数据.
const json = this.getJson();
// 转为 base64 字符串
const base64 = btoa(JSON.stringify(json));
return base64;
};
export { toJsonBase64 };编写插件
index.ts
import type { PluginOptions } from 'sv-print';
import { toJsonBase64 } from './toJsonBase64';
export default function plugin(options?: any): PluginOptions {
return {
name: 'add toJsonBase64 Api',
description: 'add toJsonBase64 Api',
hooks: [
{
hook: 'newPrintTemplate', // 创建模板对象时挂载 API
name: 'add toJsonBase64 Api to PrintTemplate',
description: 'add toJsonBase64 Api to PrintTemplate',
priority: 1,
run: ({ template, options }) => {
Object.defineProperty(template, 'toJsonBase64', {
value: toJsonBase64,
enumerable: false,
writable: true,
configurable: true,
});
},
},
],
leastHiprintVersion: '0.3.0',
};
}版权所有
版权归属:sv-print
