add.js 6.4 KB

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