123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- Page({
- data: {
- isEditing: false,
- tableName: 'current_reflux', // 确保这个表名在页面中是有效的
- fileFormat: 'excel', // 默认是 Excel 格式
- shoopingtext: "", // 搜索框内容
- filteredRows: [], // 表格过滤后的数据
- rows: [], // 所有表格数据
- showModal: false, // 控制底部编辑删除弹窗的显示与否
- showAddModal: false, // 控制新增数据弹窗的显示与否
- currentRow: null, // 当前选中的表格行
- // 新增数据弹窗的输入框内容
- newData: {
- id: "",
- OM: "",
- CL: "",
- CEC: "",
- H_plus: "",
- N: "",
- Al3_plus: "",
- Delta_pH: ""
- },
- // 中文表头内容,动态生成
- tableHeaders: [
- "序号", "有机质含量", "土壤粘粒", "阳离子交换量", "交换性氢",
- "水解氮","交换性铝", "ΔpH",
- ],
- unit: [
- " ", "(g/kg)", "(g/kg)", "(cmol/kg)", "(cmol/kg)",
- "(g/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_reflux'
- },
- 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,
- 'OM': parseFloat(row.OM).toFixed(2),
- 'CL': parseFloat(row.CL).toFixed(2),
- 'CEC': parseFloat(row.CEC).toFixed(2),
- 'H_plus': parseFloat(row.H_plus).toFixed(2),
- 'N': parseFloat(row.N).toFixed(2),
- 'Al3_plus': parseFloat(row.Al3_plus).toFixed(2),
- 'Delta_pH': parseFloat(row.Delta_pH).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_reflux'; // 或者根据需要选择表名
- const format = 'excel'; // 或者 '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.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: `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.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: '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({
- isEditing: false, // 这里设置为 false,表示新增操作而非编辑
- showAddModal: true, // 显示新增数据弹窗
- newData: { // 重置新增数据
- OM: "",
- CL: "",
- CEC: "",
- H_plus: "",
- N: "",
- Al3_plus: "",
- Delta_pH: ""
- }
- });
- console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新
- },
- // 输入框绑定:更新新增数据的对应字段
- 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,
- });
- },
- onInputCEC: function(e) {
- const value = e.detail.value;
- const filteredValue = value.replace(/[^0-9.]/g, '');
- this.setData({
- 'newData.CEC': filteredValue,
- });
- },
- onInputH_plus: function(e) {
- const value = e.detail.value;
- const filteredValue = value.replace(/[^0-9.]/g, '');
- this.setData({
- 'newData.H_plus': filteredValue,
- });
- },
- onInputN: function(e) {
- const value = e.detail.value;
- const filteredValue = value.replace(/[^0-9.]/g, '');
- this.setData({
- 'newData.N': filteredValue,
- });
- },
- onInputAl3_plus: function(e) {
- const value = e.detail.value;
- const filteredValue = value.replace(/[^0-9.]/g, '');
- this.setData({
- 'newData.Al3_plus': filteredValue,
- });
- },
- onInputDelta_pH: function(e) {
- const value = e.detail.value;
- const filteredValue = value.replace(/[^0-9.]/g, '');
- this.setData({
- 'newData.Delta_pH': filteredValue,
- });
- },
- // 提交新增数据
- onSubmitAdd: function() {
- const newRow = this.data.newData;
- if (Object.values(newRow).some(value => !value)) {
- wx.showToast({
- title: '所有字段都必须填写!',
- icon: 'none'
- });
- return;
- }
- // 在请求之前打印数据,确保发送的数据正确
- console.log('Sending data:', {
- table: 'current_reflux',
- item: newRow
- });
- wx.request({
- url: 'https://soilgd.com:5000/add_item',
- method: 'POST',
- header: {
- 'Content-Type': 'application/json'
- },
- data: {
- table: 'current_reflux',
- item: newRow
- },
- success: (res) => {
- // 判断返回的状态码,进行处理
- if (res.statusCode === 200 && 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_reflux',
- 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_reflux', // 目标表
- 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 // 显示编辑删除弹窗
- });
- }
- });
|