add.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const app = getApp();
  2. import WxValidate from '../../utils/wxValidate';
  3. import { matchType } from '../../utils/dict';
  4. Page({
  5. data: {
  6. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  7. id: '',
  8. form: { logo: [] },
  9. typeList: matchType,
  10. },
  11. initValidate() {
  12. const rules = { logo: { required: true }, 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 } }
  13. // 验证字段的提示信息,若不传则调用默认的信息
  14. const messages = { logo: { required: '请上传logo' }, 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: '请输入赛事规程' } };
  15. this.WxValidate = new WxValidate(rules, messages)
  16. },
  17. back: function () {
  18. wx.navigateBack({ delta: 1 })
  19. },
  20. // 上传图片
  21. imgUpl: function (e) {
  22. const that = this;
  23. let data = that.data.form.logo;
  24. data.push(e.detail)
  25. that.setData({ 'form.logo': data })
  26. },
  27. // 删除图片
  28. imgDel: function (e) {
  29. const that = this;
  30. let list = that.data.form.logo;
  31. let arr = list.filter((i, index) => index != e.detail.index)
  32. that.setData({ 'form.logo': arr })
  33. },
  34. // 选择比赛类别
  35. typeChange: function (e) {
  36. const that = this;
  37. let data = that.data.typeList[e.detail.value];
  38. if (data) that.setData({ 'form.type': data.label })
  39. },
  40. // 选择比赛开始时间
  41. startChange: function (e) {
  42. const that = this;
  43. that.setData({ 'form.start_time': e.detail.value });
  44. },
  45. // 选择比赛结束时间
  46. endChange: function (e) {
  47. const that = this;
  48. that.setData({ 'form.end_time': e.detail.value });
  49. },
  50. // 选择报名截止日期
  51. dateChange: function (e) {
  52. const that = this;
  53. that.setData({ 'form.sign_date': e.detail.value });
  54. },
  55. // 选择报名截止时间
  56. timeChange: function (e) {
  57. const that = this;
  58. that.setData({ 'form.sign_time': e.detail.value });
  59. },
  60. toSubmit: async function (e) {
  61. const that = this;
  62. const form = that.data.form;
  63. const params = e.detail.value;
  64. params.sign_end_time = params.sign_date + '-' + params.sign_time;
  65. params.logo = form.logo;
  66. if (!this.WxValidate.checkForm(params)) {
  67. const error = this.WxValidate.errorList[0];
  68. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  69. return false
  70. } else {
  71. let arr;
  72. if (form._id) arr = await app.$post(`/newCourt/api/match/${form._id}`, params);
  73. else arr = await app.$post(`/newCourt/api/match`, params);
  74. if (arr.errcode == '0') { wx.showToast({ title: `维护信息成功`, icon: 'success', duration: 2000 }); that.back() }
  75. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  76. }
  77. },
  78. /**
  79. * 生命周期函数--监听页面加载
  80. */
  81. onLoad: function (options) {
  82. const that = this;
  83. that.setData({ id: options.id || '' })
  84. //验证规则函数
  85. that.initValidate();
  86. // 监听用户是否登录
  87. that.watchLogin();
  88. },
  89. // 监听用户是否登录
  90. watchLogin: async function () {
  91. const that = this;
  92. wx.getStorage({
  93. key: 'user',
  94. success: async res => {
  95. let arr;
  96. if (that.data.id) {
  97. arr = await app.$get(`/newCourt/api/match/${that.data.id}`);
  98. if (arr.errcode == '0') {
  99. if (arr.data.sign_end_time) {
  100. arr.data.sign_date = arr.data.sign_end_time.substring(0, 10);
  101. arr.data.sign_time = arr.data.sign_end_time.substring(11, arr.data.sign_end_time.length);
  102. }
  103. that.setData({ form: arr.data });
  104. }
  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. })