editor.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {
  2. ApiModel
  3. } from "../../../utils/api";
  4. const _Http = new ApiModel();
  5. Page({
  6. data: {
  7. formats: {},
  8. readOnly: false,
  9. placeholder: '开始输入...',
  10. editorHeight: 300,
  11. keyboardHeight: 0,
  12. isIOS: false,
  13. showRichText: '', //富文本文件
  14. isDisabled: false, //只读
  15. },
  16. readOnlyChange() {
  17. this.setData({
  18. readOnly: !this.data.readOnly
  19. })
  20. },
  21. /* 回调 */
  22. callBack() {
  23. wx.showToast({
  24. title: '保存成功',
  25. })
  26. this.getOpenerEventChannel().emit('richTextCallBack', {
  27. richText: this.data.showRichText
  28. })
  29. setTimeout(() => {
  30. wx.navigateBack()
  31. }, 500);
  32. },
  33. onLoad(options) {
  34. let isDisabled = false;
  35. if (wx.getStorageSync('userData').fisadministrator == 1) isDisabled = true;
  36. const platform = wx.getSystemInfoSync().platform
  37. const isIOS = platform === 'ios'
  38. this.setData({
  39. isIOS,
  40. showRichText: decodeURIComponent(options.fintroduction),
  41. isDisabled
  42. })
  43. const that = this
  44. this.updatePosition(0)
  45. let keyboardHeight = 0
  46. wx.onKeyboardHeightChange(res => {
  47. if (res.height === keyboardHeight) return
  48. const duration = res.height > 0 ? res.duration * 1000 : 0
  49. keyboardHeight = res.height
  50. setTimeout(() => {
  51. wx.pageScrollTo({
  52. scrollTop: 0,
  53. success() {
  54. that.updatePosition(keyboardHeight)
  55. that.editorCtx.scrollIntoView()
  56. }
  57. })
  58. }, duration)
  59. })
  60. },
  61. updatePosition(keyboardHeight) {
  62. const toolbarHeight = 50
  63. const {
  64. windowHeight,
  65. platform
  66. } = wx.getSystemInfoSync()
  67. let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
  68. this.setData({
  69. editorHeight,
  70. keyboardHeight
  71. })
  72. },
  73. calNavigationBarAndStatusBar() {
  74. const systemInfo = wx.getSystemInfoSync()
  75. const {
  76. statusBarHeight,
  77. platform
  78. } = systemInfo
  79. const isIOS = platform === 'ios'
  80. const navigationBarHeight = isIOS ? 44 : 48
  81. return statusBarHeight + navigationBarHeight
  82. },
  83. onEditorReady() {
  84. const that = this
  85. wx.createSelectorQuery().select('#editor').context(function (res) {
  86. that.editorCtx = res.context
  87. that.editorCtx.setContents({
  88. html: that.data.showRichText //将数据写入编辑器内
  89. })
  90. event.on('resetEditor', that, that.onEditorReady.bind(that))
  91. }).exec()
  92. },
  93. compileText(e) {
  94. console.log(e.detail.html)
  95. this.setData({
  96. showRichText: e.detail.html
  97. })
  98. },
  99. blur() {
  100. this.editorCtx.blur()
  101. },
  102. format(e) {
  103. let {
  104. name,
  105. value
  106. } = e.target.dataset
  107. if (!name) return
  108. // console.log('format', name, value)
  109. this.editorCtx.format(name, value)
  110. },
  111. onStatusChange(e) {
  112. const formats = e.detail
  113. this.setData({
  114. formats
  115. })
  116. },
  117. insertDivider() {
  118. this.editorCtx.insertDivider({
  119. success: function () {
  120. console.log('insert divider success')
  121. }
  122. })
  123. },
  124. clear() {
  125. this.editorCtx.clear({
  126. success: function (res) {
  127. console.log("clear success")
  128. }
  129. })
  130. },
  131. removeFormat() {
  132. this.editorCtx.removeFormat()
  133. },
  134. insertDate() {
  135. const date = new Date()
  136. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  137. this.editorCtx.insertText({
  138. text: formatDate
  139. })
  140. },
  141. insertImage() {
  142. const that = this;
  143. wx.chooseImage({
  144. count: 1,
  145. success: function (res) {
  146. console.log(res)
  147. //获取文件后缀
  148. var index = res.tempFiles[0].path.lastIndexOf(".");
  149. var ext = res.tempFiles[0].path.substr(index + 1);
  150. //文件名称
  151. const timestamp = Date.parse(new Date());;
  152. //发送请求
  153. wx.getFileSystemManager().readFile({
  154. filePath: res.tempFiles[0].path,
  155. success: result => {
  156. console.log("临时路径", result)
  157. //返回临时文件路径
  158. const fileData = result.data
  159. _Http.basic({
  160. "accesstoken": wx.getStorageSync('userData').token,
  161. "classname": "system.system.docManage",
  162. "method": "getFileName",
  163. "content": {
  164. "filename": timestamp,
  165. "filetype": ext,
  166. "ownertable": "tagents",
  167. "ownerid": wx.getStorageSync('userData').tagentsid,
  168. "ftype": "richtext"
  169. }
  170. }).then(res => {
  171. console.log("上传成功", res)
  172. if (res.msg != "成功") return wx.showToast({
  173. title: "文件上传失败,请稍后再试",
  174. icon: "none"
  175. });
  176. that.uploadFile(res, fileData)
  177. }).catch(err => {})
  178. },
  179. fail: console.error
  180. })
  181. }
  182. })
  183. },
  184. /* 上传成功反馈 */
  185. uploadFile(res, data) {
  186. console.log("文件上传")
  187. var that = this;
  188. wx.request({
  189. url: res.data.obsuploadurl,
  190. method: "PUT",
  191. data: data,
  192. header: {
  193. 'content-type': 'application/octet-stream' // 默认值
  194. },
  195. success() {
  196. _Http.basic({
  197. "accesstoken": wx.getStorageSync('userData').token,
  198. "classname": "system.system.docManage",
  199. "method": "uploadSuccess",
  200. "content": {
  201. "obsfilename": res.data.obsfilename
  202. }
  203. }).then(res => {
  204. console.log("上传成功反馈", res)
  205. that.editorCtx.insertImage({
  206. src: res.data[0].fobsurl,
  207. data: {
  208. id: res.data[0].tattachmentid,
  209. role: 'god'
  210. },
  211. /* width: '80%', */
  212. success: function () {
  213. console.log('insert image success')
  214. }
  215. })
  216. }).catch(err => {
  217. console.log(err)
  218. })
  219. }
  220. })
  221. },
  222. })