add.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate';
  3. Page({
  4. data: {
  5. frameStyle: { useTop: true, name: '信息维护', leftArrow: true, useBar: false },
  6. form: {},
  7. },
  8. initValidate() {
  9. const rules = { name: { required: true }, age: { required: false }, explain: { required: false } }
  10. // 验证字段的提示信息,若不传则调用默认的信息
  11. const messages = { name: { required: '未输入名称' }, age: { required: '未输入年龄限制' }, explain: { required: '未输入说明' } };
  12. this.WxValidate = new WxValidate(rules, messages)
  13. },
  14. // 返回
  15. back: function () {
  16. wx.navigateBack({ delta: 1 })
  17. },
  18. //提交
  19. onSubmit: async function (e) {
  20. const that = this;
  21. const params = e.detail.value;
  22. if (!this.WxValidate.checkForm(params)) {
  23. const error = this.WxValidate.errorList[0];
  24. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  25. return false
  26. } else {
  27. let arr;
  28. if (params._id) { arr = await app.$post(`/matchGroup/${params._id}`, params) }
  29. else { arr = await app.$post(`/matchGroup`, params) }
  30. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  31. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  32. }
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad: async function (options) {
  38. const that = this;
  39. that.setData({ id: options.id || '' })
  40. //验证规则函数
  41. that.initValidate();
  42. // 监听用户是否登录
  43. await that.watchLogin();
  44. },
  45. // 监听用户是否登录
  46. watchLogin: async function () {
  47. const that = this;
  48. wx.getStorage({
  49. key: 'user',
  50. success: async res => {
  51. if (that.data.id) {
  52. let arr = await app.$get(`/matchGroup/${that.data.id}`);
  53. if (arr.errcode == '0') {
  54. that.setData({ form: arr.data })
  55. }
  56. };
  57. },
  58. fail: async res => {
  59. wx.redirectTo({ url: '/pages/index/index' })
  60. }
  61. })
  62. },
  63. /**
  64. * 生命周期函数--监听页面初次渲染完成
  65. */
  66. onReady: function () {
  67. },
  68. /**
  69. * 生命周期函数--监听页面显示
  70. */
  71. onShow: function () {
  72. },
  73. /**
  74. * 生命周期函数--监听页面隐藏
  75. */
  76. onHide: function () {
  77. },
  78. /**
  79. * 生命周期函数--监听页面卸载
  80. */
  81. onUnload: function () {
  82. },
  83. /**
  84. * 页面相关事件处理函数--监听用户下拉动作
  85. */
  86. onPullDownRefresh: function () {
  87. },
  88. /**
  89. * 页面上拉触底事件的处理函数
  90. */
  91. onReachBottom: function () {
  92. },
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage: function () {
  97. }
  98. })