add.js 4.1 KB

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