add.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 }, rise: { required: true }, remark: { required: false } }
  22. // 验证字段的提示信息,若不传则调用默认的信息
  23. const messages = { match_id: { required: '请选择赛事名称', }, grouping_id: { required: '请选择赛事组别', }, project_id: { required: '请选择组内项目', }, name: { required: '请输入组名' }, rise: { required: '请输入前x名晋级' } };
  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. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  76. },
  77. // 选择组内人员
  78. personChange: function (e) {
  79. const that = this;
  80. const list = that.data.personList;
  81. const type = that.data.form.type;
  82. let person = [];
  83. if (type == '单打') {
  84. for (const val of e.detail.value) { let data = list.find((i) => i.openid == val); if (data) person.push({ ...data, checked: true }) }
  85. } else if (type == '双打') {
  86. for (const val of e.detail.value) { let data = list.find((i) => i.team_id == val); if (data) person.push({ ...data, checked: true }) }
  87. }
  88. that.setData({ 'form.person': person })
  89. },
  90. // 提交保存
  91. onSubmit: async function (e) {
  92. const that = this;
  93. const params = e.detail.value;
  94. const person = that.data.form.person;
  95. if (!this.WxValidate.checkForm(params)) {
  96. const error = this.WxValidate.errorList[0];
  97. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  98. return false
  99. } else {
  100. if (person && person.length > 0) {
  101. params.person = person;
  102. let arr;
  103. if (that.data.id) { arr = await app.$post(`/newCourt/api/raceTeam/${that.data.id}`, params); }
  104. else { arr = await app.$post(`/newCourt/api/raceTeam`, params); }
  105. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  106. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  107. } else {
  108. wx.showToast({ title: `请选择组内人员`, icon: 'error', duration: 2000 })
  109. }
  110. }
  111. },
  112. /**
  113. * 生命周期函数--监听页面加载
  114. */
  115. onLoad: function (options) {
  116. console.log(options);
  117. const that = this;
  118. //验证规则函数
  119. that.initValidate();
  120. that.setData({ id: options.id || '62eb1358d1c7ad6a16b963fb' });//62eb1138d1c7ad6a16b9600a 62eb1358d1c7ad6a16b963fb
  121. that.watchLogin()
  122. },
  123. // 监听用户是否登录
  124. watchLogin: async function () {
  125. const that = this;
  126. wx.getStorage({
  127. key: 'user',
  128. success: async res => {
  129. const match = await app.$get(`/newCourt/api/match`, { status: '0' });//2
  130. if (match.errcode == '0') { that.setData({ matchList: match.data }); };
  131. if (that.data.id) {
  132. const arr = await app.$get(`/newCourt/api/raceTeam/${that.data.id}`);
  133. if (arr.errcode == '0') {
  134. // 比赛信息
  135. let matchInfo = match.data.find((i) => i._id == arr.data.match_id);
  136. if (matchInfo) {
  137. arr.data.match_name = matchInfo.name;
  138. // 赛事组别
  139. let groupingInfo = matchInfo.grouping.find((i) => i._id == arr.data.grouping_id);
  140. if (groupingInfo) { arr.data.grouping_name = groupingInfo.name; }
  141. // 组内项目
  142. let project = await app.$get(`/newCourt/api/matchProject/${arr.data.project_id}`)
  143. if (project.errcode == '0') { arr.data.project_name = project.data.name; arr.data.type = project.data.type }
  144. };
  145. // 查询组内人员
  146. let info = { match_id: arr.data.match_id, grouping_id: arr.data.grouping_id, project_id: arr.data.project_id }
  147. const person = await app.$get(`/newCourt/api/raceTeam/getRangePerson`, { ...info });
  148. let personList = []
  149. if (person.errcode == '0') { personList = person.data }
  150. for (const val of arr.data.person) { val.checked = true };
  151. personList.unshift(...arr.data.person);
  152. that.setData({ personList: personList })
  153. that.setData({ form: arr.data })
  154. }
  155. }
  156. },
  157. fail: res => {
  158. wx.redirectTo({ url: '/pages/index/index', })
  159. }
  160. })
  161. },
  162. /**
  163. * 生命周期函数--监听页面初次渲染完成
  164. */
  165. onReady: function () {
  166. },
  167. /**
  168. * 生命周期函数--监听页面显示
  169. */
  170. onShow: function () {
  171. },
  172. /**
  173. * 生命周期函数--监听页面隐藏
  174. */
  175. onHide: function () {
  176. },
  177. /**
  178. * 生命周期函数--监听页面卸载
  179. */
  180. onUnload: function () {
  181. },
  182. /**
  183. * 页面相关事件处理函数--监听用户下拉动作
  184. */
  185. onPullDownRefresh: function () {
  186. },
  187. /**
  188. * 页面上拉触底事件的处理函数
  189. */
  190. onReachBottom: function () {
  191. },
  192. /**
  193. * 用户点击右上角分享
  194. */
  195. onShareAppMessage: function () {
  196. }
  197. })