//引入请求 import { ApiModel } from "../../utils/api" //引入校验文件 import { TestVerify } from "../../utils/verify" const _Http = new ApiModel(); const _Verify = new TestVerify(); Page({ /** * 页面的初始数据 */ data: { fobsurl: {}, //头像列表 userImg: {}, //使用头像地址 fname: "", //用户名 frole: "", //角色 fphonenumber: "", //手机号 fsex: "", //性别 fbirthdate: "", //生日 femail: "", //邮箱 fwechatno: "", //微信 faddress: "", //地址 /* 必填项未填红色显示 */ inputErrRed: { isFname: false, //用户名 isFrole: false, //角色 } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { /* 初始化数据 */ const data = JSON.parse(options.data) this.setData({ fobsurl: data.attinfos, userImg: data.attinfos[data.attinfos.length - 1], fname: data.fname, frole: data.frole, fsex: data.fsex, fbirthdate: data.fbirthdate, femail: data.femail, fwechatno: data.fwechatno, faddress: data.faddress, frole: data.frole, faddress: data.faddress, fphonenumber: data.fphonenumber, }) }, /* 修改用户头像 */ changeUserImg() { var that = this; wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['album', 'camera'], camera: 'back', success(res) { const file = res.tempFiles[0]; var index = file.tempFilePath.lastIndexOf("."); var ext = file.tempFilePath.substr(index + 1); var timestamp = Date.parse(new Date()); wx.getFileSystemManager().readFile({ filePath: file.tempFilePath, // encoding:'utf-8', success: result => { //返回临时文件路径 const fileData = result.data const userid = getApp().globalData.accountList[wx.getStorageSync('userIndex')].userid _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "system.system.docManage", "method": "getFileName", "content": { "filename": 'wx' + timestamp, "filetype": ext, "ownertable": "tenterprise_users", "ownerid": userid, "ftype": "headportrait", } }).then(res => { /* 上次并查询头像 */ that.uploadFile(res, fileData) }).catch(err => { console.log(err) }) }, fail: console.error }) } }) }, /* 上次成功反馈 */ uploadFile(res, data) { var that = this wx.request({ url: res.data.obsuploadurl, method: "PUT", data: data, header: { 'content-type': 'application/octet-stream' // 默认值 }, success() { _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "system.system.docManage", "method": "uploadSuccess", "content": { "obsfilename": res.data.obsfilename } }).then(res => { //删除之前头像 that.imageDelete() that.setData({ fobsurl: res.data[0], userImg: res.data[res.data.length - 1], }) }).catch(err => { console.log(err) }) } }) }, /* 删除替换头像 */ imageDelete() { const userid = getApp().globalData.accountList[wx.getStorageSync('userIndex')].userid for (let i = 0; i < this.data.fobsurl.length; i++) { _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "system.system.docManage", "method": "deleteDoc", "content": { "ownertable": "tenterprise_users", "ownerid": userid, "tattachmentid": this.data.fobsurl[i].tattachmentid } }).then(s => { console.log(s) }) } }, /* input必填项获取焦点 */ inputEssentialFocus(e) { const { name } = e.currentTarget.dataset; let inputErrRed = this.data.inputErrRed; inputErrRed[name] = false; this.setData({ inputErrRed }) }, /* input必填项失去焦点 */ inputEssentialBlur(e) { if (e.detail.value != "" && e.detail.value != " ") return; const { name } = e.currentTarget.dataset; let inputErrRed = this.data.inputErrRed; inputErrRed[name] = true; this.setData({ inputErrRed }) }, /* 校验必填项 */ inputCertain() { let state = true, inputErrRed = this.data.inputErrRed; if (!_Verify.required(this.data.fname)) { inputErrRed["isFname"] = true; state = false; }; if (!_Verify.required(this.data.frole)) { inputErrRed["isFrole"] = true; state = false; }; this.setData({ inputErrRed }); return state; }, /* 表单提交 */ formSubmit() { /* 检测是否还有为空必填项 */ if (!this.inputCertain()) return wx.showToast({ title: '必填项不可为空!', icon: "none" }); /* 发送请求 */ _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "customer.usercenter.usermsg.usermsg", "method": "update_usermsg", "content": { fname: this.data.fname, frole: this.data.frole, fsex: this.data.fsex, fbirthdate: this.data.fbirthdate, femail: this.data.femail, fwechatno: this.data.fwechatno, faddress: this.data.faddress, } }).then(s => { if (s.msg != "成功") return; wx.showToast({ title: '保存成功', }) setTimeout(() => { wx.navigateBack({ delta: 1 }) }, 500) this.setData({ userMsg: s.data[0] }) }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })