add.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. if (params._id) { arr = await app.$post(`/matchGroup/${params._id}`, params, 'race') }
  40. else { arr = await app.$post(`/matchGroup`, params, 'race') }
  41. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  42. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  43. }
  44. },
  45. /**
  46. * 生命周期函数--监听页面加载
  47. */
  48. onLoad: async function (options) {
  49. const that = this;
  50. that.setData({ id: options.id || '' })
  51. //验证规则函数
  52. that.initValidate();
  53. // 查询其他信息
  54. await that.searchOther();
  55. // 监听用户是否登录
  56. await that.watchLogin();
  57. },
  58. searchOther: async function () {
  59. const that = this;
  60. let arr;
  61. arr = await app.$get(`/match`, { status: "0" }, 'race');
  62. if (arr.errcode == '0') that.setData({ matchList: arr.data })
  63. },
  64. // 监听用户是否登录
  65. watchLogin: async function () {
  66. const that = this;
  67. let matchList = that.data.matchList;
  68. wx.getStorage({
  69. key: 'user',
  70. success: async res => {
  71. if (that.data.id) {
  72. let arr = await app.$get(`/matchGroup/${that.data.id}`, {}, 'race');
  73. if (arr.errcode == '0') {
  74. // 比赛名称
  75. let match = matchList.find(i => i._id == arr.data.match_id);
  76. if (match) arr.data.match_name = match.name;
  77. that.setData({ form: arr.data })
  78. }
  79. };
  80. },
  81. fail: async res => {
  82. wx.redirectTo({ url: '/pages/index/index' })
  83. }
  84. })
  85. },
  86. /**
  87. * 生命周期函数--监听页面初次渲染完成
  88. */
  89. onReady: function () {
  90. },
  91. /**
  92. * 生命周期函数--监听页面显示
  93. */
  94. onShow: function () {
  95. },
  96. /**
  97. * 生命周期函数--监听页面隐藏
  98. */
  99. onHide: function () {
  100. },
  101. /**
  102. * 生命周期函数--监听页面卸载
  103. */
  104. onUnload: function () {
  105. },
  106. /**
  107. * 页面相关事件处理函数--监听用户下拉动作
  108. */
  109. onPullDownRefresh: function () {
  110. },
  111. /**
  112. * 页面上拉触底事件的处理函数
  113. */
  114. onReachBottom: function () {
  115. },
  116. /**
  117. * 用户点击右上角分享
  118. */
  119. onShareAppMessage: function () {
  120. }
  121. })