add.js 9.9 KB

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