管灌系统农户端微信小程序(嘉峪关应用)
zuoxiao
2024-08-12 19804e37592d2250c551fcf5df1d87cdf551f1d1
pages/home/home.js
@@ -11,41 +11,23 @@
   */
  data: {
    sessionId: "",
    showConfirm: false,
    myItem: {},
    waterIntakeName: "",
    image: "/images/ic_head_bg.jpg",
    userPhone: "158****0723",
    userName: "张三",
    scrollViewHeight: 0,
    listData: [{
        name: '取水口 1',
        code: '1055201'
      },
      {
        name: '取水口 2',
        code: '1055201'
      },
      {
        name: '取水口 3',
        code: '1055201'
      },
      {
        name: '取水口 3',
        code: '1055201'
      },
      {
        name: '取水口 4',
        code: '1055201'
      },
      {
        name: '取水口 5',
        code: '1055201'
      },
      {
        name: '取水口 6',
        code: '1055201'
      }
    ],
    listData: [],
    isRefreshing: false,
    isWXRefreshing: false
    isWXRefreshing: false,
    errorData: '', //错误内容
    showErrorDialog: false,
    confirmBtn: {
      content: '确认'
    },
    errorDialogTitle: "关阀错误",
    showForceConfirm: false //是否强制开阀
  },
  openValve: function (e) {
@@ -68,12 +50,8 @@
      this.setData({
        isRefreshing: true
      });
      setTimeout(function () {
        self.setData({
          isRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        })
        console.log('下拉刷新已完成');
      }, 3000);
      this.getOpenList();
    }
  },
