123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- Page({
- data: {
- result: '', // 存储计算结果
- ph: '', // 土壤PH
- OM: '', // 有机质含量
- CL: '', // 土壤粘粒重量
- H: '', // 氢离子含量
- Al: '', // 铝离子含量
- init_pH: '',
- target_pH: '',
- showModal: false, // 控制弹窗显示与隐藏
- },
- // 更新输入数据
- onOMChange: function (e) {
- this.setData({
- OM: e.detail.value,
- });
- },
- onCLChange: function (e) {
- this.setData({
- CL: e.detail.value,
- });
- },
- onHChange: function (e) {
- this.setData({
- H: e.detail.value,
- });
- },
- onAlChange: function (e) {
- this.setData({
- Al: e.detail.value,
- });
- },
- onPhChange: function (e) {
- this.setData({
- ph: e.detail.value,
- });
- },
- onInitPhChange: function (e) {
- this.setData({
- init_pH: e.detail.value,
- });
- },
- onTargetPhChange: function (e) {
- this.setData({
- target_pH: e.detail.value,
- });
- },
- // 点击按钮后进行计算并提示结果
- // 点击按钮后进行计算并提示结果
- calculate: function () {
- console.log('开始计算...');
- const data = {
- model_id: 6,
- parameters: {
- init_pH: this.data.init_pH,
- target_pH: this.data.target_pH,
- OM: this.data.OM,
- CL: this.data.CL,
- H: this.data.H,
- Al: this.data.Al,
- },
- };
- 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.result);
- // 确保结果显示两位小数
- let result = res.data.result;
- if (result !== null && !isNaN(result)) {
- result = parseFloat(result).toFixed(2); // 将结果格式化为两位小数
- }
- // 更新计算结果
- this.setData({
- result: result,
- showModal: true, // 显示弹窗
- });
- },
- fail: (error) => {
- console.error('请求失败:', error);
- wx.showToast({
- title: '计算失败,请重试',
- icon: 'none',
- });
- },
- });
- },
- // 关闭弹窗
- closeModal: function () {
- this.setData({
- showModal: false, // 隐藏弹窗
- });
- },
- });
|