// pages/createFile/createFile.js import { createDocument } from "../../api/document"; import { userUploadHeadImg } from '../../api/upload' Page({ /** * 页面的初始数据 */ data: { from: '', choseAvatar: false, choseAvaSrc: '', username: '', sexIndex: '0', relateIndex: 1, pickerText: '请选择', relativeText: '请选择', relativeRange: ['本人','父母', '子女', '配偶', '朋友', '祖孙', '兄妹', '其他'] // 关系 关系 1.本人 2.父母 3.子女 4.配偶 5.朋友 6.祖孙、7.兄妹、8.其他 }, onLoad (options) { this.setData({ from: options.from }) }, bindUserNameInput (e) { this.setData({ username: e.detail.value }) }, /** * 保存 */ handleSave() { var that = this if (this.data.choseAvaSrc == '') { wx.showToast({ title: '请选择头像', icon: 'error' }) return } if (this.data.username == '') { wx.showToast({ title: '请输入姓名', icon: 'error' }) return } if (this.data.pickerText == '请选择') { wx.showToast({ title: '请选择生日', icon: 'error' }) return } if (this.data.relativeText == '请选择') { wx.showToast({ title: '请选择关系', icon: 'error' }) return } wx.showModal({ title: '温馨提示', content: '新建健康档案时,请您如实填写信息,如填写不正确将会影响您的健康指标!', confirmColor: '#333', cancelColor: '#666', success (res) { if (res.confirm) { that.submitCreate() } } }) }, // 提交新建人员表单 submitCreate () { var that = this var data = { realName: this.data.username, sex: this.data.sexIndex == 0 ? true : false, birthday: this.data.pickerText, relationship: this.data.relateIndex, headImg: this.data.choseAvaSrc } wx.showLoading({ title: '加载中...', mask: true }) createDocument(data).then(res => { wx.hideLoading() wx.showToast({ title: '保存成功', icon: 'success' }) var timer = setTimeout(() => { if (that.data.from == 'buy') { wx.navigateBack({ delta: 2 }) } else { wx.navigateBack() } clearTimeout(timer) }, 1500); }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, /** * 选择关系 */ bindReaPickerChange(e) { console.log(e); var value = this.data.relativeRange[e.detail.value] var selsectKey = Number(e.detail.value) + 1 console.log(selsectKey); this.setData({ relativeText: value, relateIndex: selsectKey }) }, /** * 用户选择生日 */ bindPickerChange(e) { this.setData({ pickerText: e.detail.value }) }, /** * 切换性别 */ handleSex(e) { console.log(e.currentTarget.dataset.index); this.setData({ sexIndex: e.currentTarget.dataset.index }) }, /** * 用户选择头像 */ handleChoseAvatar() { var that = this wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['album'], success (res) { console.log(res); // tempFilePath可以作为 img 标签的 src 属性显示图片 const tempFilePaths = res.tempFiles[0].tempFilePath that.uploadImg(tempFilePaths) } }) }, // 上传用户头像到服务器 uploadImg (filePath) { var that = this var data = { filePath: filePath } wx.showLoading({ title: '上传中...', mask: true }) userUploadHeadImg(data).then(res => { wx.hideLoading() var response = JSON.parse(res) if (response.code != '000') { wx.showToast({ title: '头像上传失败', icon: 'error' }) } else { that.setData({ choseAvatar: true, choseAvaSrc: response.data }) } }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) } })