messAdd.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. import { is_show_project } from '../../utils/dict';
  4. const moment = require("../../utils/moment.min")
  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. },
  16. initValidate() {
  17. const rules = { title: { required: true }, create_time: { required: false }, origin: { required: true }, brief: { required: true }, content: { required: false }, is_show: { required: true } }
  18. // 验证字段的提示信息,若不传则调用默认的信息
  19. const messages = { title: { required: '请输入话题名称', }, create_time: { required: '请输入发布时间', }, origin: { required: '请输入信息来源', }, brief: { required: '请输入信息简介' }, content: { required: '请输入信息内容' }, is_show: { required: '请选择是否公开' } };
  20. this.WxValidate = new WxValidate(rules, messages)
  21. },
  22. // 返回
  23. back: function () {
  24. wx.navigateBack({ delta: 1 })
  25. },
  26. //用户输入内容
  27. inputChange: function (e) {
  28. const that = this;
  29. let html = e.detail.html;
  30. that.setData({ "form.content": html });
  31. },
  32. // 是否公开
  33. is_showChange: function (e) {
  34. const that = this;
  35. let index = e.detail.value;
  36. let value = that.data.is_showList[index];
  37. that.setData({ 'form.is_show': value.value });
  38. },
  39. // 提交登录
  40. onSubmit: async function (e) {
  41. const that = this;
  42. const params = e.detail.value;
  43. const data = that.data.form;
  44. params.content = data.content;
  45. params.create_time = moment().format('YYYY-MM-DD');
  46. if (!this.WxValidate.checkForm(params)) {
  47. const error = this.WxValidate.errorList[0];
  48. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  49. return false
  50. } else {
  51. let arr;
  52. if (data._id) {
  53. arr = await app.$post(`/newCourt/api/topic/${data._id}`, params);
  54. }
  55. else {
  56. arr = await app.$post(`/newCourt/api/topic`, params);
  57. }
  58. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  59. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  60. }
  61. },
  62. /**
  63. * 生命周期函数--监听页面加载
  64. */
  65. onLoad: function (options) {
  66. const that = this;
  67. that.setData({ id: options.id || '' })
  68. //验证规则函数
  69. that.initValidate();
  70. // 监听用户是否登录
  71. that.watchLogin();
  72. },
  73. // 监听用户是否登录
  74. watchLogin: async function () {
  75. const that = this;
  76. wx.getStorage({
  77. key: 'user',
  78. success: async res => {
  79. if (that.data.id) {
  80. const arr = await app.$get(`/newCourt/api/topic/${that.data.id}`);
  81. if (arr.errcode == '0') {
  82. that.setData({ form: arr.data });
  83. }
  84. }
  85. },
  86. fail: res => {
  87. wx.redirectTo({ url: '/pages/index/index', })
  88. }
  89. })
  90. },
  91. /**
  92. * 生命周期函数--监听页面初次渲染完成
  93. */
  94. onReady: function () {
  95. },
  96. /**
  97. * 生命周期函数--监听页面显示
  98. */
  99. onShow: function () {
  100. },
  101. /**
  102. * 生命周期函数--监听页面隐藏
  103. */
  104. onHide: function () {
  105. },
  106. /**
  107. * 生命周期函数--监听页面卸载
  108. */
  109. onUnload: function () {
  110. },
  111. /**
  112. * 页面相关事件处理函数--监听用户下拉动作
  113. */
  114. onPullDownRefresh: function () {
  115. },
  116. /**
  117. * 页面上拉触底事件的处理函数
  118. */
  119. onReachBottom: function () {
  120. },
  121. /**
  122. * 用户点击右上角分享
  123. */
  124. onShareAppMessage: function () {
  125. }
  126. })