稿定企业版微信小程序编辑器 SDK
编辑器是基于 web-view 组件实现, 所以发布前请提前申请 h5 白名单
h5 域名地址为 https://sdk.open.gaoding.com
接入 SDK
通过 npm 方式安装
npm install @gaoding/enterprise-wechatapp-editor-sdk
小程序的 npm 比较特殊,需要通过开发者工具构建一次 npm 具体使用方式
小程序目前无法直接支持引用 node_modules
内页面,需要手动新增一个页面来承载稿定的页面(模板中心/编辑器页面)。
比如需要创建一个地址为 /pages/gaoding/index
的页面
pages/gaoding/index.wxml
<gaoding-design query="{{query}}"></gaoding-design>
pages/gaoding/index.js
Page({
data: {
query: null,
},
onLoad: function (options) {
this.setData({ query: options });
},
});
pages/gaoding/index.json
{
"usingComponents": {
"gaoding-design": "@gaoding/enterprise-wechatapp-editor-sdk/design"
}
}
初始化
使用前需要进行初始化, 可以写在 app.js 文件中,也可以在写在承载页 js 中。
const { gaoding } = require('@gaoding/enterprise-wechatapp-editor-sdk');
gaoding.init({
pagePath: '/pages/gaoding/index',
getCode() {
return new Promise((resolve, reject) => {
wx.request({
url: 'xxx',
method: 'POST',
data: { uid: '123' },
success: (result) => {
resolve(result.data.code);
},
fail: (err) => {
console.error('获取code失败:', err);
reject(err);
},
});
});
},
});
业务域名配置
因为编辑器是通过 web-view 的方式来实现,所以需要进行业务域名配置,使得编辑器能够在小程序内正常访问,
操作如下:
打开模板中心
import { gaoding } from '@gaoding/enterprise-wechatapp-editor-sdk';
Page({
onTap() {
gaoding.goToTemplatesPage({ categoryId: '12345' }).then((res) => {
if (res === null) {
my.alert({ title: '没有完成做图' });
} else if (res.image) {
this.setData({
image: res.image,
});
}
});
},
});
打开指定模板
import { gaoding } from '@gaoding/enterprise-wechatapp-editor-sdk';
Page({
onTap() {
gaoding.openEditor('12345678', 'company').then((res) => {
if (res === null) {
my.alert({ title: '没有完成做图' });
} else if (res.image) {
this.setData({
image: res.image,
});
}
});
},
});
SDK 方法
interface SDK {
goToTemplatesPage(options: { categoryId: string }): Promise<{ image: string } | null>;
openEditor(id: string, mode: 'company' | 'user'): Promise<{ image: string } | null>;
init(options: {
getCode(): Promise<string>;
templatesPageTitle?: string;
editorPageTitle?: string;
pagePath: string;
}): void;
}