messAdd.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. },
  16. initValidate() {
  17. const rules = { title: { required: true }, create_time: { required: false }, origin: { required: true }, brief: { required: true }, content: { required: true }, 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. is_showChange: function (e) {
  28. const that = this;
  29. let index = e.detail.value;
  30. let value = that.data.is_showList[index];
  31. that.setData({ 'form.is_show': value.value });
  32. },
  33. // 提交登录
  34. onSubmit: async function (e) {
  35. const that = this;
  36. const params = e.detail.value;
  37. const data = that.data.form;
  38. if (!this.WxValidate.checkForm(params)) {
  39. const error = this.WxValidate.errorList[0];
  40. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  41. return false
  42. } else {
  43. let arr;
  44. let time = util.formatTime(new Date());
  45. time = time.slice(0, -3)
  46. if (data._id) { arr = await app.$post(`/newCourt/api/topic/${data._id}`, params); }
  47. else {
  48. params.create_time = time;
  49. arr = await app.$post(`/newCourt/api/topic`, params);
  50. }
  51. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  52. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  53. }
  54. },
  55. /**
  56. * 生命周期函数--监听页面加载
  57. */
  58. onLoad: function (options) {
  59. const that = this;
  60. that.setData({ id: options.id || '' })
  61. //验证规则函数
  62. that.initValidate();
  63. // 监听用户是否登录
  64. that.watchLogin();
  65. },
  66. // 监听用户是否登录
  67. watchLogin: async function () {
  68. const that = this;
  69. wx.getStorage({
  70. key: 'user',
  71. success: async res => {
  72. if (that.data.id) {
  73. const arr = await app.$get(`/newCourt/api/topic/${that.data.id}`);
  74. if (arr.errcode == '0') {
  75. that.setData({ form: arr.data });
  76. }
  77. }
  78. },
  79. fail: res => {
  80. wx.redirectTo({ url: '/pages/index/index', })
  81. }
  82. })
  83. },
  84. /**
  85. * 生命周期函数--监听页面初次渲染完成
  86. */
  87. onReady: function () {
  88. },
  89. /**
  90. * 生命周期函数--监听页面显示
  91. */
  92. onShow: function () {
  93. },
  94. /**
  95. * 生命周期函数--监听页面隐藏
  96. */
  97. onHide: function () {
  98. },
  99. /**
  100. * 生命周期函数--监听页面卸载
  101. */
  102. onUnload: function () {
  103. },
  104. /**
  105. * 页面相关事件处理函数--监听用户下拉动作
  106. */
  107. onPullDownRefresh: function () {
  108. },
  109. /**
  110. * 页面上拉触底事件的处理函数
  111. */
  112. onReachBottom: function () {
  113. },
  114. /**
  115. * 用户点击右上角分享
  116. */
  117. onShareAppMessage: function () {
  118. }
  119. })