add.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 matchList = that.data.matchList;
  91. let groupList = that.data.groupList;
  92. let typeList = that.data.typeList;
  93. let genderList = that.data.genderList;
  94. wx.getStorage({
  95. key: 'user',
  96. success: async res => {
  97. if (that.data.id) {
  98. let arr = await app.$get(`/matchProject/${that.data.id}`, {}, 'race');
  99. if (arr.errcode == '0') {
  100. // 比赛名称
  101. let match = matchList.find(i => i._id == arr.data.match_id);
  102. if (match) arr.data.match_name = match.name;
  103. // 赛事组别
  104. let group = groupList.find(i => i._id == arr.data.group_id);
  105. if (group) arr.data.group_name = group.name;
  106. // 项目类别
  107. let type = typeList.find(i => i.value == arr.data.type);
  108. if (type) arr.data.zhType = type.label;
  109. // 性别限制
  110. let gender = genderList.find(i => i.value == arr.data.gender);
  111. if (gender) arr.data.zhGender = gender.label;
  112. that.setData({ form: arr.data })
  113. }
  114. };
  115. },
  116. fail: async res => {
  117. wx.redirectTo({ url: '/pages/index/index' })
  118. }
  119. })
  120. },
  121. // 查询其他信息
  122. searchOther: async function () {
  123. const that = this;
  124. let arr;
  125. // 性别
  126. arr = await app.$get(`/dict`, { code: 'match_project_gender' });
  127. if (arr.errcode == '0' && arr.total > 0) that.setData({ genderList: arr.data[0].list });
  128. // 项目类别
  129. arr = await app.$get(`/dict`, { code: 'match_project_type' });
  130. if (arr.errcode == '0' && arr.total > 0) that.setData({ typeList: arr.data[0].list });
  131. // 比赛
  132. arr = await app.$get(`/match`, { status: "0" }, 'race');
  133. if (arr.errcode == '0') that.setData({ matchList: arr.data })
  134. // 组别
  135. arr = await app.$get(`/matchGroup`, {}, 'race');
  136. if (arr.errcode == '0') that.setData({ groupList: arr.data })
  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. })