const app = getApp() import WxValidate from '../../utils/wxValidate' Page({ /** * 页面的初始数据 */ data: { form: {}, // 性别 genderList: [], // 类别 typeList: [], }, // 过滤字典表 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 '暂无' } }, // 选择性别 genderChange(e) { const that = this; const index = e.detail.value; let data = that.data.genderList[index]; if (data) { that.setData({ 'form.gender': data.value }) that.setData({ 'form.gender_name': data.label }) } }, // 选择类别 typeChange(e) { const that = this; const index = e.detail.value; let data = that.data.typeList[index]; if (data) { that.setData({ 'form.type': data.value }) that.setData({ 'form.type_name': data.label }) } }, // 提交保存 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 { // 判断id使用 let form = that.data.form; let res; if (form._id) res = await app.$api(`user/${form._id}`, 'POST', parmas); else res = await app.$api('user', 'POST', parmas); if (res.errcode == '0') { wx.showToast({ title: `信息提交成功`, icon: 'success' }); wx.setStorage({ key: "user", data: res.data })// 存用户信息到storage,以便之后判断用户是否登录 wx.navigateBack({ delta: 1 }); } else { wx.showToast({ title: `${res.errmsg}`, icon: 'none' }); } } }, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { const that = this; wx.showLoading({ title: '加载中', mask: true }) //验证规则函数 await that.initValidate(); await that.searchOther() await that.search() wx.hideLoading() }, initValidate() { const rules = { name: { required: true }, phone: { required: true, tel: true }, gender: { required: true }, age: { required: true } } const messages = { name: { required: '请输入姓名' }, phone: { required: '请输入手机号' }, gender: { required: '请选择性别' }, age: { required: '请输入年龄' } }; this.WxValidate = new WxValidate(rules, messages) }, // 查询其他信息 async searchOther() { const that = this; let res; // 性别 res = await app.$api('dictData', 'GET', { type: 'gender', is_use: '0' }) if (res.errcode == '0') that.setData({ genderList: res.data }) // 类别 res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' }) if (res.errcode == '0') that.setData({ typeList: res.data }) }, search() { const that = this; wx.getStorage({ key: 'user', async success(res) { let form = {} let aee = await app.$api(`user/${res.data._id}`, 'GET', {}) if (aee.errcode == '0') { form = aee.data; if (form && form._id) { // 性别 if (form.gender) form.gender_name = that.getDict(form.gender, 'gender') // 工作状态 if (form.type) form.type_name = that.getDict(form.type, 'type') } } else { wx.showToast({ title: `${aee.errmsg}`, icon: 'error' }); } that.setData({ form }) }, fail(err) { // console.log(err); } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })