// pages/login/login.js import WxValidate from '../../utils/wxValidate' const app = getApp() Page({ /** * 页面的初始数据 */ data: { frameStyle: { useTop: true, name: '团队信息管理', leftArrow: true, useBar: false }, // 主体高度 infoHeight: '', // LOGO logo: [], // 团队成员 members: [], form: {}, // 团队类型 typeList: ['羽毛球'], // 用户列表 userList: [], // 弹框 dialog: { title: '选择成员', show: false, type: '1' } }, //验证必填项 initValidate() { const rules = { name: { required: true }, type: { required: true }, create_user: { required: true }, } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { name: { required: '团队名称' }, type: { required: '团队类型' }, create_user: { required: '团队创建人' }, }; this.WxValidate = new WxValidate(rules, messages) }, back: function () { wx.navigateBack({ url: '/pages/me/index' }) }, // 上傳圖片 imgUpload: function (e) { const that = this; let data = that.data.logo; data.push(e.detail) that.setData({ logo: data }) }, // 删除图片 imgDel: function (e) { const that = this; let list = that.data.logo; let arr = list.filter((i, index) => index != e.detail.index) that.setData({ logo: arr }) }, // 选择团队类型 typeChange: function (e) { const that = this; let data = that.data.typeList[e.detail.value]; if (data) that.setData({ 'form.type': data }) }, // 选择创建时间 dataChange: function (e) { const that = this; let value = e.detail.value; that.setData({ 'form.create_time': value }) }, // 选择成员 memAdd() { const that = this; that.setData({ dialog: { title: '选择成员', show: true, type: '1' } }) }, // 选择成员 memChange: function (e) { const that = this; let data = e.detail.value; let user = that.data.userList; let members = []; for (const val of data) { let arr = user.find((i) => i._id == val); if (arr) members.push({ user_id: arr._id, nickname: arr.nickname, icon: arr.icon }) } that.setData({ members: members }) }, // 提交保存 memSubmit: function () { const that = this; that.toClose() }, // 删除成员 memDel: function (e) { const that = this; const { user_id } = e.currentTarget.dataset; var members = that.data.members; for (var i = 0; i < members.length; i++) { if (members[i].user_id == user_id) members.splice(i, 1) } this.setData({ members: members }) }, // 关闭弹框 toClose() { const that = this; that.setData({ dialog: { title: '选择成员', show: false, type: '1' } }) }, // 取消保存 toReset: function () { const that = this; that.back() }, // 提交保存 onSubmit: async function (e) { const that = this; const params = e.detail.value; const data = this.data.form; if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) return false } else { let arr; if (data._id) { if (!params.logo) params.logo = that.data.logo; if (!params.members) params.members = that.data.members; arr = await app.$post(`/courtAdmin/api/team/${data._id}`, params); if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); } else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) } else { params.logo = that.data.logo; params.members = that.data.members; arr = await app.$post(`/courtAdmin/api/team`, params); if (arr.errcode == '0') { wx.showToast({ title: `添加信息完成`, icon: 'success', duration: 2000 }); that.back(); } else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) } } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 计算高度 this.searchHeight(); //验证规则函数 this.initValidate(); // 监听用户是否登录 this.watchLogin(); }, // 监听用户是否登录 watchLogin: function () { const that = this; wx.getStorage({ key: 'token', success: async res => { that.setData({ form: { create_id: res.data._id, create_user: res.data.nickname } }); const arr = await app.$get(`/courtAdmin/api/user`, { status: '1', type: '2' }); if (arr.errcode == '0') that.setData({ userList: arr.data }) }, fail: res => { wx.redirectTo({ url: '/pages/login/index', }) } }) }, // 计算高度 searchHeight: function () { let frameStyle = this.data.frameStyle; let client = app.globalData.client; let infoHeight = client.windowHeight; // 是否去掉状态栏 if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2); // 是否减去底部菜单 if (frameStyle.useBar) infoHeight = infoHeight - 50; if (infoHeight) this.setData({ infoHeight: infoHeight }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })