123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- // 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
- })
- })
- }
- })
|