沙盘演示系统应用的微信小程序
pages/home/home.js
@@ -34,6 +34,8 @@
    showInfoDialog: false,
    showTipDialog: false,
    options: '',
    showUnBind: false,
    useTestData: false  // 新增:是否使用测试数据的标志
  },
  openValve: function (e) {
@@ -57,6 +59,14 @@
    }).exec();
  },
  startPullDownRefresh() {
    if (this.data.useTestData) {
      this.setData({
        isRefreshing: true
      });
      this.getOpenList();
      return;
    }
    if (this.data.isLogin) {
      if (!this.data.isWXRefreshing) {
        var self = this;
@@ -91,7 +101,7 @@
  onLoad(options) {
    //当开阀成功后调用刷新
    console.log("onLoad:" + options.param);
    if (options.param) {
    if (options.param === "1" || options.param === "2") {
      this.setData({
        options: options,
        isLogin: true,
@@ -104,6 +114,17 @@
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // 直接加载测试数据
    if (this.data.useTestData) {
      this.setData({
        isLogin: true, // 设置为已登录状态以便能看到测试数据
        userName: "测试用户",
        userPhone: "135****6789"
      });
      this.getOpenList();
      return;
    }
    //判断本地是否保存sessionId
    if (storage.isHasKeySync("sessionId")) {
      const app = getApp();
@@ -113,40 +134,51 @@
          isLogin: true
        })
        if (app.globalData.sessionId) {
          this.getOpenList();
          this.getUserDataBySession();
        }
      }).catch((err) => {});
      storage.getItem('clientId').then((data) => {
        app.globalData.clientId = data;
        this.getOpenList();
      }).catch((err) => {});
    } else {
      //本地没有缓存微信登录
      this.wxLogin();
    }
    if (this.data.options.param) {
    if (this.data.options.param === "1") {
      this.setData({
        showTipDialog: true,
        tipData: "开阀命令下发成功,约20-60秒后可刷新快速关阀列表查看执行结果。"
        tipData: "开阀命令下发成功,因开阀需要时间,约20-60秒后可刷新快速关阀列表查看执行结果。"
      })
      setTimeout(() => {
        this.getOpenList();
      }, 20000)
    } else if (this.data.options.param === "2") {
      this.setData({
        showTipDialog: true,
        tipData: "预约开阀命令下发成功,当到达预约时间并且成功开阀后快速关阀列表会显示未关阀记录"
      })
      this.getOpenList();
    }
  },
  wxLogin() {
    const that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          var code = res.code;
          console.log(code);
          // 将code发送到服务器获取openid
          that.codeLogin(code);
        } else {
          console.log('登录失败!' + res.errMsg);
    // if (!this.data.isLogin) {
      const that = this;
      wx.login({
        success: function (res) {
          if (res.code) {
            var code = res.code;
            console.log(code);
            // 将code发送到服务器获取openid
            that.codeLogin(code);
          } else {
            console.log('登录失败!' + res.errMsg);
          }
        }
      }
    });
      });
    // }
  },
  //微信code登录
  codeLogin(codeData) {
@@ -268,7 +300,8 @@
    this.setData({
      showForceConfirm: false,
      showConfirm: false,
      showTipDialog: false
      showTipDialog: false,
      showUnBind: false
    });
  },
  /**
@@ -308,7 +341,9 @@
          showTipDialog: true,
          tipData: "关阀命令下发成功,约20-60秒后可刷新快速关阀列表查看执行结果。"
        })
        this.getOpenList();
        setTimeout(() => {
          this.getOpenList();
        }, 40000)
      } else {
        wx.showToast({
          title: '命令执行失败',
@@ -329,15 +364,88 @@
    });
  },
  /**
   * 生成测试数据 - 新增函数
   */
  generateTestData() {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    const dateString = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    const orderBase = `TEST-${year}${month}${day}-`;
    const testData = [
      {
        intakeNum: "取水口1",
        isOnLine: true,
        dt: dateString,
        waterInstant: "1.23",
        orderNo: orderBase + "001",
        rtuAddr: "TEST-RTU-001",
        vcNum: "TEST-VC-001",
        planned: false,
        time: this.extractTime(dateString),
        deleteText: "删除",
        displayText: "关阀",
        isOpen: true
      },
      {
        intakeNum: "取水口2",
        isOnLine: false,
        dt: dateString,
        waterInstant: "2.45",
        orderNo: orderBase + "002",
        rtuAddr: "TEST-RTU-002",
        vcNum: "TEST-VC-002",
        planned: false,
        time: this.extractTime(dateString),
        deleteText: "删除",
        displayText: "关阀",
        isOpen: true
      },
      {
        intakeNum: "取水口3",
        isOnLine: true,
        dt: dateString,
        waterInstant: "3.67",
        orderNo: orderBase + "003",
        rtuAddr: "TEST-RTU-003",
        vcNum: "TEST-VC-003",
        planned: true,
        time: this.extractTime(dateString),
        deleteText: "删除",
        displayText: "取消",
        isOpen: true
      }
    ];
    return testData;
  },
  /**
   * 获取为关阀记录
   */
  getOpenList() {
    if (this.data.isLogin) {
      // 如果设置了使用测试数据,则直接使用测试数据
      if (this.data.useTestData) {
        const testData = this.generateTestData();
        this.setData({
          listData: testData,
          isRefreshing: false,
          isWXRefreshing: false,
        });
        this.updateDisplayText();
        return;
      }
      const app = getApp();
      const params = {
        url: 'wx/valve/get',
        data: {
          operator: app.globalData.sessionId
          operator: app.globalData.clientId
        }
      };
      get(params).then(data => {
@@ -379,6 +487,30 @@
    this.setData({
      showConfirm: false
    });
    if (this.data.useTestData) {
      // 测试数据模式下,模拟关阀操作
      wx.showLoading({
        title: '正在关阀请稍候...',
        mask: true
      });
      // 模拟请求延迟
      setTimeout(() => {
        wx.hideLoading();
        // 从列表中移除被关闭的阀门
        const updatedList = this.data.listData.filter(item =>
          item.orderNo !== this.data.myItem.orderNo
        );
        this.setData({
          listData: updatedList,
          showTipDialog: true,
          tipData: "关阀命令下发成功,约20-60秒后可刷新快速关阀列表查看执行结果。"
        });
      }, 1500);
      return;
    }
    this.postCloseValaue(this.data.myItem.orderNo, this.data.myItem.rtuAddr, this.data.myItem.vcNum);
  },
  //根据session获取农户信息
@@ -548,13 +680,40 @@
  onDelete(e) {
    const item = e.currentTarget.dataset.item;
    const that = this;
    if (this.data.useTestData) {
      // 测试数据模式下,模拟删除操作
      wx.showLoading({
        title: '正在强制删除请稍候...',
        mask: true
      });
      // 模拟请求延迟
      setTimeout(() => {
        wx.hideLoading();
        // 从列表中移除被删除的项
        const updatedList = this.data.listData.filter(listItem =>
          listItem.orderNo !== item.orderNo
        );
        this.setData({
          listData: updatedList
        });
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        });
      }, 1500);
      return;
    }
    wx.showLoading({
      title: '正在强制删除请稍候...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
    });
    that.setData({
      lastIntakeName: intakeName
    })
    const data = {
      vcNum: item.vcNum, //取水口ID
      rtuAddr: item.rtuAddr, //阀控器地址
@@ -586,9 +745,22 @@
      } else {
        displayText = "关阀"
      }
      let deleteText = "删除"
      let time;
      if (!item.dt) {
        time = "暂无"
      } else {
        time = this.extractTime(item.dt)
      }
      if (item.waterInstant===null) {
        item.waterInstant = "暂无"
      }
      return {
        ...item,
        displayText
        displayText,
        deleteText,
        time
      }; // 保留所有其他字段,并添加 displayText 字段
    });
    // 更新列表数据
@@ -598,11 +770,14 @@
  },
  //解绑
  unbind() {
    //清空数据
    this.setData({
      userPhone: "未登录",
      userName: "请登录",
      listData: []
      showUnBind: true
    })
  },
  //确认解绑
  unBindPost() {
    this.setData({
      showUnBind: false
    })
    wx.showLoading({
      title: '正在解绑请稍候...', // 加载提示文字
@@ -641,6 +816,11 @@
      wx.hideLoading();
      // 处理错误响应
      console.error('请求失败:', error);
      wx.showToast({
        title: '解绑失败',
        icon: 'error',
        duration: 3000
      })
    });
  },
  //点击提示按钮
@@ -654,5 +834,29 @@
    this.setData({
      showInfoDialog: false
    })
  },
  //处理时间去掉年月日
  extractTime(datetimeString) {
    const formattedDate = datetimeString.replace(" ", "T");
    const date = new Date(formattedDate);
    // 获取小时、分钟和秒
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    const seconds = date.getSeconds().toString().padStart(2, '0');
    return `${hours}:${minutes}:${seconds}`;
  },
  onShareAppMessage: function (options) {
    // 设置分享内容
    return {
      title: '大禹灌溉助手', // 分享标题
      path: '/pages/home/home', // 分享路径,该路径需要是当前小程序存在的页面路径
      imageUrl: '/images/logo2.jpg', // 分享图标,可以是网络图片路径或本地图片路径
      success: function (res) {
        // 分享成功的回调函数
      },
      fail: function (res) {
        // 分享失败的回调函数
      }
    };
  }
})