thres.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // pages/threshold/threshold.js
  2. Page({
  3. data: {
  4. refluxThreshold: null, // 反酸阈值
  5. reduceAcidThreshold: null, // 降酸阈值
  6. newReflux: null, // 新反酸阈值输入
  7. newReduce: null, // 新降酸阈值输入
  8. loading: true
  9. },
  10. onLoad() {
  11. this.getThresholds();
  12. },
  13. // 获取双阈值
  14. getThresholds() {
  15. this.setData({ loading: true });
  16. wx.request({
  17. url: 'https://soilgd.com:5000/get-threshold',
  18. method: 'GET',
  19. success: (res) => {
  20. if (res.statusCode === 200 && res.data) {
  21. this.setData({
  22. reduceThreshold: res.data.reduce.current_threshold || null,
  23. refluxThreshold: res.data.reflux.current_threshold || null
  24. });
  25. }
  26. },
  27. fail: (err) => {
  28. wx.showToast({ title: '获取失败', icon: 'error' });
  29. },
  30. complete: () => {
  31. this.setData({ loading: false });
  32. }
  33. });
  34. },
  35. // 统一输入处理
  36. onInput(e) {
  37. const type = e.currentTarget.dataset.type;
  38. const value = Number(e.detail.value);
  39. const field = `new${type.charAt(0).toUpperCase() + type.slice(1)}`;
  40. if (!isNaN(value)) {
  41. this.setData({ [field]: value });
  42. }
  43. },
  44. // 统一更新处理
  45. updateThreshold(e) {
  46. const type = e.currentTarget.dataset.type;
  47. const field = `new${type.charAt(0).toUpperCase() + type.slice(1)}`;
  48. const value = this.data[field];
  49. if (value === 0) {
  50. wx.showToast({ title: '阈值不能为0', icon: 'error' });
  51. this.setData({ [field]: null }); // 清空非法输入
  52. return;
  53. }
  54. if (value === null || isNaN(value)) {
  55. wx.showToast({ title: '请输入有效数字', icon: 'error' });
  56. return;
  57. }
  58. wx.showLoading({ title: '更新中...' });
  59. wx.request({
  60. url: 'https://soilgd.com:5000/update-threshold',
  61. method: 'POST',
  62. data: {
  63. data_type: type === 'reduce' ? 'reduce' : 'reflux',
  64. threshold: value
  65. },
  66. success: (res) => {
  67. if (res.statusCode === 200) {
  68. wx.showToast({ title: '更新成功' });
  69. this.setData({ [`${type}Threshold`]: value });
  70. }
  71. },
  72. complete: () => wx.hideLoading()
  73. });
  74. }
  75. });