index.js 5.7 KB

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