index.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // components/editor/index.js
  2. const app = getApp()
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. content: { type: String, value: '' },
  9. name: { type: String, value: '' }
  10. },
  11. // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
  12. attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  13. ready: function () { },
  14. pageLifetimes: {
  15. // 组件所在页面的生命周期函数
  16. show: function () { const that = this; that.search() },
  17. hide: function () { },
  18. resize: function () { },
  19. },
  20. /**
  21. * 组件的初始数据
  22. */
  23. data: {
  24. formats: {},
  25. // 客户端平台,是否为苹果
  26. isIOS: false
  27. },
  28. /**
  29. * 组件的方法列表
  30. */
  31. methods: {
  32. search() {
  33. const that = this;
  34. // 获取当前小程序版本
  35. const platform = wx.getSystemInfoSync().platform;
  36. // 客户端平台
  37. const isIOS = platform === 'ios'
  38. that.setData({ isIOS });
  39. },
  40. // 编辑初始化完成时触发
  41. bindready: function () {
  42. const that = this;
  43. const query = wx.createSelectorQuery()
  44. query.in(this).select('#editor').context(function (res) {
  45. that.editorCtx = res.context;
  46. let { content } = that.data;
  47. let html = content;
  48. if (html) that.editorCtx.setContents({ html })
  49. }).exec()
  50. },
  51. // 编辑器值改变时触发
  52. bindInput: function (e) {
  53. const that = this;
  54. const { html, text } = e.detail;
  55. this.triggerEvent("bindInput", { html, text, name: that.properties.name });
  56. },
  57. // 样式设置
  58. format(e) {
  59. let { name, value, tips } = e.target.dataset
  60. if (!name) return;
  61. this.editorCtx.format(name, value);
  62. wx.showToast({ title: `${tips}`, icon: 'none', duration: 2000 });
  63. },
  64. // 上传图片
  65. insertImage() {
  66. const that = this
  67. wx.chooseImage({
  68. count: 1,
  69. sizeType: ['compressed'],
  70. sourceType: ['album'],
  71. success: function (res) {
  72. that._uploadImage(res.tempFilePaths[0]);
  73. }
  74. });
  75. },
  76. // 提交图片
  77. _uploadImage: function (tempFilePath) {
  78. let that = this;
  79. wx.uploadFile({
  80. url: `${app.globalData.serverUrl}/files/court/elimg/upload`,
  81. filePath: tempFilePath,
  82. name: 'file',
  83. formData: {},
  84. success: (res) => {
  85. let arr = JSON.parse(res.data);
  86. if (arr.errcode == '0') {
  87. that.editorCtx.insertImage({ src: app.globalData.serverUrl + arr.uri });
  88. } else {
  89. wx.showToast({ title: `${arr.errmsg}`, icon: 'fail', duration: 2000 })
  90. }
  91. },
  92. })
  93. },
  94. }
  95. })