AcidNeutralizationModel.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Page({
  2. data: {
  3. result: '', // 存储计算结果
  4. ph: '', // 土壤PH
  5. OM: '', // 有机质含量
  6. CL: '', // 土壤粘粒重量
  7. H: '', // 氢离子含量
  8. Al: '', // 铝离子含量
  9. init_pH: '',
  10. target_pH: '',
  11. showModal: false, // 控制弹窗显示与隐藏
  12. },
  13. // 更新输入数据
  14. onOMChange: function (e) {
  15. this.setData({
  16. OM: e.detail.value,
  17. });
  18. },
  19. onCLChange: function (e) {
  20. this.setData({
  21. CL: e.detail.value,
  22. });
  23. },
  24. onHChange: function (e) {
  25. this.setData({
  26. H: e.detail.value,
  27. });
  28. },
  29. onAlChange: function (e) {
  30. this.setData({
  31. Al: e.detail.value,
  32. });
  33. },
  34. onPhChange: function (e) {
  35. this.setData({
  36. ph: e.detail.value,
  37. });
  38. },
  39. onInitPhChange: function (e) {
  40. this.setData({
  41. init_pH: e.detail.value,
  42. });
  43. },
  44. onTargetPhChange: function (e) {
  45. this.setData({
  46. target_pH: e.detail.value,
  47. });
  48. },
  49. // 点击按钮后进行计算并提示结果
  50. // 点击按钮后进行计算并提示结果
  51. calculate: function () {
  52. console.log('开始计算...');
  53. const data = {
  54. model_id: 6,
  55. parameters: {
  56. init_pH: this.data.init_pH,
  57. target_pH: this.data.target_pH,
  58. OM: this.data.OM,
  59. CL: this.data.CL,
  60. H: this.data.H,
  61. Al: this.data.Al,
  62. },
  63. };
  64. wx.request({
  65. url: 'https://soilgd.com:5000/predict',
  66. method: 'POST',
  67. data: JSON.stringify(data),
  68. header: {
  69. 'content-type': 'application/json',
  70. },
  71. success: (res) => {
  72. console.log('预测结果:', res.data.result);
  73. // 确保结果显示两位小数
  74. let result = res.data.result;
  75. if (result !== null && !isNaN(result)) {
  76. result = parseFloat(result).toFixed(2); // 将结果格式化为两位小数
  77. }
  78. // 更新计算结果
  79. this.setData({
  80. result: result,
  81. showModal: true, // 显示弹窗
  82. });
  83. },
  84. fail: (error) => {
  85. console.error('请求失败:', error);
  86. wx.showToast({
  87. title: '计算失败,请重试',
  88. icon: 'none',
  89. });
  90. },
  91. });
  92. },
  93. // 关闭弹窗
  94. closeModal: function () {
  95. this.setData({
  96. showModal: false, // 隐藏弹窗
  97. });
  98. },
  99. });