add.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_name: { required: true }, group_name: { required: true }, age: { required: false }, gender: { required: false }, num: { required: false }, explain: { required: false } }
  18. // 验证字段的提示信息,若不传则调用默认的信息
  19. const messages = { name: { required: '未输入名称' }, match_name: { required: '未选择比赛赛事' }, group_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 });
  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. wx.getStorage({
  95. key: 'user',
  96. success: async res => {
  97. let match = await app.$get(`/match`, {}, 'race');
  98. if (match.errcode == '0') that.setData({ matchList: match.data })
  99. let group = await app.$get(`/matchGroup`, {}, 'race');
  100. if (group.errcode == '0') that.setData({ groupList: group.data })
  101. if (that.data.id) {
  102. let arr = await app.$get(`/matchProject/${that.data.id}`, 'race');
  103. if (arr.errcode == '0') {
  104. // 项目类别
  105. let type = typeList.find(i => i.value == arr.data.type);
  106. if (type) arr.data.zhType = type.label;
  107. // 性别
  108. let gender = genderList.find(i => i.value == arr.data.gender);
  109. if (gender) arr.data.zhGender = gender.label;
  110. that.setData({ form: arr.data })
  111. }
  112. };
  113. },
  114. fail: async res => {
  115. wx.redirectTo({ url: '/pages/index/index' })
  116. }
  117. })
  118. },
  119. // 查询其他信息
  120. searchOther: async function () {
  121. const that = this;
  122. let arr;
  123. // 性别
  124. arr = await app.$get(`/dict`, { code: 'gender' });
  125. if (arr.errcode == '0' && arr.total > 0) that.setData({ genderList: arr.data[0].list });
  126. // 项目类别
  127. arr = await app.$get(`/dict`, { code: 'match_project_type' });
  128. if (arr.errcode == '0' && arr.total > 0) that.setData({ typeList: arr.data[0].list });
  129. },
  130. /**
  131. * 生命周期函数--监听页面初次渲染完成
  132. */
  133. onReady: function () {
  134. },
  135. /**
  136. * 生命周期函数--监听页面显示
  137. */
  138. onShow: function () {
  139. },
  140. /**
  141. * 生命周期函数--监听页面隐藏
  142. */
  143. onHide: function () {
  144. },
  145. /**
  146. * 生命周期函数--监听页面卸载
  147. */
  148. onUnload: function () {
  149. },
  150. /**
  151. * 页面相关事件处理函数--监听用户下拉动作
  152. */
  153. onPullDownRefresh: function () {
  154. },
  155. /**
  156. * 页面上拉触底事件的处理函数
  157. */
  158. onReachBottom: function () {
  159. },
  160. /**
  161. * 用户点击右上角分享
  162. */
  163. onShareAppMessage: function () {
  164. }
  165. })