//index.js var common = require('../common/common.js') //获取应用实例 const app = getApp() // const host_url = "https://www.meitan.hongweisoft.com" const host_url = common.base_host Page({ data: { farming:[], farming_index:0, date:'2020-05-12', base:[], base_index:0, executor:'', note:'', files: [], loading:false, showTopTips: false, phone_number:'', image_url:'' }, onLoad: function () { const that = this const eventChanner = this.getOpenerEventChannel() this.setData({ selectFile: this.selectFile.bind(this), uplaodFile: this.uplaodFile.bind(this) }) //请求农事活动列表 this.request_farming() wx.getStorage({ key: 'phone_number', success(res){ console.log(res.data) that.setData({ phone_number:res.data },function(){ //请求基地数据 that.request_base(); }) }, fail(e){ wx.navigateTo({ url: '../login/login', }); } }) }, onShow: function (){ if(this.data.phone_number == ""){ const that = this wx.getStorage({ key: 'phone_number', success(res){ that.setData({ phone_number:res.data },function(){ //请求基地数据 that.request_base(); }) } }) } }, request_base: function(){ if(this.data.phone_number == ""){ return } const that = this wx.request({ url: host_url + '/orchardacqui/orderselect', data:{phone:that.data.phone_number}, success(res){ let data = res.data if(data.retHead.errCode == 0){ that.setData({ base:data.retBody, base_index:0, executor:(data.retBody)[0].leaderName }) }else{ that.setData({ error:"基地获取出错:" + data.retHead.errMsg }) } } }) }, request_farming: function(){ const that = this wx.request({ url: host_url + '/orchardactivity/select', success(res){ let data = res.data if(data.retHead.errCode == 0){ that.setData({ farming:data.retBody, farming_index:0 }) }else{ that.setData({ error:"农事活动列表获取出错:" + data.retHead.errMsg }) } } }) }, bindChangePhone:function(e){ wx.navigateTo({ url: '../login/login', }) }, bindPickerChange: function(e) { console.log('farming picker发送选择改变,携带值为', e.detail.value) this.setData({ farming_index: e.detail.value }) }, bindDateChange: function(e) { console.log('date picker发送选择改变,携带值为', e.detail.value) this.setData({ date: e.detail.value }) }, bindBaseChange: function(e) { console.log('base picker发送选择改变,携带值为', e.detail.value) this.setData({ base_index: e.detail.value, executor:this.data.base[e.detail.value].leaderName }) }, bindExecutor:function(e){ console.log('执行人' , e.detail.value) this.data.executor = e.detail.value }, bindNote: function(e){ console.log('备注说明' , e.detail.value) this.data.note = e.detail.value }, chooseImage: function (e) { var that = this; wx.chooseImage({ sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 that.setData({ files: that.data.files.concat(res.tempFilePaths) }); } }) }, previewImage: function(e){ wx.previewImage({ current: e.currentTarget.id, // 当前显示图片的http链接 urls: this.data.files // 需要预览的图片http链接列表 }) }, selectFile(files) { console.log('files', files) // 返回false可以阻止某次文件上传 }, uplaodFile(files) { console.log('upload files', files) // 文件上传的函数,返回一个promise return new Promise((resolve, reject) => { wx.uploadFile({ filePath: files.tempFilePaths[0], name: 'file', url: host_url + '/file/uploadDocument', success(res){ const data = res.data let dataJ = JSON.parse(data) console.log('body' , dataJ.retBody) resolve({urls:[host_url + '/' + dataJ.retBody.fileUrl]}) }, fail(err){ reject(err) } }) // setTimeout(() => { // //reject('some error') // resolve({urls:['http://mmbiz.qpic.cn/mmbiz_png/VUIF3v9blLsicfV8ysC76e9fZzWgy8YJ2bQO58p43Lib8ncGXmuyibLY7O3hia8sWv25KCibQb7MbJW3Q7xibNzfRN7A/0']}) // }, 1000) }) }, uploadError(e) { console.log('upload error', e.detail) }, uploadSuccess(e) { console.log('upload success', e.detail) let pic_url = e.detail.urls[0] pic_url = pic_url.replace(host_url , "") this.setData({ image_url:pic_url }) }, submitForm(e){ console.log("上传数据" , "farming:" + this.data.farming[this.data.farming_index] + " date:" + this.data.date ) const that = this //验证数据 let error_msg = "" if(this.data.executor == null || this.data.executor.trim() == ""){ error_msg = "执行人不能为空" }else if(this.data.phone_number == null || this.data.phone_number == ""){ error_msg = "手机号不能为空" } if( error_msg != ""){ this.setData({ error:error_msg }) return } //显示加载框 this.setData({ loading:true }) wx.request({ url: host_url + '/orchardacqui', method:"POST", header:{ 'content-type': 'application/json', }, data:{ activityId:this.data.farming[this.data.farming_index].id, executionTime:this.data.date + " 00:00:00", tOrchardId:this.data.base[this.data.base_index].id, executionUser:this.data.executor, remarks:this.data.note, imgUrl:this.data.image_url, phone:this.data.phone_number }, success(res){ that.setData({ loading:false }) let data = res.data if(data.retHead.errCode == 0){ wx.showToast({ title: '提交成功', icon: 'success', duration: 2000 }) }else{ that.setData({ error:data.retHead.errMsg, }) } }, fail(error){ that.setData({ error:error.errMsg, loading:false }) } }) //提示 } })