index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. const moment = require("../../utils/moment.min");
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. disabled: false,
  10. match_id: '',
  11. id: '',
  12. user: {},
  13. form: {},
  14. info: {},
  15. // 团队
  16. teamList: [],
  17. // 成员
  18. member: '',
  19. memberList: []
  20. },
  21. initValidate() {
  22. const rules = { match_name: { required: true }, team_name: { required: true } }
  23. const messages = { match_name: { required: '请输入比赛名称' }, team_name: { required: '请输入团队名称' } };
  24. this.WxValidate = new WxValidate(rules, messages)
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. async onLoad(options) {
  30. const that = this;
  31. if (options.id) that.setData({ disabled: true });
  32. that.setData({ match_id: options.match, id: options.id });
  33. wx.showLoading({ title: '加载中', mask: true })
  34. //验证规则函数
  35. that.initValidate();
  36. await that.searchOther()
  37. await that.search()
  38. wx.hideLoading()
  39. },
  40. search() {
  41. const that = this;
  42. wx.getStorage({
  43. key: 'token',
  44. async success(res) {
  45. that.setData({ user: res.data })
  46. if (that.data.id) {
  47. let arr = await app.$api(`application/${that.data.id}`, 'GET', {})
  48. if (arr.errcode == '0') {
  49. let data = that.data.teamList.find(i => i._id == arr.data.team_id);
  50. if (data) that.setData({ userList: data.member })
  51. that.setData({ form: arr.data, memberList: arr.data.user_id })
  52. }
  53. } else {
  54. let aee = await app.$api(`match/${that.data.match_id}`, 'GET', {})
  55. if (aee.errcode == '0') {
  56. that.setData({ 'form.match_id': aee.data._id, 'form.match_name': aee.data.name })
  57. }
  58. }
  59. },
  60. fail(err) {
  61. console.log(err);
  62. }
  63. })
  64. },
  65. // 过滤字典表
  66. getDict(value, model) {
  67. const that = this;
  68. if (value) {
  69. let list = that.data[model + 'List']
  70. let data = list.find(i => i.value == value);
  71. if (data) return data.label
  72. else return '暂无'
  73. }
  74. },
  75. // 选择团队
  76. async teamChange(e) {
  77. const that = this;
  78. const index = e.detail.value;
  79. let data = that.data.teamList[index];
  80. if (data) {
  81. that.setData({ 'form.team_id': data._id })
  82. that.setData({ 'form.team_name': data.name })
  83. that.setData({ member: '', memberList: [] })
  84. that.setData({ userList: data.member })
  85. }
  86. },
  87. // 选择成员
  88. userChange(e) {
  89. const that = this;
  90. const memberList = that.data.memberList
  91. const index = e.detail.value;
  92. let data = that.data.userList[index];
  93. if (data) {
  94. const res = memberList.find(i => i._id == data._id)
  95. if (!res) {
  96. memberList.push(data)
  97. that.setData({ memberList, 'form.num': memberList.length.toString() })
  98. that.setData({ member: data.name })
  99. }
  100. }
  101. },
  102. // 删除成员
  103. async toDel(e) {
  104. const that = this;
  105. let res = e.currentTarget.dataset.item
  106. if (res) {
  107. let memberList = that.data.memberList.filter(i => i._id != res._id);
  108. that.setData({ memberList, 'form.num': memberList.length.toString() })
  109. }
  110. },
  111. // 提交保存
  112. async toSave(e) {
  113. const that = this;
  114. const parmas = e.detail.value;
  115. if (!this.WxValidate.checkForm(parmas)) {
  116. const error = that.WxValidate.errorList[0];
  117. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  118. return false
  119. } else {
  120. if (that.data.memberList.length > 0) {
  121. parmas.apply_time = moment().format('YYYY-MM-DD HH:mm:ss')
  122. parmas.user_id = that.data.memberList
  123. parmas.num = that.data.memberList.length.toString()
  124. let res
  125. if (that.data.id) res = await app.$api(`application/${that.data.id}`, 'POST', parmas);
  126. else res = await app.$api('application', 'POST', parmas);
  127. if (res.errcode == '0') {
  128. wx.showToast({ title: `信息提交成功`, icon: 'success' });
  129. wx.navigateBack({ delta: 1 });
  130. } else {
  131. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  132. }
  133. } else {
  134. wx.showToast({ title: `请选择参加比赛成员`, icon: 'none' });
  135. }
  136. }
  137. },
  138. // 查询其他信息
  139. async searchOther() {
  140. const that = this;
  141. let res;
  142. // 团队
  143. res = await app.$api('team', 'GET', { administrator: that.data.user._id, status: '1' })
  144. if (res.errcode == '0') that.setData({ teamList: res.data })
  145. },
  146. /**
  147. * 生命周期函数--监听页面初次渲染完成
  148. */
  149. onReady() {
  150. },
  151. /**
  152. * 生命周期函数--监听页面显示
  153. */
  154. onShow() {
  155. },
  156. /**
  157. * 生命周期函数--监听页面隐藏
  158. */
  159. onHide() {
  160. },
  161. /**
  162. * 生命周期函数--监听页面卸载
  163. */
  164. onUnload() {
  165. },
  166. /**
  167. * 页面相关事件处理函数--监听用户下拉动作
  168. */
  169. onPullDownRefresh() {
  170. },
  171. /**
  172. * 页面上拉触底事件的处理函数
  173. */
  174. onReachBottom() {
  175. },
  176. /**
  177. * 用户点击右上角分享
  178. */
  179. onShareAppMessage() {
  180. }
  181. })