add.js 3.6 KB

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