// pages/dailyreport/index.js const api = require('../../api/index.js'); Page({ /** * 页面的初始数据 */ data: { fileList: [], // 用于存储选中的xlsx文件路径 file:[], powerindex:'', datestart:'', powerdata:[], filedata:[] }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.getpowerdata() }, getpowerstationdatalog(){ let data = { powerstationId:this.data.powerindex==''?'':this.data.powerdata[this.data.powerindex].powerstationId } api.request(`/syspowerstation/selectpowerstationdatalog`, 'POST',data) .then((res) => { console.log(res); this.setData({ powerstationdata:res.data }) }) .catch((err) => { console.error('请求失败:', err); // 在这里处理请求失败的情况 }); }, // 上传xlsx文档--点击触发 bindtoXlsx() { let _this = this // 先检查隐私授权状态 wx.getPrivacySetting({ success: (res) => { if (res.needAuthorization) { // 弹出隐私协议弹窗 wx.requirePrivacyAuthorize({ success: () => { console.log('用户已同意隐私协议'); _this.chooseXlsxFile(); // 继续执行上传 }, fail: () => { console.log('用户拒绝了隐私协议'); wx.showToast({ title: '需同意隐私协议才能使用', icon: 'none' }); } }); } else { _this.chooseXlsxFile(); // 已授权,直接上传 } } }); }, // 选择xlsx文件 chooseXlsxFile() { let _this = this wx.chooseMessageFile({ count: 1, // 只允许选择1个文件 type: 'file', // 选择文件类型 extension: ['xlsx'], // 限制只选择xlsx格式 success(res) { console.log('选中的文件:', res); const tempFiles = res.tempFiles; // 存储选中的文件路径 _this.setData({ fileList: tempFiles.map(file => file.path), filedata:tempFiles }); console.log(_this.data.fileList); // 上传到服务器 // _this.uploadXlsxFile(tempFiles[0]); // 因为count=1,直接取第一个 _this.setData({ file:tempFiles[0] }) }, fail(err) { console.error("文件选择失败:", err); wx.showToast({ title: '文件选择失败', icon: 'none' }); } }) }, // 上传xlsx到服务器 uploadXlsxFile() { console.log(api.baseUrl); let _this = this if (_this.data.powerindex==''||_this.data.datestart==''||_this.data.file.length<1) { wx.showToast({ title: '请填写电站与上传月份', icon: 'none' }); return } const token = wx.getStorageSync('token'); // 显示加载中提示 wx.showLoading({ title: '上传中...', }) console.log(_this.data.file); wx.uploadFile({ // url: 'http://192.168.8.102:9999/syspowerstation/insertexcel', url: `${api.baseUrl}/syspowerstation/insertexcel`, filePath: _this.data.file.path, method:'POST', name: 'fis', // 与后端接收参数名保持一致 formData: { 'powerstationId':_this.data.powerdata[_this.data.powerindex].powerstationId, 'currentYearMonth':_this.data.datestart, 'fisname': _this.data.file.name, // 传递文件名 'fileType': 'xlsx' // 传递文件类型 }, header: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'multipart/form-data' }, success: (res) => { wx.hideLoading(); console.log('上传成功响应:', res); try { const obj = JSON.parse(res.data); if (obj.code === 200) { // 假设200为成功状态码 wx.showToast({ title: '上传成功', icon: 'success' }); // 根据实际返回数据结构存储文件信息 _this.getpowerstationdatalog() } else { wx.showToast({ title: obj.msg || '上传失败', icon: 'none' }); } } catch (e) { console.error('解析响应失败:', e); wx.showToast({ title: '上传失败', icon: 'none' }); } }, fail: (err) => { wx.hideLoading(); console.error('上传失败:', err); wx.showToast({ title: '网络错误,上传失败', icon: 'none' }); } }); }, bindpowerChange(e){ console.log('picker发送选择改变,携带值为', e.detail.value) this.setData({ powerindex: e.detail.value }) this.getpowerstationdatalog() }, // 电站 getpowerdata(){ api.request(`/syspowerstation/selectpowestation`, 'POST') .then((res) => { console.log(res); this.setData({ powerdata:res.data }) this.getpowerstationdatalog() }) .catch((err) => { console.error('请求失败:', err); // 在这里处理请求失败的情况 }); }, // 选择时间 bindstartChange: function(e) { console.log('picker发送选择改变,携带值为', e.detail.value) this.setData({ datestart: e.detail.value }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })