matchadd.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { matchType } from '../../utils/dict';
  2. import WxValidate from '../../utils/wxValidate';
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '信息维护', leftArrow: true, useBar: false },
  10. typeList: matchType,
  11. form: {},
  12. logo: [],
  13. id: ''
  14. },
  15. initValidate() {
  16. const rules = { type: { required: true }, name: { required: true }, start_time: { required: true }, end_time: { required: true }, address: { required: true }, sign_end_time: { required: true }, money_remark: { required: true }, money_mode: { required: true }, contact: { required: true }, explain: { required: true }, regular: { required: true } }
  17. // 验证字段的提示信息,若不传则调用默认的信息
  18. const messages = { type: { required: '请选择比赛类别' }, name: { required: '请输入比赛名称' }, start_time: { required: '比赛开始时间' }, end_time: { required: '比赛结束时间' }, address: { required: '请输入比赛地点' }, sign_end_time: { required: '报名截止时间' }, money_remark: { required: '请输入报名费用说明' }, money_mode: { required: '请输入付款方式' }, contact: { required: '请输入联系方式' }, explain: { required: '请输入报名说明' }, regular: { required: '请输入赛事规程' } };
  19. this.WxValidate = new WxValidate(rules, messages)
  20. },
  21. // 跳转菜单
  22. back(e) {
  23. wx.navigateBack({ delta: 1 })
  24. },
  25. // 选择比赛类别
  26. typeChange: function (e) {
  27. const that = this;
  28. let data = that.data.typeList[e.detail.value];
  29. if (data) that.setData({ 'form.type': data.label })
  30. },
  31. // 选择比赛开始时间
  32. startChange: function (e) {
  33. const that = this;
  34. that.setData({ 'form.start_time': e.detail.value });
  35. },
  36. // 选择比赛结束时间
  37. endChange: function (e) {
  38. const that = this;
  39. that.setData({ 'form.end_time': e.detail.value });
  40. },
  41. // 选择报名截止时间
  42. signendChange: function (e) {
  43. const that = this;
  44. that.setData({ 'form.sign_end_time': e.detail.value });
  45. },
  46. // 上传图片
  47. imgUpl: function (e) {
  48. const that = this;
  49. let data = that.data.logo;
  50. data.push(e.detail)
  51. that.setData({ logo: data })
  52. },
  53. // 删除图片
  54. imgDel: function (e) {
  55. const that = this;
  56. let list = that.data.logo;
  57. let arr = list.filter((i, index) => index != e.detail.index)
  58. that.setData({ logo: arr })
  59. },
  60. onSubmit: async function (e) {
  61. const that = this;
  62. const params = e.detail.value;
  63. const logo = that.data.logo;
  64. if (!this.WxValidate.checkForm(params)) {
  65. const error = this.WxValidate.errorList[0];
  66. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  67. return false
  68. } else {
  69. if (logo && logo.length > 0) {
  70. params.logo = logo;
  71. let arr;
  72. if (that.data.id) arr = await app.$post(`/newCourt/api/match/${that.data.id}`, params);
  73. else arr = await app.$post('/newCourt/api/match', params);
  74. if (arr.errcode == '0') {
  75. wx.showToast({ title: `信息维护成功`, icon: 'success', duration: 2000 });
  76. that.back()
  77. } else {
  78. wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 });
  79. }
  80. } else { wx.showToast({ title: `请上传logo`, icon: 'fail', duration: 2000 }); }
  81. }
  82. },
  83. /**
  84. * 生命周期函数--监听页面加载
  85. */
  86. onLoad: function (options) {
  87. const that = this;
  88. that.setData({ id: options.id || '' })
  89. //验证规则函数
  90. that.initValidate();
  91. // 监听用户是否登录
  92. that.watchLogin();
  93. },
  94. watchLogin: function () {
  95. const that = this;
  96. wx.getStorage({
  97. key: 'user',
  98. success: async (res) => {
  99. if (that.data.id) {
  100. const arr = await app.$get(`/newCourt/api/match/${that.data.id}`);
  101. if (arr.errcode == '0') {
  102. that.setData({ form: arr.data })
  103. that.setData({ logo: arr.data.logo })
  104. } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); }
  105. }
  106. },
  107. fail: async (res) => {
  108. wx.redirectTo({ url: '/pages/index/index' });
  109. },
  110. });
  111. },
  112. /**
  113. * 生命周期函数--监听页面初次渲染完成
  114. */
  115. onReady: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面显示
  119. */
  120. onShow: function () {
  121. },
  122. /**
  123. * 生命周期函数--监听页面隐藏
  124. */
  125. onHide: function () {
  126. },
  127. /**
  128. * 生命周期函数--监听页面卸载
  129. */
  130. onUnload: function () {
  131. },
  132. /**
  133. * 页面相关事件处理函数--监听用户下拉动作
  134. */
  135. onPullDownRefresh: function () {
  136. },
  137. /**
  138. * 页面上拉触底事件的处理函数
  139. */
  140. onReachBottom: function () {
  141. },
  142. /**
  143. * 用户点击右上角分享
  144. */
  145. onShareAppMessage: function () {
  146. }
  147. })