// pages/createFile/createFile.js import { createDocument } from "../../api/document"; Page({ /** * 页面的初始数据 */ data: { choseAvatar: false, choseAvaSrc: '', username: '', sexIndex: '0', relateIndex: 1, pickerText: '请选择', relativeText: '请选择', relativeRange: ['本人','父母', '子女', '配偶', '朋友'] // 关系 关系 1.本人 2.父母 3.子女 4.配偶 5.朋友 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, bindUserNameInput (e) { this.setData({ username: e.detail.value }) }, /** * 保存 */ handleSave() { 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 } 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 } console.log(data); wx.showLoading({ title: '加载中...', mask: true }) createDocument(data).then(res => { wx.hideLoading() wx.showToast({ title: '保存成功', icon: 'success' }) wx.navigateBack() }).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.setData({ choseAvatar: true, choseAvaSrc: tempFilePaths }) } }) } })