index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate';
  3. import { format, match_status } from '../../utils/dict';
  4. const app = getApp()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. frameStyle: { useTop: true, name: '比赛信息管理', leftArrow: true, useBar: false },
  11. // 主体高度
  12. infoHeight: '',
  13. form: {},
  14. // 赛制
  15. formatList: format,
  16. // 状态
  17. statusList: match_status,
  18. // 赛制
  19. format: [],
  20. id: ''
  21. },
  22. initValidate() {
  23. const rules = { name: { required: true }, start_time: { required: true }, end_time: { required: true }, address: { required: true } }
  24. // 验证字段的提示信息,若不传则调用默认的信息
  25. const messages = { name: { required: '请输入比赛名称' }, start_time: { required: '请选择开始时间' }, end_time: { required: '请选择结束时间' }, address: { required: '请输入比赛地点' } };
  26. this.WxValidate = new WxValidate(rules, messages)
  27. },
  28. back: function () {
  29. wx.navigateBack({ delta: 1 })
  30. },
  31. // 选择比赛开始时间
  32. dateChange: function (e) {
  33. const that = this;
  34. const { type } = e.currentTarget.dataset;
  35. const { value } = e.detail;
  36. if (type == 'start') that.setData({ 'form.start_time': value })
  37. else if (type == 'end') that.setData({ 'form.end_time': value })
  38. },
  39. // 选择赛制
  40. formatChange: function (e) {
  41. const that = this;
  42. const format = that.data.format;
  43. const { value } = e.detail;
  44. let list = [...format];
  45. let data = that.data.formatList[value];
  46. if (data) {
  47. if (format && format.length > 0) {
  48. let is_data = format.find((i) => i.label == data.label);
  49. if (!is_data) list.push(data)
  50. } else list.push(data)
  51. that.setData({ format: list })
  52. }
  53. },
  54. // 删除赛制
  55. toDel: function (e) {
  56. const that = this;
  57. let list = that.data.format;
  58. const { index } = e.currentTarget.dataset;
  59. list.splice(index, 1);
  60. that.setData({ format: list });
  61. },
  62. // 选择状态
  63. statusChange: function (e) {
  64. const that = this;
  65. const { value } = e.detail;
  66. that.setData({ 'form.status': value })
  67. },
  68. // 提交比赛
  69. onSubmit: async function (e) {
  70. const that = this;
  71. const params = e.detail.value;
  72. params.match_time = params.start_time + '-' + params.end_time;
  73. const format = that.data.format;
  74. params.format = format;
  75. const id = that.data.id;
  76. if (!this.WxValidate.checkForm(params)) {
  77. const error = this.WxValidate.errorList[0];
  78. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  79. return false
  80. } else {
  81. if (params.format && params.format.length > 0) {
  82. let arr;
  83. if (id) arr = await app.$post(`/courtAdmin/api/match/${id}`, params);
  84. else arr = await app.$post(`/courtAdmin/api/match`, params);
  85. if (arr.errcode == '0') { wx.showToast({ title: `维护比赛完成`, icon: 'success', duration: 2000 }); that.back(); }
  86. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  87. } else wx.showToast({ title: `请选择赛制`, icon: 'error', duration: 2000 })
  88. }
  89. },
  90. /**
  91. * 生命周期函数--监听页面加载
  92. */
  93. onLoad: function (options) {
  94. const that = this;
  95. if (options && options.id) that.setData({ id: options.id })
  96. //验证规则函数
  97. that.initValidate();
  98. // 计算高度
  99. that.searchHeight();
  100. // 监听用户是否登录
  101. that.watchLogin();
  102. },
  103. // 监听用户是否登录
  104. watchLogin: async function () {
  105. const that = this;
  106. let id = that.data.id;
  107. wx.getStorage({
  108. key: 'token',
  109. success: async res => {
  110. if (id) {
  111. const arr = await app.$get(`/courtAdmin/api/match/${id}`);
  112. if (arr.errcode == '0') {
  113. // 处理时间
  114. let data = arr.data;
  115. data.start_time = data.match_time.substring(0, 10);
  116. data.end_time = data.match_time.substring(11, data.match_time.length);
  117. that.setData({ format: data.format });
  118. that.setData({ form: data });
  119. }
  120. }
  121. },
  122. fail: res => {
  123. wx.redirectTo({ url: '/pages/index/index', })
  124. }
  125. })
  126. },
  127. // 计算高度
  128. searchHeight: function () {
  129. let frameStyle = this.data.frameStyle;
  130. let client = app.globalData.client;
  131. let infoHeight = client.windowHeight;
  132. // 是否去掉状态栏
  133. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  134. // 是否减去底部菜单
  135. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  136. if (infoHeight) this.setData({ infoHeight: infoHeight })
  137. },
  138. /**
  139. * 生命周期函数--监听页面初次渲染完成
  140. */
  141. onReady: function () {
  142. },
  143. /**
  144. * 生命周期函数--监听页面显示
  145. */
  146. onShow: function () {
  147. },
  148. /**
  149. * 生命周期函数--监听页面隐藏
  150. */
  151. onHide: function () {
  152. },
  153. /**
  154. * 生命周期函数--监听页面卸载
  155. */
  156. onUnload: function () {
  157. },
  158. /**
  159. * 页面相关事件处理函数--监听用户下拉动作
  160. */
  161. onPullDownRefresh: function () {
  162. },
  163. /**
  164. * 页面上拉触底事件的处理函数
  165. */
  166. onReachBottom: function () {
  167. },
  168. /**
  169. * 用户点击右上角分享
  170. */
  171. onShareAppMessage: function () {
  172. }
  173. })