info.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const app = getApp()
  2. const moment = require("../../utils/moment.min")
  3. import WxValidate from '../../utils/wxValidate'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  10. // dialog弹框
  11. dialog: { title: '发帖', show: false, type: '1' },
  12. id: '',
  13. // 详情
  14. info: {},
  15. // 用户信息
  16. user: {},
  17. },
  18. initValidate() {
  19. const rules = { title: { required: true }, content: { required: false } }
  20. // 验证字段的提示信息,若不传则调用默认的信息
  21. const messages = { title: { required: '请输入话题名称', }, content: { required: '请输入信息内容' } };
  22. this.WxValidate = new WxValidate(rules, messages)
  23. },
  24. // 返回
  25. back: function () {
  26. wx.navigateBack({ delta: 1 })
  27. },
  28. // 打开弹框
  29. toDialog: function () {
  30. const that = this;
  31. that.setData({ dialog: { title: '发帖', show: true, type: '1' } })
  32. },
  33. // 关闭弹框
  34. toClose: function () {
  35. const that = this;
  36. that.setData({ dialog: { title: '发帖', show: false, type: '1' } })
  37. },
  38. // 发帖
  39. onSubmit: async function (e) {
  40. const that = this;
  41. const params = e.detail.value;
  42. const user = that.data.user;
  43. params.create_time = moment().format('YYYY-MM-DD');
  44. params.openid = user.openid;
  45. params.name = user.name;
  46. let stick = [...that.data.info.stick, params];
  47. if (!this.WxValidate.checkForm(params)) {
  48. const error = this.WxValidate.errorList[0];
  49. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  50. return false
  51. } else {
  52. const arr = await app.$post(`/newCourt/api/topic/${that.data.id}`, { stick: stick });
  53. if (arr.errcode == '0') {
  54. wx.showToast({ title: `发帖完成`, icon: 'success', duration: 2000 });
  55. that.toClose(); that.watchLogin();
  56. that.setData({ form: {} })
  57. }
  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 || '62ea3510a97c2931cbbec91e' })
  67. //验证规则函数
  68. that.initValidate();
  69. },
  70. // 监听用户是否登录
  71. watchLogin: async function () {
  72. const that = this;
  73. wx.getStorage({
  74. key: 'user',
  75. success: async res => {
  76. this.setData({ user: res.data });
  77. if (that.data.id) {
  78. const arr = await app.$get(`/newCourt/api/topic/${that.data.id}`);
  79. if (arr.errcode == '0') {
  80. that.setData({ info: arr.data });
  81. let content = arr.data.content.replace(/\<img/gi, '<img style="width:100%;height:auto;margin: 5px 0"');
  82. this.setData({ 'info.content': content });
  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. this.watchLogin();
  102. },
  103. /**
  104. * 生命周期函数--监听页面隐藏
  105. */
  106. onHide: function () {
  107. },
  108. /**
  109. * 生命周期函数--监听页面卸载
  110. */
  111. onUnload: function () {
  112. },
  113. /**
  114. * 页面相关事件处理函数--监听用户下拉动作
  115. */
  116. onPullDownRefresh: function () {
  117. },
  118. /**
  119. * 页面上拉触底事件的处理函数
  120. */
  121. onReachBottom: function () {
  122. },
  123. /**
  124. * 用户点击右上角分享
  125. */
  126. onShareAppMessage: function () {
  127. }
  128. })