var app = getApp() Page({ /** * 页面的初始数据 */ data: { defaultSize: 'mini', photos: [], projectDesc: '', orgId: '', orgName: '', placeholder: "请输入项目施工说明", // 最大输入长度,设置为 -1 的时候不限制最大长度 maxlength: -1, focus: true, weather: '', lat: '', lng: '', address: '', rtuCode: '', rtuName: '', updateRtuLocationChecked: false }, onLoad: function (options) { const type = options.type; var today = new Date(); var year = today.getFullYear() var month = today.getMonth() + 1; month = month < 10 ? '0' + month : month; var day = today.getDate() < 10 ? '0' + today.getDate() : today.getDate(); var hours = today.getHours() < 10 ? '0' + today.getHours() : today.getHours(); var mins = today.getMinutes() < 10 ? '0' + today.getMinutes() : today.getMinutes(); wx.setStorageSync('reportCacheTime', year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':00') if (type == '1') { const img = "/images/add.png"; var tmpphoto = this.data.photos; var tp = {}; tp['path'] = img; tp['type'] = 0; tp['fileName'] = ''; tmpphoto.push(tp); this.setData({ photos: tmpphoto, // orgId: options.orgId, // orgName: options.orgName, weather: options.weather, address: options.address, lng: options.lng, lat: options.lat }); wx.setStorageSync('report_cache_weather', options.weather); wx.setStorageSync('report_cache_address', options.address); wx.setStorageSync('report_cache_lng', options.lng); wx.setStorageSync('report_cache_lat', options.lat); wx.setStorageSync('report_cache_photos', JSON.stringify(tmpphoto)); wx.setStorageSync('report_cache_projectDesc', ''); wx.setStorageSync('report_cache_rtuCode', ''); wx.setStorageSync('report_cache_rtuName', ''); wx.setStorageSync('report_cache_isUpdateRtuLocation', false); } else if (type == '2') { this.reloadReportCache(); } }, reloadReportCache: function () { var tmpphoto = this.data.photos; const photoJson = wx.getStorageSync('report_cache_photos'); if (null != photoJson && photoJson.length > 0) { var cachePhotos = JSON.parse(photoJson); for (var i = 0; i < cachePhotos.length; i++) { tmpphoto.push(cachePhotos[i]); } } const weather = wx.getStorageSync('report_cache_weather'); const address = wx.getStorageSync('report_cache_address'); const lng = wx.getStorageSync('report_cache_lng'); const lat = wx.getStorageSync('report_cache_lat'); const rtuCode = wx.getStorageSync('report_cache_rtuCode'); const rtuName = wx.getStorageSync('report_cache_rtuName'); const projectDesc = wx.getStorageSync('report_cache_projectDesc'); const isUpdateRtuLocaton = wx.getStorageSync('report_cache_isUpdateRtuLocation'); this.setData({ photos: tmpphoto, weather: weather, address: address, lng: lng, lat: lat, rtuCode: rtuCode, rtuName: rtuName, projectDesc: projectDesc, updateRtuLocationChecked: isUpdateRtuLocaton }); }, takePhoto: function (event) { var that = this; if (this.data.rtuCode != null) { wx.showActionSheet({ itemList: ['拍摄', '相册'], success: function (res) { if (!res.cancel) { console.log(res.tapIndex); if (res.tapIndex == 0) { wx.chooseImage({ // 默认9 count: 1, // 可以指定是原图还是压缩图,默认二者都有 sizeType: ['compressed'], // 可以指定来源是相册还是相机,默认二者都有 sourceType: ['camera'], success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 var tempFilePaths = res.tempFilePaths console.debug(tempFilePaths); wx.uploadFile({ url: app.globalData.cloudBase + "/Wx/Project/Implementation/Upload/Image/" + that.data.rtuCode, filePath: tempFilePaths[0], name: 'image', success: function (res) { var data = res.data; var jsonStr = data.replace(" ", ""); var json; if (typeof jsonStr != 'object') { jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点 json = JSON.parse(jsonStr); } if (json.code == "0") { var tmpphoto = that.data.photos; var img = tempFilePaths[0]; console.log("path " + img); var tp = {}; tp['path'] = img; tp['type'] = 1; tp['fileName'] = json.fileId; tmpphoto.unshift(tp); that.setData({ photos: tmpphoto }) wx.setStorageSync('report_cache_photos', JSON.stringify(tmpphoto)); } } }) } }) } else if (res.tapIndex == 1) { wx.chooseImage({ count: 1, // 默认9 sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 var tempFilePaths = res.tempFilePaths console.debug(tempFilePaths); wx.uploadFile({ url: app.globalData.cloudBase + "/Wx/Project/Implementation/Upload/Image/" + that.data.rtuCode, filePath: tempFilePaths[0], name: 'image', success: function (res) { var data = res.data; var jsonStr = data.replace(" ", ""); var json; if (typeof jsonStr != 'object') { jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点 json = JSON.parse(jsonStr); } if (json.code == "0") { console.log(json); var tmpphoto = that.data.photos; var img = tempFilePaths[0]; console.log("path " + img); var tp = {}; tp['path'] = img; tp['type'] = 1; tp['fileName'] = json.fileId; tmpphoto.unshift(tp); that.setData({ photos: tmpphoto }) wx.setStorageSync('report_cache_photos', JSON.stringify(tmpphoto)); } } }) } }) } } } }) } else { wx.showModal({ title: '提示', content: '请先录入RTU编码!', showCancel: false, success: function (res) {} }) } }, selectRtuBackup: function (rtuCode, rtuName) { this.setData({ rtuCode: rtuCode, rtuName: rtuName }) wx.setStorageSync('report_cache_rtuCode', rtuCode); wx.setStorageSync('report_cache_rtuName', rtuName); }, editPhoto: function (event) { }, submitData: function (event) { if (this.data.projectDesc.length > 0 || (this.data.photos.length - 1) > 0) { var that = this; try { wx.showModal({ title: '提示', content: '确定上报项目情况?', success: function (res) { if (res.confirm) { wx.showNavigationBarLoading() var data = {}; data['orgId'] = that.data.orgId; var openid = wx.getStorageSync('openid') if (openid) { data['openid'] = openid; } if (that.data.projectDesc.length > 0) { data['desc'] = that.data.projectDesc; } else { data['desc'] = ''; } if (that.data.address.length > 0) { data['address'] = that.data.address; } else { data['address'] = ''; } data['lat'] = that.data.lat; data['lng'] = that.data.lng; data['weather'] = that.data.weather; data['rtuCode'] = that.data.rtuCode; data['rtuName'] = that.data.rtuName; data['isUpdateRtuLocation'] = that.data.updateRtuLocationChecked; var photos = that.data.photos; var tmpphotos = []; for (var i = 0; i < photos.length; i++) { var photo = photos[i]; if (photo.type !== 0) { var photoobj = { id: photo.fileName, showBoxId: 0 } console.log(photoobj) tmpphotos.push(photoobj); } } data['photos'] = tmpphotos; console.log(data); wx.request({ url: app.globalData.cloudBase + "/Wx/Porject/Report/" + openid, data: data, method: "POST", header: { 'content-type': 'application/json' // 默认值 }, success: function (res) { console.log(res.data) if (res.data.code === '0') { wx.setStorageSync('report_cache_lng', ''); wx.setStorageSync('report_cache_lat', ''); wx.showModal({ title: '提示', content: '上报成功!', showCancel: false, success: function (res) { if (res.confirm) { wx.navigateBack({ delta: 1 }) } } }) } else { wx.showModal({ title: '提示', content: '上报失败!', showCancel: false, success: function (res) {} }) } }, fail: function (res) { console.log(res.errMsg) } }) } else { console.log('用户点击取消') } } }); } catch (e) { console.log(e); } } else { console.debug("sendPhoto fail"); wx.showToast({ title: '请检查是否正确录入项目情况!', icon: 'none', duration: 3000 }) } }, bindKeyInput: function (e) { this.setData({ projectDesc: e.detail.value }) wx.setStorageSync('report_cache_projectDesc', e.detail.value); }, rtuCodeInput: function (e) { console.info(e.detail.value) this.setData({ rtuCode: e.detail.value }) wx.setStorageSync('report_cache_rtuCode', e.detail.value) }, rtuNameInput: function (e) { console.info(e.detail.value) this.setData({ rtuName: e.detail.value }) wx.setStorageSync('report_cache_rtuName', e.detail.value); }, bindAddressKeyInput: function (e) { this.setData({ address: e.detail.value }) wx.setStorageSync('report_cache_address', e.detail.value); }, error(e) { console.log(e.detail) }, toRtuSelectPageCheckHandle: function (e) { wx.navigateTo({ url: '/pages/rtu/projectSelectView?model=1' }); }, updateRtuLocationCheckboxChange: function (e) { console.log('change事件,携带值为', e.detail.value) this.setData({ updateRtuLocationChecked: e.detail.value }) wx.setStorageSync('report_cache_isUpdateRtuLocation', this.data.updateRtuLocationChecked); } })