|
@@ -1,55 +1,76 @@
|
|
|
+// pages/threshold/threshold.js
|
|
|
Page({
|
|
|
data: {
|
|
|
- countOptions: ['25', '40', '60'], // 条数阈值选项
|
|
|
- percentageOptions: ['25', '40', '60'], // 百分比阈值选项
|
|
|
-
|
|
|
- selectedCountIndex: 0, // 条数默认选择
|
|
|
- selectedPercentageIndex: 0, // 百分比默认选择
|
|
|
-
|
|
|
- countThresh: 25, // 当前条数阈值
|
|
|
- percentageThresh: 25, // 当前百分比阈值
|
|
|
+ threshold: null, // 初始化为null
|
|
|
+ newThreshold: null,
|
|
|
+ loading: true // 添加加载状态
|
|
|
},
|
|
|
|
|
|
- // 条数选择框变化
|
|
|
- onCountPickerChange(e) {
|
|
|
- const selectedValue = this.data.countOptions[e.detail.value];
|
|
|
- this.setData({
|
|
|
- selectedCountIndex: e.detail.value,
|
|
|
- countThresh: selectedValue, // 更新到输入框
|
|
|
- });
|
|
|
+ onLoad() {
|
|
|
+ this.getThreshold();
|
|
|
},
|
|
|
|
|
|
- // 百分比选择框变化
|
|
|
- onPercentagePickerChange(e) {
|
|
|
- const selectedValue = this.data.percentageOptions[e.detail.value];
|
|
|
- this.setData({
|
|
|
- selectedPercentageIndex: e.detail.value,
|
|
|
- percentageThresh: selectedValue, // 更新到输入框
|
|
|
+ // 获取当前阈值(优化版)
|
|
|
+ getThreshold() {
|
|
|
+ this.setData({ loading: true });
|
|
|
+ wx.request({
|
|
|
+ url: 'https://soilgd.com:5000/get-threshold',
|
|
|
+ success: (res) => {
|
|
|
+ if (res.statusCode === 200 && res.data && typeof res.data.current_threshold === 'number') {
|
|
|
+ this.setData({
|
|
|
+ threshold: res.data.current_threshold,
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ wx.showToast({ title: '数据格式错误', icon: 'error' });
|
|
|
+ console.error('Invalid response:', res);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ wx.showToast({ title: '请求失败', icon: 'error' });
|
|
|
+ console.error('API Error:', err);
|
|
|
+ },
|
|
|
+ complete: () => {
|
|
|
+ this.setData({ loading: false });
|
|
|
+ }
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- // 更新条数阈值输入框
|
|
|
- updateCountThreshold(e) {
|
|
|
- this.setData({
|
|
|
- countThresh: e.detail.value, // 允许手动输入
|
|
|
- });
|
|
|
+ // 输入框改变事件(添加校验)
|
|
|
+ onInput(e) {
|
|
|
+ const value = Number(e.detail.value);
|
|
|
+ if (!isNaN(value)) {
|
|
|
+ this.setData({
|
|
|
+ newThreshold: value
|
|
|
+ });
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
- // 更新百分比阈值输入框
|
|
|
- updatePercentageThreshold(e) {
|
|
|
- this.setData({
|
|
|
- percentageThresh: e.detail.value, // 允许手动输入
|
|
|
- });
|
|
|
- },
|
|
|
+ // 提交更新阈值(优化版)
|
|
|
+ updateThreshold() {
|
|
|
+ if (this.data.newThreshold === null || isNaN(this.data.newThreshold)) {
|
|
|
+ wx.showToast({ title: '请输入有效数字', icon: 'error' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- // 保存设置
|
|
|
- saveSettings() {
|
|
|
- wx.showToast({
|
|
|
- title: '保存成功',
|
|
|
- icon: 'success',
|
|
|
+ wx.showLoading({ title: '更新中...' });
|
|
|
+ wx.request({
|
|
|
+ url: 'https://soilgd.com:5000/update-threshold',
|
|
|
+ method: 'POST',
|
|
|
+ data: { threshold: this.data.newThreshold },
|
|
|
+ success: (res) => {
|
|
|
+ if (res.statusCode === 200) {
|
|
|
+ wx.showToast({ title: '更新成功' });
|
|
|
+ this.setData({ threshold: this.data.newThreshold });
|
|
|
+ } else {
|
|
|
+ wx.showToast({ title: '更新失败', icon: 'error' });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ wx.showToast({ title: '网络错误', icon: 'error' });
|
|
|
+ },
|
|
|
+ complete: () => {
|
|
|
+ wx.hideLoading();
|
|
|
+ }
|
|
|
});
|
|
|
-
|
|
|
- console.log('条数阈值:', this.data.countThresh);
|
|
|
- console.log('百分比阈值:', this.data.percentageThresh);
|
|
|
- },
|
|
|
-});
|
|
|
+ }
|
|
|
+});
|