index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. team_id: "",
  9. id: '',
  10. form: {},
  11. // 状态
  12. statusList: [],
  13. userList: [],
  14. red_disabled: false,
  15. blue_disabled: false
  16. },
  17. // 过滤字典表
  18. getDict(value, model) {
  19. const that = this;
  20. if (model == 'winner') {
  21. if (value) {
  22. let list = that.data[model + 'List']
  23. let data = list.find(i => i._id == value);
  24. if (data) return data.name
  25. else return '暂无'
  26. }
  27. } else {
  28. if (value) {
  29. let list = that.data[model + 'List']
  30. let data = list.find(i => i.value == value);
  31. if (data) return data.label
  32. else return '暂无'
  33. }
  34. }
  35. },
  36. // 选择状态
  37. statusChange(e) {
  38. const that = this;
  39. const index = e.detail.value;
  40. let data = that.data.statusList[index];
  41. if (data) {
  42. that.setData({ 'form.status': data.value })
  43. that.setData({ 'form.status_name': data.label })
  44. }
  45. },
  46. // 提交保存
  47. async toSave(e) {
  48. const that = this;
  49. const parmas = e.detail.value;
  50. if (!this.WxValidate.checkForm(parmas)) {
  51. const error = that.WxValidate.errorList[0];
  52. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  53. return false
  54. } else {
  55. // 判断id使用
  56. let form = that.data.form;
  57. let res;
  58. if (form._id) res = await app.$api(`course/${form._id}`, 'POST', parmas);
  59. else res = await app.$api('course', 'POST', parmas);
  60. if (res.errcode == '0') {
  61. wx.showToast({ title: `信息提交成功`, icon: 'success' });
  62. wx.navigateBack({ delta: 1 });
  63. } else {
  64. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  65. }
  66. }
  67. },
  68. /**
  69. * 生命周期函数--监听页面加载
  70. */
  71. async onLoad(options) {
  72. const that = this;
  73. that.setData({ id: options.id, team_id: options.team_id });
  74. wx.showLoading({ title: '加载中', mask: true })
  75. //验证规则函数
  76. that.initValidate();
  77. await that.searchOther()
  78. await that.search()
  79. wx.hideLoading()
  80. },
  81. initValidate() {
  82. const rules = { red_score: { required: true }, blue_score: { required: true }, status: { required: true } }
  83. const messages = { red_score: { required: '请输入红方比分' }, blue_score: { required: '请输入蓝方比分' }, status: { required: '请选择状态' } };
  84. this.WxValidate = new WxValidate(rules, messages)
  85. },
  86. // 查询其他信息
  87. async searchOther() {
  88. const that = this;
  89. let res;
  90. // 状态
  91. res = await app.$api('dictData', 'GET', { type: 'course_status', is_use: '0' })
  92. if (res.errcode == '0') that.setData({ statusList: res.data })
  93. // 团队
  94. res = await app.$api('team', 'GET', { status: '1' })
  95. if (res.errcode == '0') that.setData({ userList: res.data })
  96. },
  97. search() {
  98. const that = this;
  99. wx.getStorage({
  100. key: 'user',
  101. async success(res) {
  102. let form = {}
  103. let aee = await app.$api(`course/${that.data.id}`, 'GET', {})
  104. if (aee.errcode == '0') {
  105. form = aee.data;
  106. if (form && form._id) {
  107. if (form.red_team_id == that.data.team_id) that.setData({ blue_disabled: true })
  108. else if (form.blue_team_id == that.data.team_id) that.setData({ red_disabled: true })
  109. // 状态
  110. if (form.status) form.status_name = that.getDict(form.status, 'status')
  111. }
  112. } else {
  113. wx.showToast({ title: `${aee.errmsg}`, icon: 'error' });
  114. }
  115. that.setData({ form })
  116. },
  117. fail(err) {
  118. // console.log(err);
  119. }
  120. })
  121. },
  122. /**
  123. * 生命周期函数--监听页面初次渲染完成
  124. */
  125. onReady() {
  126. },
  127. /**
  128. * 生命周期函数--监听页面显示
  129. */
  130. onShow() {
  131. },
  132. /**
  133. * 生命周期函数--监听页面隐藏
  134. */
  135. onHide() {
  136. },
  137. /**
  138. * 生命周期函数--监听页面卸载
  139. */
  140. onUnload() {
  141. },
  142. /**
  143. * 页面相关事件处理函数--监听用户下拉动作
  144. */
  145. onPullDownRefresh() {
  146. },
  147. /**
  148. * 页面上拉触底事件的处理函数
  149. */
  150. onReachBottom() {
  151. },
  152. /**
  153. * 用户点击右上角分享
  154. */
  155. onShareAppMessage() {
  156. }
  157. })