var app = getApp() Page({ /** * 页面的初始数据 */ data: { defaultSize: 'mini', src: null, photos: [], inputValue: '', orgId: '', orgName: '', // value: "", // 文本的内容 placeholder: "请输入项目施工说明", maxlength: -1, // 最大输入长度,设置为 -1 的时候不限制最大长度 focus: true, weather:'', lat:'', lng:'', address:'' }, onLoad: function (options) { var 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 }); }, takePhoto: function (event) { console.debug("takePhoto"); var that = this; wx.showActionSheet({ itemList: ['拍摄', '相册'], success: function (res) { if (!res.cancel) { console.log(res.tapIndex); if (res.tapIndex == 0) { wx.chooseImage({ count: 1, // 默认9 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.orgId, //仅为示例,非真实的接口地址 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.filename; tmpphoto.unshift(tp); that.setData({ photos: tmpphoto }) } //console.log("upload back " + res.data); // var jsonStr = data.replace(" ", ""); // var json; // if (typeof jsonStr != 'object') { // jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点 // json = JSON.parse(jsonStr); // } // if (json.status === 'ok') { // var tmpphoto = that.data.photos; // var img = tempFilePaths[0]; // console.log("path " + img); // var tp = {}; // tp['path'] = img; // tp['type'] = 1; // tp['fileName'] = json.filename; // tmpphoto.unshift(tp); // that.setData({ // photos: 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); // var tmpphoto = that.data.photos; // var img = tempFilePaths[0]; // var tp = {}; // tp['path'] = img; // tp['type'] = 1; // tp['fileName'] = ''; // tmpphoto.unshift(tp); // that.setData({ // photos: tmpphoto // }) wx.uploadFile({ url: app.globalData.cloudBase + "/Wx/Upload/Image", //仅为示例,非真实的接口地址 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.filename; tmpphoto.unshift(tp); that.setData({ photos: tmpphoto }) } // console.log("upload back " + res.data); // var jsonStr = data.replace(" ", ""); // var json; // if (typeof jsonStr != 'object') { // jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点 // json = JSON.parse(jsonStr); // } // if (json.status === 'ok') { // var tmpphoto = that.data.photos; // var img = tempFilePaths[0]; // console.log("path " + img); // var tp = {}; // tp['path'] = img; // tp['type'] = 1; // tp['fileName'] = json.filename; // tmpphoto.unshift(tp); // that.setData({ // photos: tmpphoto // }) // } } }) } }) } } } }) }, editPhoto: function (event) { console.debug("editPhoto"); wx.navigateBack({ delta: 1 }) }, sendPhoto: function (event) { if (this.data.inputValue.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.inputValue.length > 0) { data['desc'] = that.data.inputValue; } 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; 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: i, showBoxId: 0, photo: photo.fileName } 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.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({ inputValue: e.detail.value }) }, bindAddressKeyInput: function (e) { this.setData({ address: e.detail.value }) }, error(e) { console.log(e.detail) }, })