add.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate';
  3. Page({
  4. data: {
  5. frameStyle: { useTop: true, name: '信息维护', leftArrow: true, useBar: false },
  6. // 赛事列表
  7. matchList: [],
  8. // 赛事组别
  9. groupList: [],
  10. //项目类别
  11. typeList: [],
  12. //性别限制
  13. genderList: [],
  14. id: '',
  15. form: {},
  16. },
  17. initValidate() {
  18. const rules = { match_id: { required: true }, group_id: { required: true }, name: { required: true }, type: { required: true }, age: { required: true }, gender: { required: true }, num: { required: true }, explain: { required: false } }
  19. // 验证字段的提示信息,若不传则调用默认的信息
  20. const messages = { match_id: { required: '赛事名称' }, group_id: { required: '赛事组别' }, name: { required: '项目名称' }, type: { required: '项目类别' }, age: { required: '人数限制' }, gender: { required: '性别限制' }, num: { required: '人数限制' }, explain: { required: '项目说明' } };
  21. this.WxValidate = new WxValidate(rules, messages)
  22. },
  23. // 返回
  24. back: function () {
  25. wx.navigateBack({ delta: 1 })
  26. },
  27. // 赛事名称
  28. matchChange: function (e) {
  29. const that = this;
  30. let data = that.data.matchList[e.detail.value];
  31. if (data) {
  32. that.setData({ 'form.match_id': data._id, 'form.match_name': data.name });
  33. // 查询赛事组别
  34. that.searchGroup(data)
  35. }
  36. },
  37. // 查询赛事组别
  38. searchGroup: async function (e) {
  39. const that = this;
  40. let arr;
  41. arr = await app.$get(`/matchGroup`, { match_id: e._id }, 'race');
  42. if (arr.errcode == '0') { that.setData({ groupList: arr.data }) }
  43. },
  44. // 赛事组别
  45. groupChange: function (e) {
  46. const that = this;
  47. let data = that.data.groupList[e.detail.value];
  48. if (data) { that.setData({ 'form.group_id': data._id, 'form.group_name': data.name }) }
  49. },
  50. // 项目类别
  51. typeChange: function (e) {
  52. const that = this;
  53. let data = that.data.typeList[e.detail.value];
  54. if (data) { that.setData({ 'form.type': data.value, 'form.zhType': data.label }) }
  55. },
  56. // 选择性别
  57. genderChange: function (e) {
  58. const that = this;
  59. let data = that.data.genderList[e.detail.value];
  60. if (data) { that.setData({ 'form.gender': data.value, 'form.zhGender': data.label }) }
  61. },
  62. //提交
  63. onSubmit: async function (e) {
  64. const that = this;
  65. const params = e.detail.value;
  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 (params._id) { arr = await app.$post(`/matchProject/${params._id}`, params, 'race') }
  73. else { arr = await app.$post(`/matchProject`, params, 'race') }
  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: async function (options) {
  82. const that = this;
  83. that.setData({ id: options && options.id || '' })
  84. //验证规则函数
  85. that.initValidate();
  86. // 查询其他信息
  87. await that.searchOther();
  88. // 监听用户是否登录
  89. await that.watchLogin();
  90. },
  91. // 查询其他信息
  92. searchOther: async function () {
  93. const that = this;
  94. let arr;
  95. // 性别
  96. arr = await app.$get(`/dict`, { code: 'match_project_gender' });
  97. if (arr.errcode == '0' && arr.total > 0) that.setData({ genderList: arr.data[0].list });
  98. // 项目类别
  99. arr = await app.$get(`/dict`, { code: 'match_project_type' });
  100. if (arr.errcode == '0' && arr.total > 0) that.setData({ typeList: arr.data[0].list });
  101. },
  102. // 监听用户是否登录
  103. watchLogin: async function () {
  104. const that = this;
  105. let typeList = that.data.typeList;
  106. let genderList = that.data.genderList;
  107. wx.getStorage({
  108. key: 'raceuser',
  109. success: async res => {
  110. let arr = await app.$get(`/match`, { belong_id: res.data._id }, 'race');
  111. if (arr.errcode == '0') that.setData({ matchList: arr.data })
  112. if (that.data.id) {
  113. let arr = await app.$get(`/matchProject/${that.data.id}`, {}, 'race');
  114. if (arr.errcode == '0') {
  115. // 赛事名称
  116. let match = that.data.matchList.find(i => i._id == arr.data.match_id);
  117. if (match) arr.data.match_name = match.name;
  118. // 赛事组别
  119. const group = await app.$get(`/matchGroup/${arr.data.group_id}`, {}, 'race');
  120. if (group.errcode == '0') { arr.data.group_name = group.data.name };
  121. // 项目类别
  122. let type = typeList.find(i => i.value == arr.data.type);
  123. if (type) arr.data.zhType = type.label;
  124. // 性别限制
  125. let gender = genderList.find(i => i.value == arr.data.gender);
  126. if (gender) arr.data.zhGender = gender.label;
  127. that.setData({ form: arr.data })
  128. }
  129. };
  130. },
  131. fail: async res => {
  132. wx.redirectTo({ url: '/pages/index/index' })
  133. }
  134. })
  135. },
  136. /**
  137. * 生命周期函数--监听页面初次渲染完成
  138. */
  139. onReady: function () {
  140. },
  141. /**
  142. * 生命周期函数--监听页面显示
  143. */
  144. onShow: function () {
  145. },
  146. /**
  147. * 生命周期函数--监听页面隐藏
  148. */
  149. onHide: function () {
  150. },
  151. /**
  152. * 生命周期函数--监听页面卸载
  153. */
  154. onUnload: function () {
  155. },
  156. /**
  157. * 页面相关事件处理函数--监听用户下拉动作
  158. */
  159. onPullDownRefresh: function () {
  160. },
  161. /**
  162. * 页面上拉触底事件的处理函数
  163. */
  164. onReachBottom: function () {
  165. },
  166. /**
  167. * 用户点击右上角分享
  168. */
  169. onShareAppMessage: function () {
  170. }
  171. })