Calculation.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_id: 13,
  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. n: this.data.HN,
  79. al_concentration: this.data.Al,
  80. },
  81. };
  82. wx.request({
  83. url: 'http://127.0.0.1:5000/predict',
  84. method: 'POST',
  85. data: JSON.stringify(data),
  86. header: {
  87. 'content-type': 'application/json'
  88. },
  89. success: (res) => {
  90. console.log('预测结果:', res.data.predictions);
  91. // 直接更新页面的 result
  92. if (res.data.result && Array.isArray(res.data.result)) {
  93. const result = res.data.result[0] ? res.data.result[0].toString() : '无结果';
  94. this.setData({
  95. result: result,
  96. showResultPopup: true, // 显示弹窗
  97. });
  98. } else {
  99. console.error('返回数据格式错误');
  100. wx.showToast({
  101. title: '预测结果无效',
  102. icon: 'none'
  103. });
  104. }
  105. },
  106. fail: (error) => {
  107. console.error('请求失败:', error);
  108. wx.showToast({
  109. title: '请求失败,请重试',
  110. icon: 'none'
  111. });
  112. }
  113. });
  114. },
  115. // 关闭弹窗
  116. closePopup: function() {
  117. this.setData({
  118. showResultPopup: false, // 隐藏弹窗
  119. });
  120. },
  121. // 页面加载时
  122. onLoad: function(options) {
  123. // 页面加载时,不再处理解码
  124. },
  125. });