messAdd.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. import { is_show_project } from '../../utils/dict';
  4. let util = require('../../utils/util');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  11. id: '',
  12. // 是否使用
  13. is_showList: is_show_project,
  14. form: {},
  15. content: ''
  16. },
  17. initValidate() {
  18. const rules = { title: { required: true }, create_time: { required: false }, origin: { required: true }, brief: { required: true }, content: { required: false }, is_show: { required: true } }
  19. // 验证字段的提示信息,若不传则调用默认的信息
  20. const messages = { title: { required: '请输入话题名称', }, create_time: { required: '请输入发布时间', }, origin: { required: '请输入信息来源', }, brief: { required: '请输入信息简介' }, content: { required: '请输入信息内容' }, is_show: { required: '请选择是否公开' } };
  21. this.WxValidate = new WxValidate(rules, messages)
  22. },
  23. // 返回
  24. back: function () {
  25. wx.navigateBack({ delta: 1 })
  26. },
  27. /**
  28. * 组件的方法列表
  29. */
  30. onEditorReady() {
  31. const that = this
  32. that.createSelectorQuery().select('#editor').context(function (res) {
  33. that.editorCtx = res.context
  34. if (!res) return
  35. that.editorCtx.setContents({
  36. html: that.data.form.content,
  37. })
  38. }).exec()
  39. },
  40. format(e) {
  41. let self = this;
  42. let { name, value } = e.target.dataset;
  43. // 富文本编辑器格式化内容方法
  44. self.editorCtx.format(name, value);
  45. },
  46. setContents(rechtext) {
  47. this.editorCtx.setContents({
  48. html: rechtext,
  49. success: (res) => {
  50. // 富文本内容设置成功
  51. // console.log("[setContents success]", res);
  52. },
  53. });
  54. },
  55. getEditorContent() {
  56. const that = this;
  57. let self = this;
  58. // 富文本编辑器获取内容方法
  59. self.editorCtx.getContents({
  60. success: (res) => {
  61. let array = [];
  62. array["html"] = res.html;
  63. array["index"] = self.properties.index;
  64. // 通过自定义事件把内容传到父组件
  65. self.triggerEvent("getEditorValue", array);
  66. },
  67. });
  68. that.editorCtx.getContents({
  69. success: function (res) {
  70. that.setData({ content: res.html })
  71. },
  72. });
  73. },
  74. insertImage() {
  75. const that = this
  76. wx.chooseImage({
  77. count: 1,
  78. success: function (res) {
  79. that.editorCtx.insertImage({
  80. src: res.tempFilePaths[0],
  81. data: {
  82. id: 'abcd',
  83. role: 'god'
  84. },
  85. width: '80%',
  86. success: function () {
  87. console.log('insert image success')
  88. }
  89. })
  90. }
  91. })
  92. },
  93. // 是否公开
  94. is_showChange: function (e) {
  95. const that = this;
  96. let index = e.detail.value;
  97. let value = that.data.is_showList[index];
  98. that.setData({ 'form.is_show': value.value });
  99. },
  100. // 提交登录
  101. onSubmit: async function (e) {
  102. const that = this;
  103. const params = e.detail.value;
  104. const data = that.data.form;
  105. if (!this.WxValidate.checkForm(params)) {
  106. const error = this.WxValidate.errorList[0];
  107. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  108. return false
  109. } else {
  110. let arr;
  111. let time = util.formatTime(new Date());
  112. time = time.slice(0, -9)
  113. params.content = that.data.content;
  114. if (data._id) {
  115. arr = await app.$post(`/newCourt/api/topic/${data._id}`, params);
  116. }
  117. else {
  118. params.create_time = time;
  119. arr = await app.$post(`/newCourt/api/topic`, params);
  120. }
  121. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  122. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  123. }
  124. },
  125. /**
  126. * 生命周期函数--监听页面加载
  127. */
  128. onLoad: function (options) {
  129. const that = this;
  130. that.setData({ id: options.id || '' })
  131. //验证规则函数
  132. that.initValidate();
  133. // 监听用户是否登录
  134. that.watchLogin();
  135. },
  136. // 监听用户是否登录
  137. watchLogin: async function () {
  138. const that = this;
  139. wx.getStorage({
  140. key: 'user',
  141. success: async res => {
  142. if (that.data.id) {
  143. const arr = await app.$get(`/newCourt/api/topic/${that.data.id}`);
  144. if (arr.errcode == '0') {
  145. that.setData({ form: arr.data });
  146. that.onEditorReady();
  147. }
  148. }
  149. },
  150. fail: res => {
  151. wx.redirectTo({ url: '/pages/index/index', })
  152. }
  153. })
  154. },
  155. /**
  156. * 生命周期函数--监听页面初次渲染完成
  157. */
  158. onReady: function () {
  159. },
  160. /**
  161. * 生命周期函数--监听页面显示
  162. */
  163. onShow: function () {
  164. },
  165. /**
  166. * 生命周期函数--监听页面隐藏
  167. */
  168. onHide: function () {
  169. },
  170. /**
  171. * 生命周期函数--监听页面卸载
  172. */
  173. onUnload: function () {
  174. },
  175. /**
  176. * 页面相关事件处理函数--监听用户下拉动作
  177. */
  178. onPullDownRefresh: function () {
  179. },
  180. /**
  181. * 页面上拉触底事件的处理函数
  182. */
  183. onReachBottom: function () {
  184. },
  185. /**
  186. * 用户点击右上角分享
  187. */
  188. onShareAppMessage: function () {
  189. }
  190. })