123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- Page({
- data: {
- result: '', // 用于存储计算结果
- OM: '', // 有机质含量
- CL: '', // 土壤粘粒重量
- CEC: '', // 阳离子交换量
- H_plus: '', // 氢离子含量
- N: '', // 铵离子含量
- Al3_plus: '', // 铝离子含量
- delta_ph: '',
- showResultPopup: false, // 控制弹窗显示与否
- },
- // 仅允许输入数字或小数点
- validateNumberInput: function(value) {
- // 正则表达式:只允许数字和小数点
- const regExp = /^[0-9]*\.?[0-9]*$/;
- if (regExp.test(value)) {
- return value;
- }
- return value.slice(0, -1); // 删除最后一个字符
- },
- // 更新有机质含量
- onOMChange: function(e) {
- this.setData({
- OM: this.validateNumberInput(e.detail.value)
- });
- },
- // 更新土壤粘粒重量
- onCLChange: function(e) {
- this.setData({
- CL: this.validateNumberInput(e.detail.value)
- });
- },
- // 更新阳离子交换量
- onCECChange: function(e) {
- this.setData({
- CEC: this.validateNumberInput(e.detail.value)
- });
- },
- // 更新氢离子含量
- onH_plusChange: function(e) {
- this.setData({
- H_plus: this.validateNumberInput(e.detail.value)
- });
- },
- // 更新铵离子含量
- onNChange: function(e) {
- this.setData({
- N: this.validateNumberInput(e.detail.value)
- });
- },
- // 更新铝离子含量
- onAl3_plusChange: function(e) {
- this.setData({
- Al3_plus: this.validateNumberInput(e.detail.value)
- });
- },
- onDeltaPhChange: function(e) {
- this.setData({
- delta_ph: this.validateNumberInput(e.detail.value)
- });
- },
- // 计算方法
- calculate: function() {
- console.log('开始计算...');
- const data = {
- model_id: 24,
- parameters: {
- organic_matter: this.data.OM,
- chloride: this.data.CL,
- cec: this.data.CEC,
- h_concentration: this.data.H_plus,
- n: this.data.N,
- al_concentration: this.data.Al3_plus,
- },
- };
- wx.request({
- url: 'https://soilgd.com:5000/predict',
- method: 'POST',
- data: JSON.stringify(data),
- header: {
- 'content-type': 'application/json'
- },
- success: (res) => {
- console.log('预测结果:', res.data.predictions);
- // 直接更新页面的 result
- if (res.data.result && Array.isArray(res.data.result)) {
- const result = res.data.result[0] ? res.data.result[0].toString() : '无结果';
- this.setData({
- result: parseFloat(result).toFixed(2),
- showResultPopup: true, // 显示弹窗
- });
- } else {
- console.error('返回数据格式错误');
- wx.showToast({
- title: '预测结果无效',
- icon: 'none'
- });
- }
- },
- fail: (error) => {
- console.error('请求失败:', error);
- wx.showToast({
- title: '请求失败,请重试',
- icon: 'none'
- });
- }
- });
- },
- // 关闭弹窗
- closePopup: function() {
- this.setData({
- showResultPopup: false, // 隐藏弹窗
- });
- },
- // 页面加载时
- onLoad: function(options) {
- // 页面加载时,不再处理解码
- },
- });
|