matchadd.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. dateChange: function (e) {
  43. const that = this;
  44. that.setData({ 'form.sign_date': e.detail.value });
  45. },
  46. // 选择报名截止时间
  47. timeChange: function (e) {
  48. const that = this;
  49. that.setData({ 'form.sign_time': e.detail.value });
  50. },
  51. // 上传图片
  52. imgUpl: function (e) {
  53. const that = this;
  54. let data = that.data.logo;
  55. data.push(e.detail)
  56. that.setData({ logo: data })
  57. },
  58. // 删除图片
  59. imgDel: function (e) {
  60. const that = this;
  61. let list = that.data.logo;
  62. let arr = list.filter((i, index) => index != e.detail.index)
  63. that.setData({ logo: arr })
  64. },
  65. onSubmit: async function (e) {
  66. const that = this;
  67. const params = e.detail.value;
  68. params.sign_end_time = params.sign_date + '-' + params.sign_time;
  69. const logo = that.data.logo;
  70. if (!this.WxValidate.checkForm(params)) {
  71. const error = this.WxValidate.errorList[0];
  72. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  73. return false
  74. } else {
  75. if (logo && logo.length > 0) {
  76. params.logo = logo;
  77. let arr;
  78. if (that.data.id) arr = await app.$post(`/newCourt/api/match/${that.data.id}`, params);
  79. else arr = await app.$post('/newCourt/api/match', params);
  80. if (arr.errcode == '0') {
  81. wx.showToast({ title: `信息维护成功`, icon: 'success', duration: 2000 });
  82. that.back()
  83. } else {
  84. wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 });
  85. }
  86. } else { wx.showToast({ title: `请上传logo`, icon: 'fail', duration: 2000 }); }
  87. }
  88. },
  89. /**
  90. * 生命周期函数--监听页面加载
  91. */
  92. onLoad: function (options) {
  93. const that = this;
  94. that.setData({ id: options.id || '62e388d4fd61d871861b80af' })
  95. //验证规则函数
  96. that.initValidate();
  97. // 监听用户是否登录
  98. that.watchLogin();
  99. },
  100. watchLogin: function () {
  101. const that = this;
  102. wx.getStorage({
  103. key: 'user',
  104. success: async (res) => {
  105. if (that.data.id) {
  106. const arr = await app.$get(`/newCourt/api/match/${that.data.id}`);
  107. if (arr.errcode == '0') {
  108. if (arr.data.sign_end_time) {
  109. arr.data.sign_date = arr.data.sign_end_time.substring(0, 10);
  110. arr.data.sign_time = arr.data.sign_end_time.substring(11, arr.data.sign_end_time.length);
  111. }
  112. that.setData({ form: arr.data })
  113. that.setData({ logo: arr.data.logo })
  114. } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); }
  115. }
  116. },
  117. fail: async (res) => {
  118. wx.redirectTo({ url: '/pages/index/index' });
  119. },
  120. });
  121. },
  122. /**
  123. * 生命周期函数--监听页面初次渲染完成
  124. */
  125. onReady: function () {
  126. },
  127. /**
  128. * 生命周期函数--监听页面显示
  129. */
  130. onShow: function () {
  131. },
  132. /**
  133. * 生命周期函数--监听页面隐藏
  134. */
  135. onHide: function () {
  136. },
  137. /**
  138. * 生命周期函数--监听页面卸载
  139. */
  140. onUnload: function () {
  141. },
  142. /**
  143. * 页面相关事件处理函数--监听用户下拉动作
  144. */
  145. onPullDownRefresh: function () {
  146. },
  147. /**
  148. * 页面上拉触底事件的处理函数
  149. */
  150. onReachBottom: function () {
  151. },
  152. /**
  153. * 用户点击右上角分享
  154. */
  155. onShareAppMessage: function () {
  156. }
  157. })