@@ -100,7 +78,17 @@
    wx.nextTick(() => {
      this.calculateScrollViewHeight();
    });
    //当开阀成功后调用刷新
    console.log("onLoad:" + options.param);
    if (options.param) {
      wx.showToast({
        title: '开阀成功',
        icon: 'success',
        duration: 3000
      })
      this.getOpenList();
    }
    this.getUserDataBySession();
  },
  /**
@@ -113,15 +101,14 @@
        this.setData({
          sessionId: data
        });
        if (sessionId !== "") {
        }
        if (sessionId !== "") {}
      }).catch((err) => {
        console.error('Failed to load parameter:', err);
      });
    } else {
      console.log('Failed to load parameter:false');
    }
    this.getOpenList();
  },
  /**
@@ -157,13 +144,7 @@
    });
    console.log(this.data.isRefreshing);
    // 数据请求完成后,停止下拉刷新的动画
    var self = this;
    setTimeout(function () {
      self.setData({
        isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
      })
      console.log('下拉刷新已完成');
    }, 3000);
    this.getOpenList();
  },
  /**
@@ -178,5 +159,238 @@
   */
  onShareAppMessage() {
  },
  recharge() {
    // wx.navigateTo({
    //   url: '/pages/rechargeCard/rechargeCard',
    // })
    wx.showToast({
      title: '暂未开放',
      icon: 'none'
    })
  },
  openValveList() {
    // wx.navigateTo({
    //   url: '/pages/valveList/valveList',
    // })
    wx.showToast({
      title: '暂未开放',
      icon: 'none'
    })
  },
  feedBack() {
    wx.showToast({
      title: '暂未开放',
      icon: 'none'
    })
  },
  handleChange(e) {
    const item = e.currentTarget.dataset.item;
    console.log(item);
    this.setData({
      showConfirm: true,
      waterIntakeName: item.intakeNum,
      myItem: item
    });
  },
  closeDialog() {
    console.log("closeDialog");
    this.setData({
      showConfirm: false,
      showErrorDialog: false,
      showForceConfirm: false
    });
  },
  cancelDialog(){
    this.setData({
      showForceConfirm: false,
      showConfirm: false
    });
  },
  /**
   * 关闭阀门
   * @param {*} orderNo 订单号
   * @param {*} rtuAddr 阀控器地址
   * @param {*} vcNum 虚拟卡编号
   */
  postCloseValaue(orderNo, rtuAddr, vcNum) {
    wx.showLoading({
      title: '正在关阀请稍候...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    const app = getApp();
    const data = {
      rtuAddr: rtuAddr,
      vcNum: vcNum, //虚拟卡ID
      orderNo: orderNo,
      operator: app.globalData.sessionId //操作员
    };
    console.log("postCloseValaue" + data);
    post({
      url: "wx/valve/close_wx",
      data: data,
      isShowLoding: false,
      timeout: 185000
    }).then(response => {
      // 处理成功响应
      console.log('请求成功:', response);
      // 加载完成后隐藏加载动画
      wx.hideLoading();
      this.getOpenList();
      wx.showToast({
        title: '关阀成功',
        icon: 'success',
        duration: 3000
      })
    }).catch(error => {
      wx.hideLoading();
      this.setData({
        showErrorDialog: true,
        errorData: error.msg,
        errorDialogTitle: "关阀错误"
      })
      // 处理错误响应
      console.error('请求失败:', error);
    });
  },
  /**
   * 获取为关阀记录
   */
  getOpenList() {
    const app = getApp();
    const params = {
      url: 'wx/valve/get',
      data: {
        operator: app.globalData.sessionId
      }
    };
    get(params).then(data => {
      this.setData({
        listData: data.content,
        isRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
      })
    }).catch(err => {
      // 错误回调
      this.setData({
        isRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
        isWXRefreshing: false, // 将triggered属性设置为false,表示下拉刷新已完成
      })
      wx.showToast({
        title: err.msg,
        icon: 'error',
        duration: 3000
      })
    });
  },
  /**
   * 确认关闭回调
   * @param {} item
   */
  confirmDialog() {
    this.setData({
      showConfirm: false
    });
    this.postCloseValaue(this.data.myItem.orderNo, this.data.myItem.rtuAddr, this.data.myItem.vcNum);
  },
  //根据session获取农户信息
  getUserDataBySession() {
    const app = getApp();
    const params = {
      url: 'wx/client/simple_info',
      data: {
        sessionId: app.globalData.sessionId
      }
    };
    get(params).then(data => {
      this.setData({
        userName: data.content.clientName,
        userPhone: this.maskPhoneNumber(data.content.phone)
      })
    }).catch(err => {
      // 错误回调
      wx.showToast({
        title: err.msg,
        icon: 'error',
        duration: 3000
      })
    })
  },
  maskPhoneNumber(phoneNumber) {
    if (phoneNumber.length !== 11) {
      throw new Error("Phone number must be 11 digits");
    }
    // 使用正则表达式替换中间四位数字为星号
    const maskedPhoneNumber = phoneNumber.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
    return maskedPhoneNumber;
  },
  closeDialog() {
    this.setData({
      showErrorDialog: false
    })
  },
  /**
   * 扫码开阀
   */
  scenCode() {
    const that = this;
    wx.scanCode({
      success(res) {
        console.log(res.result); // 当且仅当扫码为非微信二维码时,会返回result
        wx.showLoading({
          title: '正在开阀请稍候...', // 加载提示文字
          mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
        });
        const app = getApp();
        const data = {
          intakeName: res.result, //取水口ID
          // vcId: vcId, //虚拟卡ID
          operator: app.globalData.sessionId //操作员
        };
        post({
          url: "wx/valve/open_wx",
          data: data,
          timeout: 180000
        }).then(response => {
          // 处理成功响应
          console.log('请求成功:', response);
          // 加载完成后隐藏加载动画
          wx.hideLoading();
          //完成后回到首页
          wx.reLaunch({
            url: '/pages/home/home?param=true' // 首页的路径,根据实际情况填写
          });
        }).catch(error => {
          // 加载完成后隐藏加载动画
          wx.hideLoading();
          // 处理错误响应
          console.error('请求失败:', error);
          if (error.code==="1003") {
            that.setData({
              showForceConfirm:true
            })
          } else {
            that.setData({
              showErrorDialog: true,
              errorData: error.msg,
              errorDialogTitle: "开阀错误"
            })
          }
        });
      },
      fail(err) {
        console.error(err);
      }
    })
  },
  confirmForceDialog(){
    console.log("confirmForceDialog");
  }
})