index.js 5.6 KB

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