Calculation.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. Page({
  2. data: {
  3. result: '', // 用于存储计算结果
  4. OM: '', // 有机质含量
  5. CL: '', // 土壤粘粒重量
  6. CEC: '', // 阳离子交换量
  7. H: '', // 氢离子含量
  8. HN: '', // 铵离子含量
  9. Al: '', // 铝离子含量
  10. free_alumina: '', // 游离氧化铝含量
  11. free_iron_oxides: '', // 游离氧化铁含量
  12. delta_ph: '',
  13. showResultPopup: false, // 控制弹窗显示与否
  14. },
  15. // 更新有机质含量
  16. onOMChange: function(e) {
  17. this.setData({
  18. OM: e.detail.value
  19. });
  20. },
  21. // 更新土壤粘粒重量
  22. onCLChange: function(e) {
  23. this.setData({
  24. CL: e.detail.value
  25. });
  26. },
  27. // 更新阳离子交换量
  28. onCECChange: function(e) {
  29. this.setData({
  30. CEC: e.detail.value
  31. });
  32. },
  33. // 更新氢离子含量
  34. onHChange: function(e) {
  35. this.setData({
  36. H: e.detail.value
  37. });
  38. },
  39. // 更新铵离子含量
  40. onHNChange: function(e) {
  41. this.setData({
  42. HN: e.detail.value
  43. });
  44. },
  45. // 更新铝离子含量
  46. onAlChange: function(e) {
  47. this.setData({
  48. Al: e.detail.value
  49. });
  50. },
  51. // 更新游离氧化铝含量
  52. onFreeAluminaChange: function(e) {
  53. this.setData({
  54. free_alumina: e.detail.value
  55. });
  56. },
  57. // 更新游离氧化铁含量
  58. onFreeIronOxidesChange: function(e) {
  59. this.setData({
  60. free_iron_oxides: e.detail.value
  61. });
  62. },
  63. onDeltaPhChange: function(e) {
  64. this.setData({
  65. delta_ph: e.detail.value
  66. });
  67. },
  68. // 计算方法
  69. calculate: function() {
  70. console.log('开始计算...');
  71. const data = {
  72. model_name: "RF_filt",
  73. parameters: {
  74. organic_matter: this.data.OM,
  75. chloride: this.data.CL,
  76. cec: this.data.CEC,
  77. h_concentration: this.data.H,
  78. hn: this.data.HN,
  79. al_concentration: this.data.Al,
  80. free_alumina: this.data.free_alumina,
  81. free_iron: this.data.free_iron_oxides,
  82. delta_ph: this.data.delta_ph,
  83. },
  84. };
  85. wx.request({
  86. url: 'http://127.0.0.1:5000/predict',
  87. method: 'POST',
  88. data: JSON.stringify(data),
  89. header: {
  90. 'content-type': 'application/json'
  91. },
  92. success: (res) => {
  93. console.log('预测结果:', res.data.predictions);
  94. // 直接更新页面的 result
  95. if (res.data.predictions && Array.isArray(res.data.predictions)) {
  96. const result = res.data.predictions[0] ? res.data.predictions[0].toString() : '无结果';
  97. this.setData({
  98. result: result,
  99. showResultPopup: true, // 显示弹窗
  100. });
  101. } else {
  102. console.error('返回数据格式错误');
  103. wx.showToast({
  104. title: '预测结果无效',
  105. icon: 'none'
  106. });
  107. }
  108. },
  109. fail: (error) => {
  110. console.error('请求失败:', error);
  111. wx.showToast({
  112. title: '请求失败,请重试',
  113. icon: 'none'
  114. });
  115. }
  116. });
  117. },
  118. // 关闭弹窗
  119. closePopup: function() {
  120. this.setData({
  121. showResultPopup: false, // 隐藏弹窗
  122. });
  123. },
  124. // 页面加载时
  125. onLoad: function(options) {
  126. // 页面加载时,不再处理解码
  127. },
  128. });