// components/editor/index.js const app = getApp() Component({ /** * 组件的属性列表 */ properties: { content: { type: String, value: '' }, name: { type: String, value: '' } }, // 生命周期函数,可以为函数,或一个在methods段中定义的方法名 attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖 ready: function () { }, pageLifetimes: { // 组件所在页面的生命周期函数 show: function () { const that = this; that.search() }, hide: function () { }, resize: function () { }, }, /** * 组件的初始数据 */ data: { formats: {}, // 客户端平台,是否为苹果 isIOS: false }, /** * 组件的方法列表 */ methods: { search() { const that = this; // 获取当前小程序版本 const platform = wx.getSystemInfoSync().platform; // 客户端平台 const isIOS = platform === 'ios' that.setData({ isIOS }); }, // 编辑初始化完成时触发 bindready: function () { const that = this; const query = wx.createSelectorQuery() query.in(this).select('#editor').context(function (res) { that.editorCtx = res.context; let { content } = that.data; let html = content; if (html) that.editorCtx.setContents({ html }) }).exec() }, // 编辑器值改变时触发 bindInput: function (e) { const that = this; const { html, text } = e.detail; this.triggerEvent("bindInput", { html, text, name: that.properties.name }); }, // 样式设置 format(e) { let { name, value, tips } = e.target.dataset if (!name) return; this.editorCtx.format(name, value); wx.showToast({ title: `${tips}`, icon: 'none', duration: 2000 }); }, // 上传图片 insertImage() { const that = this wx.chooseImage({ count: 1, sizeType: ['compressed'], sourceType: ['album'], success: function (res) { that._uploadImage(res.tempFilePaths[0]); } }); }, // 提交图片 _uploadImage: function (tempFilePath) { let that = this; wx.uploadFile({ url: `${app.globalData.serverUrl}/files/court/elimg/upload`, filePath: tempFilePath, name: 'file', formData: {}, success: (res) => { let arr = JSON.parse(res.data); if (arr.errcode == '0') { that.editorCtx.insertImage({ src: app.globalData.serverUrl + arr.uri }); } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'fail', duration: 2000 }) } }, }) }, } })