auto.js 6.0 KB

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