|
@@ -1,6 +1,8 @@
|
|
|
Page({
|
|
|
data: {
|
|
|
isEditing: false,
|
|
|
+ tableName: 'current_reflux', // 确保这个表名在页面中是有效的
|
|
|
+ fileFormat: 'excel', // 默认是 Excel 格式
|
|
|
shoopingtext: "", // 搜索框内容
|
|
|
filteredRows: [], // 表格过滤后的数据
|
|
|
rows: [], // 所有表格数据
|
|
@@ -9,25 +11,21 @@ Page({
|
|
|
currentRow: null, // 当前选中的表格行
|
|
|
// 新增数据弹窗的输入框内容
|
|
|
newData: {
|
|
|
- Sample_ID: "",
|
|
|
+ id: "",
|
|
|
OM: "",
|
|
|
CL: "",
|
|
|
CEC: "",
|
|
|
- H: "",
|
|
|
+ H_plus: "",
|
|
|
HN: "",
|
|
|
- Al: "",
|
|
|
+ Al3_plus: "",
|
|
|
free_alumina: "",
|
|
|
free_iron_oxides: "",
|
|
|
- pH: "",
|
|
|
- final_pH: "",
|
|
|
- Collection_location: "",
|
|
|
- Collection_date: ""
|
|
|
+ Delta_pH: ""
|
|
|
},
|
|
|
// 中文表头内容,动态生成
|
|
|
tableHeaders: [
|
|
|
- "序号", "0天 pH", "有机质含量 (g/kg)", "土壤粘粒重量 (g/kg)", "阳离子交换量 (cmol/kg)",
|
|
|
- "氢离子含量 (cmol/kg)", "铵离子含量 (mg/kg)", "铝离子含量 (cmol/kg)", "游离氧化铝 (g/kg)",
|
|
|
- "游离氧化铁 (g/kg)", "105天 pH", "采集位置", "采集日期"
|
|
|
+ "序号", "有机质含量", "土壤粘粒重量", "阳离子交换量", "氢离子含量",
|
|
|
+ "硝态氮含量","铝离子含量", "游离氧化铝", "游离氧化铁", "酸碱差值",
|
|
|
]
|
|
|
},
|
|
|
|
|
@@ -44,7 +42,7 @@ Page({
|
|
|
'Content-Type': 'application/json'
|
|
|
},
|
|
|
data: {
|
|
|
- table: 'Soil_samples'
|
|
|
+ table: 'current_reflux'
|
|
|
},
|
|
|
success: (res) => {
|
|
|
console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式
|
|
@@ -52,19 +50,16 @@ Page({
|
|
|
if (res.data && Array.isArray(res.data.rows)) {
|
|
|
const rows = res.data.rows.map(row => {
|
|
|
return {
|
|
|
- 'Sample_ID': row[0],
|
|
|
- 'pH': row[1],
|
|
|
- 'OM': row[2],
|
|
|
- 'CL': row[3],
|
|
|
- 'CEC': row[4],
|
|
|
- 'H': row[5],
|
|
|
- 'HN': row[6],
|
|
|
- 'Al': row[7],
|
|
|
- 'free_alumina': row[8],
|
|
|
- 'free_iron_oxides': row[9],
|
|
|
- 'final_pH': row[10],
|
|
|
- 'Collection_location': row[11],
|
|
|
- 'Collection_date': row[12]
|
|
|
+ 'id': row[0],
|
|
|
+ 'OM': row[1],
|
|
|
+ 'CL': row[2],
|
|
|
+ 'CEC': row[3],
|
|
|
+ 'H_plus': row[4],
|
|
|
+ 'HN': row[5],
|
|
|
+ 'Al3_plus': row[6],
|
|
|
+ 'free_alumina': row[7],
|
|
|
+ 'free_iron_oxides': row[8],
|
|
|
+ 'Delta_pH': row[9],
|
|
|
};
|
|
|
});
|
|
|
|
|
@@ -108,6 +103,217 @@ Page({
|
|
|
});
|
|
|
},
|
|
|
|
|
|
+ // 下载模板函数
|
|
|
+ onDownloadTemplate: function() {
|
|
|
+ wx.showLoading({
|
|
|
+ title: '下载中...',
|
|
|
+ });
|
|
|
+
|
|
|
+ const tableName = 'current_reflux'; // 或者根据需要选择表名
|
|
|
+ const format = 'excel'; // 或者 'csv' 作为模板格式
|
|
|
+
|
|
|
+ wx.request({
|
|
|
+ url: `http://localhost: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.showToast({
|
|
|
+ title: '文件已保存',
|
|
|
+ icon: 'success'
|
|
|
+ });
|
|
|
+ // 触发文件下载
|
|
|
+ wx.openDocument({
|
|
|
+ filePath: filePath,
|
|
|
+ fileType: format === 'excel' ? 'xlsx' : 'csv',
|
|
|
+ success: () => {
|
|
|
+ console.log('文件打开成功');
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('文件打开失败', error);
|
|
|
+ wx.showToast({
|
|
|
+ title: '打开文件失败',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ });
|
|
|
+},
|
|
|
+
|
|
|
+ // 导出数据按钮点击事件
|
|
|
+ onExport: function () {
|
|
|
+ const tableName = this.data.tableName;
|
|
|
+ const fileFormat = this.data.fileFormat;
|
|
|
+
|
|
|
+ wx.showLoading({
|
|
|
+ title: '导出中...',
|
|
|
+ });
|
|
|
+
|
|
|
+ // 向后端发送请求,获取表格数据并导出
|
|
|
+ wx.request({
|
|
|
+ url: `http://localhost: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.showToast({
|
|
|
+ title: '文件已保存',
|
|
|
+ icon: 'success'
|
|
|
+ });
|
|
|
+
|
|
|
+ // 打开已保存的文件
|
|
|
+ wx.openDocument({
|
|
|
+ filePath: filePath,
|
|
|
+ fileType: fileFormat === 'excel' ? 'xlsx' : 'csv',
|
|
|
+ success: () => {
|
|
|
+ console.log('文件打开成功');
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('文件打开失败', error);
|
|
|
+ wx.showToast({
|
|
|
+ title: '打开文件失败',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 导入数据功能
|
|
|
+ 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: 'http://localhost: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) {
|
|
|
+ wx.showToast({
|
|
|
+ title: '导入成功',
|
|
|
+ icon: 'success'
|
|
|
+ });
|
|
|
+
|
|
|
+ // 重新加载数据
|
|
|
+ this.LoadData();
|
|
|
+ } else {
|
|
|
+ wx.showToast({
|
|
|
+ title: '导入失败,请检查文件格式',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showToast({
|
|
|
+ title: '导入失败,请重试',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ console.error('文件上传失败:', err);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('文件选择失败:', err);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
// 新增按钮点击,显示新增弹窗
|
|
|
onAdd: function() {
|
|
|
console.log("新增按钮被点击了"); // 调试日志
|
|
@@ -117,15 +323,12 @@ Page({
|
|
|
OM: "",
|
|
|
CL: "",
|
|
|
CEC: "",
|
|
|
- H: "",
|
|
|
+ H_plus: "",
|
|
|
HN: "",
|
|
|
- Al: "",
|
|
|
+ Al3_plus: "",
|
|
|
free_alumina: "",
|
|
|
free_iron_oxides: "",
|
|
|
- pH: "",
|
|
|
- final_pH: "",
|
|
|
- Collection_location: "",
|
|
|
- Collection_date: ""
|
|
|
+ Delta_pH: ""
|
|
|
}
|
|
|
});
|
|
|
console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新
|
|
@@ -147,9 +350,9 @@ Page({
|
|
|
'newData.CEC': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
- onInputHplus: function(e) {
|
|
|
+ onInputH_plus: function(e) {
|
|
|
this.setData({
|
|
|
- 'newData.H': e.detail.value
|
|
|
+ 'newData.H_plus': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
onInputHN: function(e) {
|
|
@@ -157,39 +360,24 @@ Page({
|
|
|
'newData.HN': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
- onInputAl3plus: function(e) {
|
|
|
+ onInputAl3_plus: function(e) {
|
|
|
this.setData({
|
|
|
- 'newData.Al': e.detail.value
|
|
|
+ 'newData.Al3_plus': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
- onInputAlumina: function(e) {
|
|
|
+ onInputfree_alumina: function(e) {
|
|
|
this.setData({
|
|
|
'newData.free_alumina': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
- onInputIronOxides: function(e) {
|
|
|
+ onInputfree_iron_oxides: function(e) {
|
|
|
this.setData({
|
|
|
'newData.free_iron_oxides': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
- onInputinitPH: function(e) {
|
|
|
+ onInputDelta_pH: function(e) {
|
|
|
this.setData({
|
|
|
- 'newData.pH': e.detail.value
|
|
|
- });
|
|
|
- },
|
|
|
- onInputfinalPH: function(e) {
|
|
|
- this.setData({
|
|
|
- 'newData.final_pH': e.detail.value
|
|
|
- });
|
|
|
- },
|
|
|
- onInputlocation: function(e) {
|
|
|
- this.setData({
|
|
|
- 'newData.Collection_location': e.detail.value
|
|
|
- });
|
|
|
- },
|
|
|
- onBindDateChange: function(e) {
|
|
|
- this.setData({
|
|
|
- 'newData.Collection_date': e.detail.value
|
|
|
+ 'newData.Delta_pH': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
|
|
@@ -211,7 +399,7 @@ Page({
|
|
|
'Content-Type': 'application/json'
|
|
|
},
|
|
|
data: {
|
|
|
- table: 'Soil_samples',
|
|
|
+ table: 'current_reflux',
|
|
|
item: newRow
|
|
|
},
|
|
|
success: (res) => {
|
|
@@ -291,7 +479,7 @@ onSubmitEdit: function() {
|
|
|
'Content-Type': 'application/json'
|
|
|
},
|
|
|
data: {
|
|
|
- table: 'Soil_samples',
|
|
|
+ table: 'current_reflux',
|
|
|
item: updatedRow
|
|
|
},
|
|
|
success: (res) => {
|
|
@@ -330,17 +518,14 @@ onSubmitEdit: function() {
|
|
|
const { index, row } = this.data.currentRow;
|
|
|
console.log("当前选中行的数据:", this.data.currentRow);
|
|
|
|
|
|
- const condition = `Sample_ID=${row.Sample_ID}`; // 使用条件进行删除
|
|
|
+ const condition = `id=${row.id}`; // 使用条件进行删除
|
|
|
|
|
|
// 发送 DELETE 请求
|
|
|
wx.request({
|
|
|
- url: 'http://127.0.0.1:5000/delete_item', // 确保使用正确的URL
|
|
|
- method: 'DELETE', // 使用 DELETE 请求方法
|
|
|
- header: {
|
|
|
- 'Content-Type': 'application/json' // 请求类型是 JSON
|
|
|
- },
|
|
|
+ url: 'http://127.0.0.1:5000/delete_item', // 后端接口地址
|
|
|
+ method: 'POST', // 使用 POST 请求
|
|
|
data: {
|
|
|
- table: 'Soil_samples', // 目标表
|
|
|
+ table: 'current_reflux', // 目标表
|
|
|
condition: condition // 删除条件
|
|
|
},
|
|
|
success: (res) => {
|
|
@@ -356,7 +541,7 @@ onSubmitEdit: function() {
|
|
|
title: '删除成功',
|
|
|
icon: 'success'
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
// 重新获取数据
|
|
|
this.LoadData(); // 重新加载数据
|
|
|
} else {
|
|
@@ -374,7 +559,7 @@ onSubmitEdit: function() {
|
|
|
console.error('请求失败:', err);
|
|
|
}
|
|
|
});
|
|
|
- },
|
|
|
+ },
|
|
|
|
|
|
onSubmit: function() {
|
|
|
if (this.data.isEditing) {
|
|
@@ -383,6 +568,7 @@ onSubmitEdit: function() {
|
|
|
this.onSubmitAdd();
|
|
|
}
|
|
|
},
|
|
|
+
|
|
|
// 取消编辑删除弹窗
|
|
|
onCancel: function() {
|
|
|
if (this.data.showModal) {
|