Calculation.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Page({
  2. data: {
  3. result: '', // 用于存储计算结果
  4. OM: '', // 有机质含量
  5. CL: '', // 土壤粘粒重量
  6. CEC: '', // 阳离子交换量
  7. H_plus: '', // 氢离子含量
  8. N: '', // 铵离子含量
  9. Al3_plus: '', // 铝离子含量
  10. delta_ph: '',
  11. showResultPopup: false, // 控制弹窗显示与否
  12. },
  13. // 仅允许输入数字或小数点
  14. validateNumberInput: function(value) {
  15. // 正则表达式:只允许数字和小数点
  16. const regExp = /^[0-9]*\.?[0-9]*$/;
  17. if (regExp.test(value)) {
  18. return value;
  19. }
  20. return value.slice(0, -1); // 删除最后一个字符
  21. },
  22. // 更新有机质含量
  23. onOMChange: function(e) {
  24. this.setData({
  25. OM: this.validateNumberInput(e.detail.value)
  26. });
  27. },
  28. // 更新土壤粘粒重量
  29. onCLChange: function(e) {
  30. this.setData({
  31. CL: this.validateNumberInput(e.detail.value)
  32. });
  33. },
  34. // 更新阳离子交换量
  35. onCECChange: function(e) {
  36. this.setData({
  37. CEC: this.validateNumberInput(e.detail.value)
  38. });
  39. },
  40. // 更新氢离子含量
  41. onH_plusChange: function(e) {
  42. this.setData({
  43. H_plus: this.validateNumberInput(e.detail.value)
  44. });
  45. },
  46. // 更新铵离子含量
  47. onNChange: function(e) {
  48. this.setData({
  49. N: this.validateNumberInput(e.detail.value)
  50. });
  51. },
  52. // 更新铝离子含量
  53. onAl3_plusChange: function(e) {
  54. this.setData({
  55. Al3_plus: this.validateNumberInput(e.detail.value)
  56. });
  57. },
  58. onDeltaPhChange: function(e) {
  59. this.setData({
  60. delta_ph: this.validateNumberInput(e.detail.value)
  61. });
  62. },
  63. // 计算方法
  64. calculate: function() {
  65. console.log('开始计算...');
  66. const data = {
  67. model_id: 13,
  68. parameters: {
  69. organic_matter: this.data.OM,
  70. chloride: this.data.CL,
  71. cec: this.data.CEC,
  72. h_concentration: this.data.H_plus,
  73. n: this.data.N,
  74. al_concentration: this.data.Al3_plus,
  75. },
  76. };
  77. wx.request({
  78. url: 'https://soilgd.com:5000/predict',
  79. method: 'POST',
  80. data: JSON.stringify(data),
  81. header: {
  82. 'content-type': 'application/json'
  83. },
  84. success: (res) => {
  85. console.log('预测结果:', res.data.predictions);
  86. // 直接更新页面的 result
  87. if (res.data.result && Array.isArray(res.data.result)) {
  88. const result = res.data.result[0] ? res.data.result[0].toString() : '无结果';
  89. this.setData({
  90. result: parseFloat(result).toFixed(5),
  91. showResultPopup: true, // 显示弹窗
  92. });
  93. } else {
  94. console.error('返回数据格式错误');
  95. wx.showToast({
  96. title: '预测结果无效',
  97. icon: 'none'
  98. });
  99. }
  100. },
  101. fail: (error) => {
  102. console.error('请求失败:', error);
  103. wx.showToast({
  104. title: '请求失败,请重试',
  105. icon: 'none'
  106. });
  107. }
  108. });
  109. },
  110. // 关闭弹窗
  111. closePopup: function() {
  112. this.setData({
  113. showResultPopup: false, // 隐藏弹窗
  114. });
  115. },
  116. // 页面加载时
  117. onLoad: function(options) {
  118. // 页面加载时,不再处理解码
  119. },
  120. });