index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const _Http = getApp().globalData.http;
  2. Component({
  3. properties: {
  4. //富文本内容
  5. content: {
  6. type: String,
  7. value: ""
  8. },
  9. //是否开启编辑
  10. editable: {
  11. type: Boolean,
  12. value: true
  13. },
  14. show: {
  15. type: Boolean,
  16. value: false
  17. },
  18. callback: {
  19. type: Function
  20. }
  21. },
  22. lifetimes: {
  23. attached: function () {
  24. getApp().globalData.Language.getLanguagePackage(this)
  25. this.ctx = this.selectComponent('#article');
  26. this.ctx.getSrc = (type, value) => {
  27. return new Promise((resolve, reject) => {
  28. if (type === 'img' || type === 'video') {
  29. // 本地选取
  30. if (type === 'img') {
  31. resolve(this.data.fileMsg.url)
  32. } else {
  33. resolve(this.data.fileMsg.url)
  34. }
  35. } else {
  36. this.callback = {
  37. resolve,
  38. reject
  39. }
  40. let title
  41. if (type === 'audio') {
  42. title = '音频链接'
  43. } else if (type === 'link') {
  44. title = '链接地址'
  45. }
  46. this.setData({
  47. modal: {
  48. title,
  49. value
  50. }
  51. })
  52. }
  53. })
  54. }
  55. },
  56. detached: function () {
  57. // 在组件实例被从页面节点树移除时执行
  58. },
  59. },
  60. methods: {
  61. insertImgEdit({
  62. detail
  63. }) {
  64. this.binding('insertImg', detail)
  65. },
  66. insertVideoEdit({
  67. detail
  68. }) {
  69. this.binding('insertVideo', detail)
  70. },
  71. binding(type, id) {
  72. const that = this;
  73. let pages = getCurrentPages();
  74. let prevPage = pages[pages.length - 1];
  75. _Http.basic({
  76. "classname": "system.attachment.Attachment",
  77. "method": "createFileLink",
  78. "content": {
  79. "ownertable": "SAT_SHAREMATERIAL",
  80. "ownerid": prevPage.data.detailsData.sat_sharematerialid,
  81. "usetype": "richtext",
  82. "attachmentids": id
  83. }
  84. }).then(res => {
  85. if (res.code != '1') return wx.showToast({
  86. title: res.msg,
  87. icon: "none"
  88. });
  89. this.setData({
  90. fileMsg: res.data[0]
  91. })
  92. prevPage.setData({
  93. richTextFile: prevPage.data.richTextFile.concat(res.data[0])
  94. })
  95. that.ctx[type]();
  96. })
  97. },
  98. // 删除图片/视频/音频标签事件
  99. remove({
  100. detail
  101. }) {
  102. let pages = getCurrentPages();
  103. let prevPage = pages[pages.length - 1];
  104. let richTextFile = prevPage.data.richTextFile;
  105. let index = richTextFile.findIndex((value) => value.url == detail.src);
  106. _Http.basic({
  107. "classname": "system.attachment.Attachment",
  108. "method": "deleteFileLink",
  109. "content": {
  110. "linksids": [richTextFile[index].linksid]
  111. }
  112. }).then(res => {
  113. richTextFile.splice(index, 1);
  114. prevPage.setData({
  115. richTextFile
  116. })
  117. })
  118. },
  119. // 处理模态框
  120. modalInput(e) {
  121. this.value = e.detail.value
  122. },
  123. modalConfirm() {
  124. this.callback.resolve(this.value || this.data.modal.value || '')
  125. this.setData({
  126. modal: null
  127. })
  128. },
  129. modalCancel() {
  130. this.callback.reject()
  131. this.setData({
  132. modal: null
  133. })
  134. },
  135. // 调用编辑器接口
  136. edit(e) {
  137. this.ctx[e.currentTarget.dataset.method]()
  138. },
  139. // 清空编辑器内容
  140. clear() {
  141. wx.showModal({
  142. title: getApp().globalData.Language.getMapText('提示'),
  143. content: getApp().globalData.Language.getMapText('确定清空内容吗') + '?',
  144. cancelText: getApp().globalData.Language.getMapText('取消'),
  145. confirmText: getApp().globalData.Language.getMapText('确定'),
  146. success: res => {
  147. if (res.confirm) this.ctx.clear()
  148. }
  149. })
  150. },
  151. // 保存编辑器内容
  152. save() {
  153. // 避免无法获取到正在编辑的文本内容
  154. setTimeout(() => {
  155. let content = this.ctx.getContent(),
  156. that = this;
  157. wx.showModal({
  158. title: getApp().globalData.Language.getMapText('提示'),
  159. content: getApp().globalData.Language.getMapText('是否确认保存'),
  160. cancelText: getApp().globalData.Language.getMapText('取消'),
  161. confirmText: getApp().globalData.Language.getMapText('完成'),
  162. success: res => {
  163. if (res.confirm) {
  164. that.triggerEvent("callback", content);
  165. that.closeShow();
  166. }
  167. }
  168. })
  169. }, 50)
  170. },
  171. closeShow() {
  172. let pages = getCurrentPages();
  173. let prevPage = pages[pages.length - 1];
  174. prevPage.setData({
  175. editRichText: false
  176. })
  177. }
  178. }
  179. })