editor.js 5.9 KB

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