add.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate';
  3. Page({
  4. data: {
  5. frameStyle: { useTop: true, name: '信息维护', leftArrow: true, useBar: false },
  6. form: {},
  7. // 比赛列表
  8. matchList: [],
  9. // 赛事组别
  10. groupList: [],
  11. //项目类别
  12. typeList: [],
  13. //性别限制
  14. genderList: [],
  15. },
  16. initValidate() {
  17. const rules = { match_id: { required: false }, group_id: { required: true }, name: { required: true }, type: { required: true }, age: { required: false }, gender: { required: true }, num: { required: true }, explain: { required: false } }
  18. // 验证字段的提示信息,若不传则调用默认的信息
  19. const messages = { match_id: { required: '比赛赛事' }, group_id: { required: '赛事分组' }, name: { required: '项目名称' }, type: { required: '项目类别' }, age: { required: '人数限制' }, gender: { required: '性别限制' }, num: { required: '人数限制' }, explain: { required: '说明' } };
  20. this.WxValidate = new WxValidate(rules, messages)
  21. },
  22. // 返回
  23. back: function () {
  24. wx.navigateBack({ delta: 1 })
  25. },
  26. // 选择性别
  27. genderChange: function (e) {
  28. const that = this;
  29. let data = that.data.genderList[e.detail.value];
  30. if (data) {
  31. that.setData({ 'form.gender': data.value, 'form.zhGender': data.label });
  32. }
  33. },
  34. // 项目类别
  35. typeChange: function (e) {
  36. const that = this;
  37. let data = that.data.typeList[e.detail.value];
  38. if (data) {
  39. that.setData({ 'form.type': data.value, 'form.zhType': data.label })
  40. }
  41. },
  42. // 赛事名称
  43. matchChange: function (e) {
  44. const that = this;
  45. let data = that.data.matchList[e.detail.value];
  46. if (data) {
  47. that.setData({ 'form.match_id': data._id, 'form.match_name': data.name })
  48. }
  49. },
  50. // 赛事组别
  51. groupChange: function (e) {
  52. const that = this;
  53. let data = that.data.groupList[e.detail.value];
  54. if (data) {
  55. that.setData({ 'form.group_id': data._id, 'form.group_name': data.name })
  56. }
  57. },
  58. //提交
  59. onSubmit: async function (e) {
  60. const that = this;
  61. const params = e.detail.value;
  62. if (!this.WxValidate.checkForm(params)) {
  63. const error = this.WxValidate.errorList[0];
  64. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  65. return false
  66. } else {
  67. let arr;
  68. if (params._id) { arr = await app.$post(`/matchProject/${params._id}`, params, 'race') }
  69. else { arr = await app.$post(`/matchProject`, params, 'race') }
  70. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  71. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  72. }
  73. },
  74. /**
  75. * 生命周期函数--监听页面加载
  76. */
  77. onLoad: async function (options) {
  78. const that = this;
  79. that.setData({ id: options.id || '' })
  80. //验证规则函数
  81. that.initValidate();
  82. // 查询其他信息
  83. await that.searchOther();
  84. // 监听用户是否登录
  85. await that.watchLogin();
  86. },
  87. // 监听用户是否登录
  88. watchLogin: async function () {
  89. const that = this;
  90. let groupList = that.data.groupList;
  91. let typeList = that.data.typeList;
  92. let genderList = that.data.genderList;
  93. wx.getStorage({
  94. key: 'raceuser',
  95. success: async res => {
  96. let arr = await app.$get(`/match`, { belong_id: res.data._id }, 'race');
  97. if (arr.errcode == '0') that.setData({ matchList: arr.data })
  98. if (that.data.id) {
  99. let arr = await app.$get(`/matchProject/${that.data.id}`, {}, 'race');
  100. if (arr.errcode == '0') {
  101. // 比赛名称
  102. let match = that.data.matchList.find(i => i._id == arr.data.match_id);
  103. if (match) arr.data.match_name = match.name;
  104. // 赛事组别
  105. let group = groupList.find(i => i._id == arr.data.group_id);
  106. if (group) arr.data.group_name = group.name;
  107. // 项目类别
  108. let type = typeList.find(i => i.value == arr.data.type);
  109. if (type) arr.data.zhType = type.label;
  110. // 性别限制
  111. let gender = genderList.find(i => i.value == arr.data.gender);
  112. if (gender) arr.data.zhGender = gender.label;
  113. that.setData({ form: arr.data })
  114. }
  115. };
  116. },
  117. fail: async res => {
  118. wx.redirectTo({ url: '/pages/index/index' })
  119. }
  120. })
  121. },
  122. // 查询其他信息
  123. searchOther: async function () {
  124. const that = this;
  125. let arr;
  126. // 性别
  127. arr = await app.$get(`/dict`, { code: 'match_project_gender' });
  128. if (arr.errcode == '0' && arr.total > 0) that.setData({ genderList: arr.data[0].list });
  129. // 项目类别
  130. arr = await app.$get(`/dict`, { code: 'match_project_type' });
  131. if (arr.errcode == '0' && arr.total > 0) that.setData({ typeList: arr.data[0].list });
  132. // 组别
  133. arr = await app.$get(`/matchGroup`, {}, 'race');
  134. if (arr.errcode == '0') that.setData({ groupList: arr.data })
  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. })