Visualizatio.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. Page({
  2. data: {
  3. isEditing: false,
  4. tableName: 'current_reduce', // 确保这个表名在页面中是有效的
  5. fileFormat: 'Excel', // 默认是 Excel 格式
  6. shoopingtext: "", // 搜索框内容
  7. filteredRows: [], // 表格过滤后的数据
  8. rows: [], // 所有表格数据
  9. showModal: false, // 控制底部编辑删除弹窗的显示与否
  10. showAddModal: false, // 控制新增数据弹窗的显示与否
  11. currentRow: null, // 当前选中的表格行
  12. // 新增数据弹窗的输入框内容
  13. newData: {
  14. id: "",
  15. Q_over_b: "",
  16. pH: "",
  17. OM: "",
  18. CL: "",
  19. H: "",
  20. Al: ""
  21. },
  22. // 中文表头内容,动态生成
  23. tableHeaders: [
  24. "序号", "Q/ΔpH", "初始pH", "有机质含量", "土壤粘粒",
  25. "交换性氢","交换性铝"
  26. ],
  27. unit: [
  28. " ", " ", " ", "(g/kg)", "(g/kg)",
  29. "(cmol/kg)", "(cmol/kg)"
  30. ]
  31. },
  32. // 页面加载时获取表格数据
  33. onLoad: function() {
  34. this.LoadData();
  35. },
  36. LoadData: function() {
  37. wx.request({
  38. url: 'https://soilgd.com:5000/table',
  39. method: 'POST',
  40. header: {
  41. 'Content-Type': 'application/json'
  42. },
  43. data: {
  44. table: 'current_reduce'
  45. },
  46. success: (res) => {
  47. console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式
  48. if (res.data && Array.isArray(res.data.rows)) {
  49. const rows = res.data.rows.map(row => {
  50. return {
  51. 'id': row.id,
  52. 'Q_over_b': parseFloat(row.Q_over_b).toFixed(2),
  53. 'pH': parseFloat(row.pH).toFixed(2),
  54. 'OM': parseFloat(row.OM).toFixed(2),
  55. 'CL': parseFloat(row.CL).toFixed(2),
  56. 'H': parseFloat(row.H).toFixed(2),
  57. 'Al': parseFloat(row.Al).toFixed(2)
  58. };
  59. });
  60. this.setData({
  61. rows: rows,
  62. filteredRows: rows
  63. });
  64. } else {
  65. wx.showToast({
  66. title: '获取数据失败',
  67. icon: 'none'
  68. });
  69. }
  70. },
  71. fail: (err) => {
  72. wx.showToast({
  73. title: '请求失败,请重试',
  74. icon: 'none'
  75. });
  76. console.error('请求失败:', err);
  77. }
  78. });
  79. },
  80. onDownloadTemplate: function() {
  81. wx.showLoading({
  82. title: '下载中...',
  83. });
  84. const tableName = 'current_reduce'; // 或者根据需要选择表名
  85. const format = 'xlsx'; // 或者 'csv' 作为模板格式
  86. // 向后端发送请求以获取模板
  87. wx.request({
  88. url: `https://soilgd.com:5000/download_template?table=${tableName}&format=${format}`,
  89. method: 'GET',
  90. responseType: 'arraybuffer', // 处理二进制文件
  91. success: (res) => {
  92. wx.hideLoading();
  93. // 确保响应体返回的数据是有效的文件
  94. if (res.statusCode === 200) {
  95. const fileType = format === 'excel' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' : 'text/csv';
  96. // 生成唯一的文件名,添加时间戳以确保唯一性
  97. const timestamp = new Date().getTime();
  98. const fileName = `${tableName}_template_${timestamp}.${format}`;
  99. const filePath = wx.env.USER_DATA_PATH + '/' + fileName;
  100. // 使用文件系统管理器保存文件
  101. const fs = wx.getFileSystemManager();
  102. fs.writeFile({
  103. filePath: filePath,
  104. data: res.data,
  105. encoding: 'binary',
  106. success: () => {
  107. // 文件保存成功后,显示菜单
  108. wx.showActionSheet({
  109. itemList: ['保存文件', '分享文件'],
  110. success: (res) => {
  111. if (res.tapIndex === 0) { // 用户选择保存文件
  112. wx.showToast({
  113. title: '文件已保存',
  114. icon: 'success'
  115. });
  116. } else if (res.tapIndex === 1) { // 用户选择分享文件
  117. this.onShareFile(filePath);
  118. }
  119. },
  120. fail: (err) => {
  121. console.error('菜单显示失败', err);
  122. }
  123. });
  124. // 保存文件路径以便后续使用
  125. this.setData({
  126. shareFilePath: filePath
  127. });
  128. },
  129. fail: (err) => {
  130. wx.showToast({
  131. title: '文件保存失败',
  132. icon: 'none'
  133. });
  134. console.error('文件保存失败', err);
  135. }
  136. });
  137. } else {
  138. wx.showToast({
  139. title: '下载失败',
  140. icon: 'none'
  141. });
  142. }
  143. },
  144. fail: (err) => {
  145. wx.hideLoading();
  146. wx.showToast({
  147. title: '请求失败,请重试',
  148. icon: 'none'
  149. });
  150. console.error('请求失败:', err);
  151. }
  152. });
  153. },
  154. // 分享文件功能
  155. onShareFile: function(filePath) {
  156. if (!filePath) {
  157. wx.showToast({
  158. title: '文件不存在',
  159. icon: 'none',
  160. });
  161. return;
  162. }
  163. // 分享文件
  164. wx.shareFileMessage({
  165. filePath: filePath,
  166. success: () => {
  167. wx.showToast({
  168. title: '文件已分享',
  169. icon: 'success',
  170. });
  171. },
  172. fail: (error) => {
  173. wx.showToast({
  174. title: '分享失败',
  175. icon: 'none',
  176. });
  177. console.error('文件分享失败', error);
  178. }
  179. });
  180. },
  181. // 导出数据按钮点击事件
  182. onExport: function () {
  183. const tableName = this.data.tableName;
  184. const fileFormat = this.data.fileFormat;
  185. wx.showLoading({
  186. title: '导出中...',
  187. });
  188. // 向后端发送请求,获取表格数据并导出
  189. const downloadUrl = `https://soilgd.com:5000/export_data?table=${tableName}&format=${fileFormat}`;
  190. wx.downloadFile({
  191. url: downloadUrl,
  192. success: (res) => {
  193. wx.hideLoading();
  194. if (res.statusCode === 200) {
  195. // 文件下载成功后,尝试打开文件
  196. wx.openDocument({
  197. filePath: res.tempFilePath, // 使用临时文件路径
  198. fileType: fileFormat === 'excel' ? 'xlsx' : 'xls',
  199. success: () => {
  200. console.log('文件打开成功');
  201. wx.showToast({
  202. title: '文件已打开',
  203. icon: 'success'
  204. });
  205. },
  206. fail: (error) => {
  207. console.error('文件打开失败', error);
  208. wx.showToast({
  209. title: '打开文件失败',
  210. icon: 'none'
  211. });
  212. }
  213. });
  214. } else {
  215. wx.showToast({
  216. title: '导出失败,请检查网络或重试',
  217. icon: 'none'
  218. });
  219. }
  220. },
  221. fail: (err) => {
  222. wx.hideLoading();
  223. wx.showToast({
  224. title: '请求失败,请重试',
  225. icon: 'none'
  226. });
  227. console.error('请求失败:', err);
  228. }
  229. });
  230. },
  231. // 显示右上角菜单
  232. onShowMenu: function () {
  233. wx.showActionSheet({
  234. itemList: ['保存文件', '分享文件'],
  235. success: (res) => {
  236. if (res.tapIndex === 0) { // 用户选择保存文件
  237. wx.showToast({
  238. title: '文件已保存',
  239. icon: 'success'
  240. });
  241. } else if (res.tapIndex === 1) { // 用户选择分享文件
  242. this.onShareFile(this.data.filePath); // 调用分享文件功能
  243. }
  244. },
  245. fail: (err) => {
  246. console.error('菜单显示失败', err);
  247. }
  248. });
  249. },
  250. // 分享文件功能
  251. onShareFile: function(filePath) {
  252. if (!filePath) {
  253. wx.showToast({
  254. title: '文件不存在',
  255. icon: 'none',
  256. });
  257. return;
  258. }
  259. // 分享文件
  260. wx.shareFileMessage({
  261. filePath: filePath,
  262. success: () => {
  263. wx.showToast({
  264. title: '文件已分享',
  265. icon: 'success',
  266. });
  267. },
  268. fail: (error) => {
  269. wx.showToast({
  270. title: '分享失败',
  271. icon: 'none',
  272. });
  273. console.error('文件分享失败', error);
  274. }
  275. });
  276. },
  277. // 导入数据功能
  278. onImport: function () {
  279. wx.chooseMessageFile({
  280. count: 1,
  281. type: 'file',
  282. extension: ['xlsx', 'csv'], // 允许上传的文件类型
  283. success: (res) => {
  284. const filePath = res.tempFiles[0].path;
  285. const fileName = res.tempFiles[0].name;
  286. wx.showLoading({ title: '上传中...' });
  287. // 上传文件到后端
  288. wx.uploadFile({
  289. url: 'https://soilgd.com:5000/import_data', // 后端接口
  290. filePath: filePath,
  291. name: 'file',
  292. formData: {
  293. table: this.data.tableName, // 表名
  294. fileName: fileName
  295. },
  296. success: (res) => {
  297. wx.hideLoading();
  298. const responseData = JSON.parse(res.data);
  299. if (responseData.success) {
  300. // 获取后端返回的结果
  301. const { total_data, new_data, duplicate_data, duplicate_details } = responseData;
  302. // 显示导入结果
  303. wx.showModal({
  304. title: '导入结果',
  305. content: `总数据: ${total_data} 条\n新增: ${new_data} 条\n重复: ${duplicate_data} 条`,
  306. success: () => {
  307. // 如果有重复数据,跳转到重复数据详情页面
  308. if (duplicate_data > 0) {
  309. wx.navigateTo({
  310. url: '/pages/duplicateDetails/duplicateDetails', // 重复数据详情页面
  311. success: (page) => {
  312. // 将重复数据传递给详情页面
  313. page.setData({ duplicateDetails: duplicate_details });
  314. }
  315. });
  316. }
  317. }
  318. });
  319. // 重新加载数据
  320. this.LoadData();
  321. } else {
  322. wx.showToast({
  323. title: responseData.message || '导入失败,请检查文件格式',
  324. icon: 'none'
  325. });
  326. }
  327. },
  328. fail: (err) => {
  329. wx.hideLoading();
  330. wx.showToast({
  331. title: '导入失败,请重试',
  332. icon: 'none'
  333. });
  334. console.error('文件上传失败:', err);
  335. }
  336. });
  337. },
  338. fail: (err) => {
  339. console.error('文件选择失败:', err);
  340. }
  341. });
  342. },
  343. // 新增按钮点击,显示新增弹窗
  344. onAdd: function() {
  345. console.log("新增按钮被点击了"); // 调试日志
  346. this.setData({
  347. showAddModal: true,
  348. newData: { // 重置新增数据
  349. Q_over_b: "",
  350. pH: "",
  351. OM: "",
  352. CL: "",
  353. H: "",
  354. Al: ""
  355. }
  356. });
  357. console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新
  358. },
  359. // 输入框绑定:更新新增数据的对应字段
  360. onInputQ_over_b: function(e) {
  361. const value = e.detail.value;
  362. const filteredValue = value.replace(/[^0-9.]/g, '');
  363. this.setData({
  364. 'newData.Q_over_b': filteredValue
  365. });
  366. },
  367. onInputpH: function(e) {
  368. const value = e.detail.value;
  369. const filteredValue = value.replace(/[^0-9.]/g, '');
  370. this.setData({
  371. 'newData.pH': filteredValue
  372. });
  373. },
  374. onInputOM: function(e) {
  375. const value = e.detail.value;
  376. const filteredValue = value.replace(/[^0-9.]/g, '');
  377. this.setData({
  378. 'newData.OM': filteredValue
  379. });
  380. },
  381. onInputCL: function(e) {
  382. const value = e.detail.value;
  383. const filteredValue = value.replace(/[^0-9.]/g, '');
  384. this.setData({
  385. 'newData.CL': filteredValue
  386. });
  387. },
  388. onInputH: function(e) {
  389. const value = e.detail.value;
  390. const filteredValue = value.replace(/[^0-9.]/g, '');
  391. this.setData({
  392. 'newData.H': filteredValue
  393. });
  394. },
  395. onInputAl: function(e) {
  396. const value = e.detail.value;
  397. const filteredValue = value.replace(/[^0-9.]/g, '');
  398. this.setData({
  399. 'newData.Al': filteredValue
  400. });
  401. },
  402. // 提交新增数据
  403. onSubmitAdd: function() {
  404. const newRow = this.data.newData;
  405. if (Object.values(newRow).some(value => !value)) {
  406. wx.showToast({
  407. title: '所有字段都必须填写!',
  408. icon: 'none'
  409. });
  410. return;
  411. }
  412. wx.request({
  413. url: 'https://soilgd.com:5000/add_item',
  414. method: 'POST',
  415. header: {
  416. 'Content-Type': 'application/json'
  417. },
  418. data: {
  419. table: 'current_reduce',
  420. item: newRow
  421. },
  422. success: (res) => {
  423. if (res.data.success) {
  424. this.setData({
  425. rows: [...this.data.rows, newRow],
  426. showAddModal: false
  427. });
  428. wx.showToast({
  429. title: '新增成功',
  430. icon: 'success'
  431. });
  432. this.LoadData();
  433. } else if (res.statusCode === 409) {
  434. wx.showToast({
  435. title: '重复数据,已有相同的数据项存在。',
  436. icon: 'none'
  437. });
  438. } else {
  439. wx.showToast({
  440. title: '新增失败',
  441. icon: 'none'
  442. });
  443. }
  444. },
  445. fail: (err) => {
  446. wx.showToast({
  447. title: '请求失败,请重试',
  448. icon: 'none'
  449. });
  450. console.error('请求失败:', err);
  451. }
  452. });
  453. },
  454. // 取消新增数据
  455. onCancel: function() {
  456. this.setData({
  457. showAddModal: false
  458. });
  459. },
  460. // 编辑按钮点击
  461. onEdit: function(e) {
  462. const { index, row } = this.data.currentRow;
  463. console.log(row);
  464. // 检查 currentRow 是否有定义并且包含数据
  465. if (row) {
  466. this.setData({
  467. isEditing: true,
  468. showModal: false,
  469. showAddModal: true, // 显示编辑弹窗
  470. newData: row, // 将当前行的数据复制到 newData 中,以便在表单中显示
  471. currentRow: { index: index, row: row } // 保存当前行的信息,以便在提交时使用
  472. });
  473. } else {
  474. wx.showToast({
  475. title: '数据加载失败,请重试',
  476. icon: 'none'
  477. });
  478. }
  479. },
  480. // 提交编辑数据
  481. onSubmitEdit: function() {
  482. const updatedRow = this.data.newData;
  483. const { index } = this.data.currentRow;
  484. if (Object.values(updatedRow).some(value => !value)) {
  485. wx.showToast({
  486. title: '所有字段都必须填写!',
  487. icon: 'none'
  488. });
  489. return;
  490. }
  491. wx.request({
  492. url: 'https://soilgd.com:5000/update_item',
  493. method: 'PUT',
  494. header: {
  495. 'Content-Type': 'application/json'
  496. },
  497. data: {
  498. table: 'current_reduce',
  499. item: updatedRow
  500. },
  501. success: (res) => {
  502. if (res.data.success) {
  503. // 更新成功后,更新前端数据
  504. let rows = this.data.rows;
  505. rows[index] = updatedRow; // 用更新后的数据替换旧数据
  506. this.setData({
  507. rows: rows,
  508. showAddModal: false // 关闭编辑弹窗
  509. });
  510. wx.showToast({
  511. title: '编辑成功',
  512. icon: 'success'
  513. });
  514. this.LoadData();
  515. } else {
  516. wx.showToast({
  517. title: '编辑失败',
  518. icon: 'none'
  519. });
  520. }
  521. },
  522. fail: (err) => {
  523. wx.showToast({
  524. title: '请求失败,请重试',
  525. icon: 'none'
  526. });
  527. console.error('请求失败:', err);
  528. }
  529. });
  530. },
  531. // 删除按钮点击
  532. onDelete: function() {
  533. const { index, row } = this.data.currentRow;
  534. console.log("当前选中行的数据:", this.data.currentRow);
  535. const condition = `id=${row.id}`; // 使用条件进行删除
  536. // 发送 DELETE 请求
  537. wx.request({
  538. url: 'https://soilgd.com:5000/delete_item', // 后端接口地址
  539. method: 'POST', // 使用 POST 请求
  540. data: {
  541. table: 'current_reduce', // 目标表
  542. condition: condition // 删除条件
  543. },
  544. success: (res) => {
  545. if (res.data.success) {
  546. // 删除成功后,更新前端数据
  547. let rows = this.data.rows;
  548. rows.splice(index, 1); // 从数据中删除选中的行
  549. this.setData({
  550. rows: rows,
  551. showModal: false // 隐藏编辑删除弹窗
  552. });
  553. wx.showToast({
  554. title: '删除成功',
  555. icon: 'success'
  556. });
  557. // 重新获取数据
  558. this.LoadData(); // 重新加载数据
  559. } else {
  560. wx.showToast({
  561. title: '删除失败',
  562. icon: 'none'
  563. });
  564. }
  565. },
  566. fail: (err) => {
  567. wx.showToast({
  568. title: '请求失败,请重试',
  569. icon: 'none'
  570. });
  571. console.error('请求失败:', err);
  572. }
  573. });
  574. },
  575. onSubmit: function() {
  576. if (this.data.isEditing) {
  577. this.onSubmitEdit();
  578. } else {
  579. this.onSubmitAdd();
  580. }
  581. },
  582. // 取消编辑删除弹窗
  583. onCancel: function() {
  584. if (this.data.showModal) {
  585. this.setData({
  586. showModal: false
  587. });
  588. } else {
  589. this.setData({
  590. showAddModal: false
  591. });
  592. }
  593. },
  594. // 行点击事件:显示编辑和删除弹窗
  595. onRowClick: function(e) {
  596. const index = e.currentTarget.dataset.index;
  597. const row = e.currentTarget.dataset.row;
  598. this.setData({
  599. currentRow: { index: index, row: row },
  600. showModal: true // 显示编辑删除弹窗
  601. });
  602. }
  603. });