const app = getApp() import WxValidate from '../../utils/wxValidate' const moment = require("../../utils/moment.min"); Page({ /** * 页面的初始数据 */ data: { disabled: false, match_id: '', id: '', user: {}, form: {}, info: {}, // 团队 teamList: [], // 成员 member: '', memberList: [] }, // 过滤字典表 getDict(value, model) { const that = this; if (value) { let list = that.data[model + 'List'] let data = list.find(i => i.value == value); if (data) return data.label else return '暂无' } }, // 选择团队 async teamChange(e) { const that = this; const index = e.detail.value; let data = that.data.teamList[index]; if (data) { that.setData({ 'form.team_id': data._id }) that.setData({ 'form.team_name': data.name }) that.setData({ member: '', memberList: [] }) let userList = [] for (const val of data.member) { const member = await app.$api(`user/${val}`, 'GET', {}) if (member.errcode == '0') { userList.push({ _id: member.data._id, name: member.data.name, icon: member.data.icon, phone: member.data.phone, age: member.data.age }) } } userList = userList.filter(i => i.name) that.setData({ userList }) } }, // 选择成员 userChange(e) { const that = this; const memberList = that.data.memberList const index = e.detail.value; let data = that.data.userList[index]; if (data) { const res = memberList.find(i => i._id == data._id) if (!res) { memberList.push(data) that.setData({ memberList, 'form.num': memberList.length.toString() }) that.setData({ member: data.name }) } } }, // 删除成员 async toDel(e) { const that = this; let res = e.currentTarget.dataset.item if (res) { let memberList = that.data.memberList.filter(i => i._id != res._id); that.setData({ memberList, 'form.num': memberList.length.toString() }) } }, // 提交保存 async toSave(e) { const that = this; const parmas = e.detail.value; if (!this.WxValidate.checkForm(parmas)) { const error = that.WxValidate.errorList[0]; wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) return false } else { if (that.data.memberList.length > 0) { parmas.apply_time = moment().format('YYYY-MM-DD HH:mm:ss') parmas.user_id = that.data.memberList parmas.num = that.data.memberList.length.toString() let res if (that.data.id) res = await app.$api(`application/${that.data.id}`, 'POST', parmas); else res = await app.$api('application', 'POST', parmas); if (res.errcode == '0') { wx.showToast({ title: `信息提交成功`, icon: 'success' }); wx.navigateBack({ delta: 1 }); } else wx.showToast({ title: `${res.errmsg}`, icon: 'none' }); } else { wx.showToast({ title: `请选择参加比赛成员`, icon: 'none' }); } } }, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { const that = this; if (options.id) that.setData({ disabled: true }); that.setData({ match_id: options.match, id: options.id }); wx.showLoading({ title: '加载中', mask: true }) //验证规则函数 that.initValidate(); await that.searchUser() await that.search() wx.hideLoading() }, initValidate() { const rules = { match_name: { required: true }, team_name: { required: true } } const messages = { match_name: { required: '请输入比赛名称' }, team_name: { required: '请输入团队名称' } }; this.WxValidate = new WxValidate(rules, messages) }, async searchUser() { const that = this; wx.getStorage({ key: 'user', async success(res) { that.setData({ user: res.data }) that.searchOther() }, fail(err) { // console.log(err); } }) }, // 查询其他信息 async searchOther() { const that = this; let res; // 团队 res = await app.$api('team', 'GET', { administrator: that.data.user._id, status: '1' }) if (res.errcode == '0') that.setData({ teamList: res.data }) }, async search() { const that = this; if (that.data.id) { let arr = await app.$api(`application/${that.data.id}`, 'GET', {}) if (arr.errcode == '0') { let data = that.data.teamList.find(i => i._id == arr.data.team_id); if (data) that.setData({ userList: data.member }) that.setData({ form: arr.data, memberList: arr.data.user_id }) } } else { let aee = await app.$api(`match/${that.data.match_id}`, 'GET', {}) if (aee.errcode == '0') { that.setData({ 'form.match_id': aee.data._id, 'form.match_name': aee.data.name }) } } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })