teamAdd.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. frameStyle: { useTop: true, name: '组项信息', leftArrow: true, useBar: false },
  9. id: '',
  10. form: {},
  11. // 赛事信息
  12. matchList: [],
  13. // 组别
  14. groupingList: [],
  15. //组内项目
  16. projectList: [],
  17. // 组内人员
  18. personList: []
  19. },
  20. initValidate() {
  21. const rules = { match_id: { required: true }, grouping_id: { required: true }, project_id: { required: true }, name: { required: true }, remark: { required: false } }
  22. // 验证字段的提示信息,若不传则调用默认的信息
  23. const messages = { match_id: { required: '请选择赛事名称', }, grouping_id: { required: '请选择赛事组别', }, project_id: { required: '请选择组内项目', }, name: { required: '请输入组名' } };
  24. this.WxValidate = new WxValidate(rules, messages)
  25. },
  26. back(e) { wx.navigateBack({ delta: 1 }) },
  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 });
  33. that.setData({ 'form.match_name': data.name });
  34. if (data.grouping && data.grouping.length > 0) {
  35. that.setData({ groupingList: data.grouping })
  36. }
  37. }
  38. },
  39. // 选择赛事组别
  40. grpupChange: async function (e) {
  41. const that = this;
  42. let data = that.data.groupingList[e.detail.value];
  43. if (data) {
  44. that.setData({ 'form.grouping_id': data._id });
  45. that.setData({ 'form.grouping_name': data.name });
  46. if (data.project && data.project.length > 0) {
  47. let projectList = [];
  48. for (const val of data.project) {
  49. const arr = await app.$get(`/newCourt/api/matchProject/${val}`)
  50. if (arr.errcode == '') projectList.push(arr.data);
  51. that.setData({ projectList: projectList })
  52. }
  53. }
  54. }
  55. },
  56. // 选择组内项目
  57. projectChange: function (e) {
  58. const that = this;
  59. let data = that.data.projectList[e.detail.value];
  60. if (data) {
  61. that.setData({ 'form.project_id': data._id });
  62. that.setData({ 'form.project_name': data.name });
  63. that.setData({ 'form.type': data.type });
  64. // 查询组内人员
  65. that.searchPerson();
  66. }
  67. },
  68. // 查询组内人员
  69. searchPerson: async function (e) {
  70. const that = this;
  71. const form = that.data.form;
  72. let info = { match_id: form.match_id, grouping_id: form.grouping_id, project_id: form.project_id }
  73. const arr = await app.$get(`/newCourt/api/raceTeam/getRangePerson`, { ...info });
  74. if (arr.errcode == '0') { that.setData({ personList: arr.data }) }
  75. },
  76. // 选择组内人员
  77. personChange: function (e) {
  78. const that = this;
  79. const list = that.data.personList;
  80. const type = that.data.form.type;
  81. let person = [];
  82. if (type == '单打') {
  83. for (const val of e.detail.value) { let data = list.find((i) => i.openid == val); if (data) person.push(data) }
  84. } else if (type == '双打') {
  85. for (const val of e.detail.value) { let data = list.find((i) => i.team_id == val); if (data) person.push(data) }
  86. }
  87. that.setData({ 'form.person': person })
  88. },
  89. // 提交保存
  90. onSubmit: async function (e) {
  91. const that = this;
  92. const params = e.detail.value;
  93. const person = that.data.form.person;
  94. if (!this.WxValidate.checkForm(params)) {
  95. const error = this.WxValidate.errorList[0];
  96. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  97. return false
  98. } else {
  99. if (person && person.length > 0) {
  100. params.person = person;
  101. let arr;
  102. if (that.data.id) { arr = await app.$post(`/newCourt/api/raceTeam/${that.data.id}`, params); }
  103. else { arr = await app.$post(`/newCourt/api/raceTeam`, params); }
  104. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  105. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  106. } else {
  107. wx.showToast({ title: `请选择组内人员`, icon: 'error', duration: 2000 })
  108. }
  109. }
  110. },
  111. /**
  112. * 生命周期函数--监听页面加载
  113. */
  114. onLoad: function (options) {
  115. const that = this;
  116. //验证规则函数
  117. that.initValidate();
  118. that.setData({ id: options.id || '' });
  119. that.watchLogin()
  120. },
  121. // 监听用户是否登录
  122. watchLogin: async function () {
  123. const that = this;
  124. wx.getStorage({
  125. key: 'user',
  126. success: async res => {
  127. const match = await app.$get(`/newCourt/api/match`, { status: '0' });//2
  128. if (match.errcode == '0') { that.setData({ matchList: match.data }); };
  129. if (that.data.id) {
  130. const arr = await app.$get(`/newCourt/api/raceTeam/${that.data.id}`);
  131. if (arr.errcode == '0') {
  132. // 比赛信息
  133. let matchInfo = match.data.find((i) => i._id == arr.data.match_id);
  134. if (matchInfo) {
  135. arr.data.match_name = matchInfo.name;
  136. // 赛事组别
  137. let groupingInfo = matchInfo.grouping.find((i) => i._id == arr.data.grouping_id);
  138. if (groupingInfo) { arr.data.grouping_name = groupingInfo.name; }
  139. // 组内项目
  140. let project = await app.$get(`/newCourt/api/matchProject/${arr.data.project_id}`)
  141. if (project.errcode == '0') { arr.data.project_name = project.data.name; arr.data.type = project.data.type }
  142. };
  143. // 查询组内人员
  144. let info = { match_id: arr.data.match_id, grouping_id: arr.data.grouping_id, project_id: arr.data.project_id }
  145. const person = await app.$get(`/newCourt/api/raceTeam/getRangePerson`, { ...info });
  146. let personList = [];
  147. if (person.errcode == '0') { personList = [...person.data] }
  148. for (const val of arr.data.person) { val.checked = true };
  149. personList.unshift(...arr.data.person);
  150. that.setData({ personList: personList })
  151. that.setData({ form: arr.data })
  152. }
  153. }
  154. },
  155. fail: res => {
  156. wx.redirectTo({ url: '/pages/index/index', })
  157. }
  158. })
  159. },
  160. /**
  161. * 生命周期函数--监听页面初次渲染完成
  162. */
  163. onReady: function () {
  164. },
  165. /**
  166. * 生命周期函数--监听页面显示
  167. */
  168. onShow: function () {
  169. },
  170. /**
  171. * 生命周期函数--监听页面隐藏
  172. */
  173. onHide: function () {
  174. },
  175. /**
  176. * 生命周期函数--监听页面卸载
  177. */
  178. onUnload: function () {
  179. },
  180. /**
  181. * 页面相关事件处理函数--监听用户下拉动作
  182. */
  183. onPullDownRefresh: function () {
  184. },
  185. /**
  186. * 页面上拉触底事件的处理函数
  187. */
  188. onReachBottom: function () {
  189. },
  190. /**
  191. * 用户点击右上角分享
  192. */
  193. onShareAppMessage: function () {
  194. }
  195. })