raceAdd.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. raceTeamList: [],
  19. // 组成员
  20. personList: [],
  21. // 比赛场地
  22. groundList: [],
  23. // 裁判信息
  24. refereeList: [],
  25. },
  26. initValidate() {
  27. const rules = { match_id: { required: true }, grouping_id: { required: true }, project_id: { required: true }, team_id: { required: true }, player_one: { required: true }, player_two: { required: true }, ground_id: { required: true }, referee_id: { required: true }, start_date: { required: true }, start_time: { required: true } }
  28. // 验证字段的提示信息,若不传则调用默认的信息
  29. const messages = { match_id: { required: '请选择赛事名称', }, grouping_id: { required: '请选择赛事组别', }, project_id: { required: '请选择组内项目', }, team_id: { required: '请选择赛程分组', }, player_one: { required: '请选择选手一', }, player_two: { required: '请选择选手二', }, ground_id: { required: '请选择比赛场地', }, referee_id: { required: '请选择裁判', }, start_date: { required: '请选择比赛日期', }, start_time: { required: '请选择比赛时间', } };
  30. this.WxValidate = new WxValidate(rules, messages)
  31. },
  32. back(e) { wx.navigateBack({ delta: 1 }) },
  33. // 选择赛事
  34. matchChange: function (e) {
  35. const that = this;
  36. let data = that.data.matchList[e.detail.value];
  37. if (data) {
  38. that.setData({ 'form.match_id': data._id });
  39. that.setData({ 'form.match_name': data.name });
  40. if (data.grouping && data.grouping.length > 0) {
  41. that.setData({ groupingList: data.grouping })
  42. }
  43. }
  44. },
  45. // 选择赛事组别
  46. grpupChange: async function (e) {
  47. const that = this;
  48. let data = that.data.groupingList[e.detail.value];
  49. if (data) {
  50. that.setData({ 'form.grouping_id': data._id });
  51. that.setData({ 'form.grouping_name': data.name });
  52. if (data.project && data.project.length > 0) {
  53. let projectList = [];
  54. for (const val of data.project) {
  55. const arr = await app.$get(`/newCourt/api/matchProject/${val}`)
  56. if (arr.errcode == '0') projectList.push(arr.data);
  57. that.setData({ projectList: projectList })
  58. }
  59. }
  60. }
  61. },
  62. // 选择组内项目
  63. projectChange: function (e) {
  64. const that = this;
  65. let data = that.data.projectList[e.detail.value];
  66. if (data) {
  67. that.setData({ 'form.project_id': data._id });
  68. that.setData({ 'form.project_name': data.name });
  69. that.setData({ 'form.type': data.type });
  70. // 查询赛事,组别,项目,分组数据
  71. that.searchTeam()
  72. }
  73. },
  74. // 查询分组数据
  75. searchTeam: async function () {
  76. const that = this;
  77. let form = that.data.form;
  78. let info = { match_id: form.match_id, grouping_id: form.grouping_id, project_id: form.project_id }
  79. const arr = await app.$get(`/newCourt/api/raceTeam`, { ...info });
  80. if (arr.errcode == '0') { that.setData({ raceTeamList: arr.data }); }
  81. },
  82. // 选择赛事分组
  83. teamChange: function (e) {
  84. const that = this;
  85. let data = that.data.raceTeamList[e.detail.value];
  86. if (data) {
  87. that.setData({ 'form.team_id': data._id });
  88. that.setData({ 'form.team_name': data.name });
  89. if (data.person && data.person.length > 0) { that.setData({ personList: data.person }) }
  90. }
  91. },
  92. // 选择选手一
  93. playeroneChange: function (e) {
  94. const that = this;
  95. let data = that.data.personList[e.detail.value];
  96. if (data) {
  97. that.setData({ 'form.player_one': data.openid || data.team_id });
  98. that.setData({ 'form.player_one_name': data.name || data.names });
  99. }
  100. },
  101. // 选择选手二
  102. playertwoChange: function (e) {
  103. const that = this;
  104. const form = that.data.form;
  105. let data = that.data.personList[e.detail.value];
  106. if (data && data.openid != form.player_one) {
  107. that.setData({ 'form.player_two': data.openid || data.team_id });
  108. that.setData({ 'form.player_two_name': data.name || data.names });
  109. } else {
  110. wx.showToast({ title: `选手相同`, icon: 'error', duration: 2000 })
  111. }
  112. },
  113. // 比赛场地
  114. groundChange: function (e) {
  115. const that = this;
  116. let data = that.data.groundList[e.detail.value]
  117. if (data) {
  118. that.setData({ 'form.ground_id': data._id });
  119. that.setData({ 'form.ground_name': data.name });
  120. if (data.referee_id) {
  121. let user = that.data.refereeList.find((i) => i.openid == data.referee_id);
  122. if (user) {
  123. that.setData({ 'form.referee_id': user.openid });
  124. that.setData({ 'form.referee_name': user.name });
  125. }
  126. } else {
  127. that.setData({ 'form.referee_id': '' });
  128. that.setData({ 'form.referee_name': '' });
  129. }
  130. }
  131. },
  132. // 裁判
  133. refereeChange: function (e) {
  134. const that = this;
  135. let data = that.data.refereeList[e.detail.value];
  136. if (data) {
  137. that.setData({ 'form.referee_id': data.openid });
  138. that.setData({ 'form.referee_name': data.name });
  139. }
  140. },
  141. // 比赛日期
  142. startdateChange: function (e) {
  143. const that = this;
  144. that.setData({ 'form.start_date': e.detail.value })
  145. },
  146. // 比赛时间
  147. starttimeChange: function (e) {
  148. const that = this;
  149. that.setData({ 'form.start_time': e.detail.value })
  150. },
  151. // 提交保存
  152. onSubmit: async function (e) {
  153. const that = this;
  154. const params = e.detail.value;
  155. if (!this.WxValidate.checkForm(params)) {
  156. const error = this.WxValidate.errorList[0];
  157. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  158. return false
  159. } else {
  160. params.match_time = params.start_date + '-' + params.start_time
  161. let arr;
  162. if (that.data.id) { arr = await app.$post(`/newCourt/api/race/${that.data.id}`, params); }
  163. else { arr = await app.$post(`/newCourt/api/race`, params); }
  164. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  165. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  166. }
  167. },
  168. /**
  169. * 生命周期函数--监听页面加载
  170. */
  171. onLoad: function (options) {
  172. const that = this;
  173. that.setData({ id: options.id || '' });
  174. //验证规则函数
  175. that.initValidate();
  176. that.watchLogin();
  177. },
  178. // 监听用户是否登录
  179. watchLogin: async function () {
  180. const that = this;
  181. wx.getStorage({
  182. key: 'user',
  183. success: async res => {
  184. let arr;
  185. // 数据信息
  186. if (that.data.id) {
  187. arr = await app.$get(`/newCourt/api/race/${that.data.id}`);
  188. if (arr.errcode == '0') {
  189. arr.data.start_date = arr.data.match_time.slice(0, 10);
  190. arr.data.start_time = arr.data.match_time.slice(11, arr.data.match_time.length);
  191. that.setData({ form: arr.data })
  192. }
  193. }
  194. // 比赛信息
  195. arr = await app.$get(`/newCourt/api/match`, { status: '0' });//2
  196. if (arr.errcode == '0') { that.setData({ matchList: arr.data }) }
  197. // 比赛场地
  198. arr = await app.$get(`/newCourt/api/ground`, { is_use: '0' });
  199. if (arr.errcode == '0') { that.setData({ groundList: arr.data }) }
  200. // 裁判信息
  201. arr = await app.$get(`/newCourt/api/user`, { type: '1' });
  202. if (arr.errcode == '0') { that.setData({ refereeList: arr.data }) };
  203. },
  204. fail: res => {
  205. wx.redirectTo({ url: '/pages/index/index', })
  206. }
  207. })
  208. },
  209. /**
  210. * 生命周期函数--监听页面初次渲染完成
  211. */
  212. onReady: function () {
  213. },
  214. /**
  215. * 生命周期函数--监听页面显示
  216. */
  217. onShow: function () { },
  218. /**
  219. * 生命周期函数--监听页面隐藏
  220. */
  221. onHide: function () {
  222. },
  223. /**
  224. * 生命周期函数--监听页面卸载
  225. */
  226. onUnload: function () {
  227. },
  228. /**
  229. * 页面相关事件处理函数--监听用户下拉动作
  230. */
  231. onPullDownRefresh: function () {
  232. },
  233. /**
  234. * 页面上拉触底事件的处理函数
  235. */
  236. onReachBottom: function () {
  237. },
  238. /**
  239. * 用户点击右上角分享
  240. */
  241. onShareAppMessage: function () {
  242. }
  243. })