Page({ data: { isEditing: false, tableName: 'current_reduce', // 确保这个表名在页面中是有效的 fileFormat: 'Excel', // 默认是 Excel 格式 shoopingtext: "", // 搜索框内容 filteredRows: [], // 表格过滤后的数据 rows: [], // 所有表格数据 showModal: false, // 控制底部编辑删除弹窗的显示与否 showAddModal: false, // 控制新增数据弹窗的显示与否 currentRow: null, // 当前选中的表格行 // 新增数据弹窗的输入框内容 newData: { id: "", Q_over_b: "", pH: "", OM: "", CL: "", H: "", Al: "" }, // 中文表头内容,动态生成 tableHeaders: [ "序号", "Q/ΔpH", "初始pH", "有机质含量", "土壤粘粒", "交换性氢","交换性铝" ], unit: [ " ", " ", " ", "(g/kg)", "(g/kg)", "(cmol/kg)", "(cmol/kg)" ] }, // 页面加载时获取表格数据 onLoad: function() { this.LoadData(); }, LoadData: function() { wx.request({ url: 'https://soilgd.com:5000/table', method: 'POST', header: { 'Content-Type': 'application/json' }, data: { table: 'current_reduce' }, success: (res) => { console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式 if (res.data && Array.isArray(res.data.rows)) { const rows = res.data.rows.map(row => { return { 'id': row.id, 'Q_over_b': parseFloat(row.Q_over_b).toFixed(2), 'pH': parseFloat(row.pH).toFixed(2), 'OM': parseFloat(row.OM).toFixed(2), 'CL': parseFloat(row.CL).toFixed(2), 'H': parseFloat(row.H).toFixed(2), 'Al': parseFloat(row.Al).toFixed(2) }; }); this.setData({ rows: rows, filteredRows: rows }); } else { wx.showToast({ title: '获取数据失败', icon: 'none' }); } }, fail: (err) => { wx.showToast({ title: '请求失败,请重试', icon: 'none' }); console.error('请求失败:', err); } }); }, onDownloadTemplate: function() { wx.showLoading({ title: '下载中...', }); const tableName = 'current_reduce'; // 或者根据需要选择表名 const format = 'xlsx'; // 或者 'csv' 作为模板格式 // 向后端发送请求以获取模板 wx.request({ url: `https://soilgd.com:5000/download_template?table=${tableName}&format=${format}`, method: 'GET', responseType: 'arraybuffer', // 处理二进制文件 success: (res) => { wx.hideLoading(); // 确保响应体返回的数据是有效的文件 if (res.statusCode === 200) { const fileType = format === 'excel' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' : 'text/csv'; // 生成唯一的文件名,添加时间戳以确保唯一性 const timestamp = new Date().getTime(); const fileName = `${tableName}_template_${timestamp}.${format}`; const filePath = wx.env.USER_DATA_PATH + '/' + fileName; // 使用文件系统管理器保存文件 const fs = wx.getFileSystemManager(); fs.writeFile({ filePath: filePath, data: res.data, encoding: 'binary', success: () => { // 文件保存成功后,显示菜单 wx.showActionSheet({ itemList: ['保存文件', '分享文件'], success: (res) => { if (res.tapIndex === 0) { // 用户选择保存文件 wx.showToast({ title: '文件已保存', icon: 'success' }); } else if (res.tapIndex === 1) { // 用户选择分享文件 this.onShareFile(filePath); } }, fail: (err) => { console.error('菜单显示失败', err); } }); // 保存文件路径以便后续使用 this.setData({ shareFilePath: filePath }); }, fail: (err) => { wx.showToast({ title: '文件保存失败', icon: 'none' }); console.error('文件保存失败', err); } }); } else { wx.showToast({ title: '下载失败', icon: 'none' }); } }, fail: (err) => { wx.hideLoading(); wx.showToast({ title: '请求失败,请重试', icon: 'none' }); console.error('请求失败:', err); } }); }, // 分享文件功能 onShareFile: function(filePath) { if (!filePath) { wx.showToast({ title: '文件不存在', icon: 'none', }); return; } // 分享文件 wx.shareFileMessage({ filePath: filePath, success: () => { wx.showToast({ title: '文件已分享', icon: 'success', }); }, fail: (error) => { wx.showToast({ title: '分享失败', icon: 'none', }); console.error('文件分享失败', error); } }); }, // 导出数据按钮点击事件 onExport: function () { const tableName = this.data.tableName; const fileFormat = this.data.fileFormat; wx.showLoading({ title: '导出中...', }); // 向后端发送请求,获取表格数据并导出 wx.request({ url: `https://soilgd.com:5000/export_data?table=${tableName}&format=${fileFormat}`, method: 'GET', responseType: 'arraybuffer', // 处理二进制文件 success: (res) => { wx.hideLoading(); // 确保响应体返回的数据是有效的文件 if (res.statusCode === 200) { const fileName = `${tableName}_data.${fileFormat === 'excel' ? 'xlsx' : 'csv'}`; const filePath = wx.env.USER_DATA_PATH + '/' + fileName; // 保存文件到本地 wx.getFileSystemManager().writeFile({ filePath: filePath, data: res.data, encoding: 'binary', success: () => { // 打开文件 wx.openDocument({ filePath: filePath, fileType: fileFormat === 'excel' ? 'xlsx' : 'csv', success: () => { console.log('文件打开成功'); }, fail: (error) => { console.error('文件打开失败', error); wx.showToast({ title: '打开文件失败,文件类型不支持或微信版本过低', icon: 'none' }); } }); // 在文件数据右上角显示菜单 this.setData({ showMenu: true, // 显示右上角菜单 filePath: filePath // 保存文件路径 }); }, fail: (err) => { wx.showToast({ title: '文件保存失败', icon: 'none' }); console.error('文件保存失败', err); } }); } else { wx.showToast({ title: '导出失败,请重试', icon: 'none' }); } }, fail: (err) => { wx.hideLoading(); wx.showToast({ title: '请求失败,请重试', icon: 'none' }); console.error('请求失败:', err); } }); }, // 显示右上角菜单 onShowMenu: function () { wx.showActionSheet({ itemList: ['保存文件', '分享文件'], success: (res) => { if (res.tapIndex === 0) { // 用户选择保存文件 wx.showToast({ title: '文件已保存', icon: 'success' }); } else if (res.tapIndex === 1) { // 用户选择分享文件 this.onShareFile(this.data.filePath); // 调用分享文件功能 } }, fail: (err) => { console.error('菜单显示失败', err); } }); }, // 分享文件功能 onShareFile: function(filePath) { if (!filePath) { wx.showToast({ title: '文件不存在', icon: 'none', }); return; } // 分享文件 wx.shareFileMessage({ filePath: filePath, success: () => { wx.showToast({ title: '文件已分享', icon: 'success', }); }, fail: (error) => { wx.showToast({ title: '分享失败', icon: 'none', }); console.error('文件分享失败', error); } }); }, // 导入数据功能 onImport: function () { wx.chooseMessageFile({ count: 1, type: 'file', extension: ['xlsx', 'csv'], // 允许上传的文件类型 success: (res) => { const filePath = res.tempFiles[0].path; const fileName = res.tempFiles[0].name; wx.showLoading({ title: '上传中...' }); // 上传文件到后端 wx.uploadFile({ url: 'https://soilgd.com:5000/import_data', // 后端接口 filePath: filePath, name: 'file', formData: { table: this.data.tableName, // 表名 fileName: fileName }, success: (res) => { wx.hideLoading(); const responseData = JSON.parse(res.data); if (responseData.success) { // 获取后端返回的结果 const { total_data, new_data, duplicate_data, duplicate_details } = responseData; // 显示导入结果 wx.showModal({ title: '导入结果', content: `总数据: ${total_data} 条\n新增: ${new_data} 条\n重复: ${duplicate_data} 条`, success: () => { // 如果有重复数据,跳转到重复数据详情页面 if (duplicate_data > 0) { wx.navigateTo({ url: '/pages/duplicateDetails/duplicateDetails', // 重复数据详情页面 success: (page) => { // 将重复数据传递给详情页面 page.setData({ duplicateDetails: duplicate_details }); } }); } } }); // 重新加载数据 this.LoadData(); } else { wx.showToast({ title: responseData.message || '导入失败,请检查文件格式', icon: 'none' }); } }, fail: (err) => { wx.hideLoading(); wx.showToast({ title: '导入失败,请重试', icon: 'none' }); console.error('文件上传失败:', err); } }); }, fail: (err) => { console.error('文件选择失败:', err); } }); }, // 新增按钮点击,显示新增弹窗 onAdd: function() { console.log("新增按钮被点击了"); // 调试日志 this.setData({ showAddModal: true, newData: { // 重置新增数据 Q_over_b: "", pH: "", OM: "", CL: "", H: "", Al: "" } }); console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新 }, // 输入框绑定:更新新增数据的对应字段 onInputQ_over_b: function(e) { const value = e.detail.value; const filteredValue = value.replace(/[^0-9.]/g, ''); this.setData({ 'newData.Q_over_b': filteredValue }); }, onInputpH: function(e) { const value = e.detail.value; const filteredValue = value.replace(/[^0-9.]/g, ''); this.setData({ 'newData.pH': filteredValue }); }, onInputOM: function(e) { const value = e.detail.value; const filteredValue = value.replace(/[^0-9.]/g, ''); this.setData({ 'newData.OM': filteredValue }); }, onInputCL: function(e) { const value = e.detail.value; const filteredValue = value.replace(/[^0-9.]/g, ''); this.setData({ 'newData.CL': filteredValue }); }, onInputH: function(e) { const value = e.detail.value; const filteredValue = value.replace(/[^0-9.]/g, ''); this.setData({ 'newData.H': filteredValue }); }, onInputAl: function(e) { const value = e.detail.value; const filteredValue = value.replace(/[^0-9.]/g, ''); this.setData({ 'newData.Al': filteredValue }); }, // 提交新增数据 onSubmitAdd: function() { const newRow = this.data.newData; if (Object.values(newRow).some(value => !value)) { wx.showToast({ title: '所有字段都必须填写!', icon: 'none' }); return; } wx.request({ url: 'https://soilgd.com:5000/add_item', method: 'POST', header: { 'Content-Type': 'application/json' }, data: { table: 'current_reduce', item: newRow }, success: (res) => { if (res.data.success) { this.setData({ rows: [...this.data.rows, newRow], showAddModal: false }); wx.showToast({ title: '新增成功', icon: 'success' }); this.LoadData(); } else if (res.statusCode === 409) { wx.showToast({ title: '重复数据,已有相同的数据项存在。', icon: 'none' }); } else { wx.showToast({ title: '新增失败', icon: 'none' }); } }, fail: (err) => { wx.showToast({ title: '请求失败,请重试', icon: 'none' }); console.error('请求失败:', err); } }); }, // 取消新增数据 onCancel: function() { this.setData({ showAddModal: false }); }, // 编辑按钮点击 onEdit: function(e) { const { index, row } = this.data.currentRow; console.log(row); // 检查 currentRow 是否有定义并且包含数据 if (row) { this.setData({ isEditing: true, showModal: false, showAddModal: true, // 显示编辑弹窗 newData: row, // 将当前行的数据复制到 newData 中,以便在表单中显示 currentRow: { index: index, row: row } // 保存当前行的信息,以便在提交时使用 }); } else { wx.showToast({ title: '数据加载失败,请重试', icon: 'none' }); } }, // 提交编辑数据 onSubmitEdit: function() { const updatedRow = this.data.newData; const { index } = this.data.currentRow; if (Object.values(updatedRow).some(value => !value)) { wx.showToast({ title: '所有字段都必须填写!', icon: 'none' }); return; } wx.request({ url: 'https://soilgd.com:5000/update_item', method: 'PUT', header: { 'Content-Type': 'application/json' }, data: { table: 'current_reduce', item: updatedRow }, success: (res) => { if (res.data.success) { // 更新成功后,更新前端数据 let rows = this.data.rows; rows[index] = updatedRow; // 用更新后的数据替换旧数据 this.setData({ rows: rows, showAddModal: false // 关闭编辑弹窗 }); wx.showToast({ title: '编辑成功', icon: 'success' }); this.LoadData(); } else { wx.showToast({ title: '编辑失败', icon: 'none' }); } }, fail: (err) => { wx.showToast({ title: '请求失败,请重试', icon: 'none' }); console.error('请求失败:', err); } }); }, // 删除按钮点击 onDelete: function() { const { index, row } = this.data.currentRow; console.log("当前选中行的数据:", this.data.currentRow); const condition = `id=${row.id}`; // 使用条件进行删除 // 发送 DELETE 请求 wx.request({ url: 'https://soilgd.com:5000/delete_item', // 后端接口地址 method: 'POST', // 使用 POST 请求 data: { table: 'current_reduce', // 目标表 condition: condition // 删除条件 }, success: (res) => { if (res.data.success) { // 删除成功后,更新前端数据 let rows = this.data.rows; rows.splice(index, 1); // 从数据中删除选中的行 this.setData({ rows: rows, showModal: false // 隐藏编辑删除弹窗 }); wx.showToast({ title: '删除成功', icon: 'success' }); // 重新获取数据 this.LoadData(); // 重新加载数据 } else { wx.showToast({ title: '删除失败', icon: 'none' }); } }, fail: (err) => { wx.showToast({ title: '请求失败,请重试', icon: 'none' }); console.error('请求失败:', err); } }); }, onSubmit: function() { if (this.data.isEditing) { this.onSubmitEdit(); } else { this.onSubmitAdd(); } }, // 取消编辑删除弹窗 onCancel: function() { if (this.data.showModal) { this.setData({ showModal: false }); } else { this.setData({ showAddModal: false }); } }, // 行点击事件:显示编辑和删除弹窗 onRowClick: function(e) { const index = e.currentTarget.dataset.index; const row = e.currentTarget.dataset.row; this.setData({ currentRow: { index: index, row: row }, showModal: true // 显示编辑删除弹窗 }); } });