add.js 4.9 KB

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