From 25ebc714977470b5ed02b0ec7f30ea0615ebd89f Mon Sep 17 00:00:00 2001
From: zuoxiao <zuoxiao>
Date: 星期五, 14 三月 2025 16:48:09 +0800
Subject: [PATCH] 更新应用配置,添加AppID并修改页面路由;优化取水口界面,增加定时开阀功能和相关参数设置;修复首页反馈图标及提示信息。

---
 pages/home/home.js |  488 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 440 insertions(+), 48 deletions(-)

diff --git a/pages/home/home.js b/pages/home/home.js
index 35d0b50..d38e37d 100644
--- a/pages/home/home.js
+++ b/pages/home/home.js
@@ -4,6 +4,7 @@
   get,
   post
 } = require('../../api/request.js');
+const { PROJECT_URLS } = require('../../api/config.js');
 Page({
 
   /**
@@ -15,12 +16,23 @@
     myItem: {},
     waterIntakeName: "",
     image: "/images/ic_head_bg.jpg",
-    userPhone: "158****0723",
-    userName: "寮犱笁",
+    userPhone: "",
+    userName: "璇风櫥褰�",
     scrollViewHeight: 0,
     listData: [],
     isRefreshing: false,
-    isWXRefreshing: false
+    isWXRefreshing: false,
+    errorData: '', //閿欒鍐呭
+    showErrorDialog: false,
+    confirmBtn: {
+      content: '纭'
+    },
+    errorDialogTitle: "鍏抽榾閿欒",
+    showForceConfirm: false, //鏄惁寮哄埗寮�闃�
+    lastIntakeName: "",
+    showProjectDialog: false,
+    selectedProject: '',
+    avatarTapCount: 0
   },
 
   openValve: function (e) {
@@ -67,41 +79,74 @@
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍔犺浇
    */
   onLoad(options) {
-    // 浣跨敤 wx.nextTick 绛夊緟椤甸潰娓叉煋瀹屾垚
-    wx.nextTick(() => {
-      this.calculateScrollViewHeight();
+    // 妫�鏌ユ槸鍚﹀凡閫夋嫨椤圭洰
+    const { PROJECT_URLS } = require('../../api/config.js');
+    
+    storage.getItem('selectedProject').then((project) => {
+      if (project) {
+        this.setData({
+          selectedProject: project
+        });
+        
+        // 纭繚鍏ㄥ眬鍙橀噺瀛樺湪
+        getApp().globalData = getApp().globalData || {};
+        // 璁剧疆 baseUrl
+        const baseUrl = PROJECT_URLS[project];
+        getApp().globalData.baseUrl = baseUrl;
+        getApp().globalData.selectedProject = project;
+        
+        // 鏍规嵁椤圭洰璁剧疆瀵瑰簲鐨則ag
+        if (project === 'JYG') {
+          getApp().globalData.tag = 'ym'; // 鍢夊唱鍏抽」鐩搴攖ag涓簓m
+        } else if (project === 'MQ') {
+          getApp().globalData.tag = 'mq'; // 姘戝嫟椤圭洰瀵瑰簲tag涓簃q
+        }
+        
+        console.log('鍔犺浇宸蹭繚瀛樼殑椤圭洰:', project, '鍩熷悕:', baseUrl, 'tag:', getApp().globalData.tag);
+        
+        // 妫�鏌ョ櫥褰曠姸鎬�
+        if (!getApp().globalData.isLoggedIn) {
+          this.checkLoginStatus();
+          return; // 濡傛灉鏈櫥褰曪紝绛夊緟璺宠浆鍒扮櫥褰曢〉闈�
+        }
+      } else {
+        // 棣栨杩涘叆锛屾樉绀洪」鐩�夋嫨寮圭獥
+        this.setData({
+          showProjectDialog: true
+        });
+        return; // 绛夊緟鐢ㄦ埛閫夋嫨椤圭洰
+      }
+      
+      //鍒ゆ柇鏈湴鏄惁淇濆瓨sessionId
+      // 浣跨敤 wx.nextTick 绛夊緟椤甸潰娓叉煋瀹屾垚
+      wx.nextTick(() => {
+        this.calculateScrollViewHeight();
+      });
+      //褰撳紑闃�鎴愬姛鍚庤皟鐢ㄥ埛鏂�
+      console.log("onLoad锛�" + options.param);
+      if (options.param) {
+        wx.showToast({
+          title: '寮�闃�鎴愬姛',
+          icon: 'success',
+          duration: 3000
+        })
+        this.getOpenList();
+      }
+      this.initData();
+    }).catch(err => {
+      console.error('Failed to get selectedProject:', err);
+      // 鍑洪敊鏃朵篃鏄剧ず椤圭洰閫夋嫨寮圭獥
+      this.setData({
+        showProjectDialog: true
+      });
     });
-    //褰撳紑闃�鎴愬姛鍚庤皟鐢ㄥ埛鏂�
-    console.log("onLoad锛�" + options.param);
-    if (options.param) {
-      wx.showToast({
-        title: '寮�闃�鎴愬姛',
-        icon: 'success',
-        duration: 3000
-      })
-      this.getOpenList();
-    }
-    this.getUserDataBySession();
   },
 
   /**
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍒濇娓叉煋瀹屾垚
    */
   onReady() {
-    //鍒ゆ柇鏈湴鏄惁淇濆瓨sessionId
-    if (storage.isHasKeySync("sessionId")) {
-      storage.getItem('sessionId').then((data) => {
-        this.setData({
-          sessionId: data
-        });
-        if (sessionId !== "") {}
-      }).catch((err) => {
-        console.error('Failed to load parameter:', err);
-      });
-    } else {
-      console.log('Failed to load parameter:false');
-    }
-    this.getOpenList();
+
   },
 
   /**
@@ -154,13 +199,21 @@
 
   },
   recharge() {
-    wx.navigateTo({
-      url: '/pages/rechargeCard/rechargeCard',
+    // wx.navigateTo({
+    //   url: '/pages/rechargeCard/rechargeCard',
+    // })
+    wx.showToast({
+      title: '鏆傛湭寮�鏀�',
+      icon: 'none'
     })
   },
   openValveList() {
-    wx.navigateTo({
-      url: '/pages/valveList/valveList',
+    // wx.navigateTo({
+    //   url: '/pages/valveList/valveList',
+    // })
+    wx.showToast({
+      title: '鏆傛湭寮�鏀�',
+      icon: 'none'
     })
   },
   feedBack() {
@@ -179,7 +232,16 @@
     });
   },
   closeDialog() {
+    console.log("closeDialog");
     this.setData({
+      showConfirm: false,
+      showErrorDialog: false,
+      showForceConfirm: false
+    });
+  },
+  cancelDialog() {
+    this.setData({
+      showForceConfirm: false,
       showConfirm: false
     });
   },
@@ -199,13 +261,14 @@
       rtuAddr: rtuAddr,
       vcNum: vcNum, //铏氭嫙鍗D
       orderNo: orderNo,
-      operator: app.globalData.sessionId //鎿嶄綔鍛�
+      operator: app.globalData.operator //鎿嶄綔鍛�
     };
     console.log("postCloseValaue" + data);
     post({
       url: "wx/valve/close_wx",
       data: data,
-      isShowLoding: false
+      isShowLoding: false,
+      timeout: 185000
     }).then(response => {
 
       // 澶勭悊鎴愬姛鍝嶅簲
@@ -220,13 +283,11 @@
       })
     }).catch(error => {
       wx.hideLoading();
-      if (error.code === "1002") {
-        wx.showToast({
-          title: '鍏抽榾澶辫触',
-          icon: 'error',
-          duration: 3000 // 鎻愮ず鐨勬寔缁椂闂达紝鍗曚綅姣
-        })
-      }
+      this.setData({
+        showErrorDialog: true,
+        errorData: error.msg,
+        errorDialogTitle: "鍏抽榾閿欒"
+      })
       // 澶勭悊閿欒鍝嶅簲
       console.error('璇锋眰澶辫触:', error);
 
@@ -240,7 +301,7 @@
     const params = {
       url: 'wx/valve/get',
       data: {
-        operator: app.globalData.sessionId
+        operator: app.globalData.operator
       }
     };
     get(params).then(data => {
@@ -253,6 +314,12 @@
       // 閿欒鍥炶皟
       this.setData({
         isRefreshing: false, // 灏唗riggered灞炴�ц缃负false锛岃〃绀轰笅鎷夊埛鏂板凡瀹屾垚
+        isWXRefreshing: false, // 灏唗riggered灞炴�ц缃负false锛岃〃绀轰笅鎷夊埛鏂板凡瀹屾垚
+      })
+      wx.showToast({
+        title: err.msg,
+        icon: 'error',
+        duration: 3000
       })
     });
   },
@@ -270,7 +337,7 @@
   getUserDataBySession() {
     const app = getApp();
     const params = {
-      url: 'sell/client/simple_info',
+      url: 'wx/client/simple_info',
       data: {
         sessionId: app.globalData.sessionId
       }
@@ -282,7 +349,11 @@
       })
     }).catch(err => {
       // 閿欒鍥炶皟
-
+      wx.showToast({
+        title: err.msg,
+        icon: 'error',
+        duration: 3000
+      })
     })
   },
   maskPhoneNumber(phoneNumber) {
@@ -292,8 +363,329 @@
     // 浣跨敤姝e垯琛ㄨ揪寮忔浛鎹腑闂村洓浣嶆暟瀛椾负鏄熷彿
     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  
+        if (res.result.startsWith("content://")) {
+          let jsonStr = res.result.replace("content://", "")
+          try {
 
+            that.saveData(jsonStr)
+          } catch (error) {
+            console.error('Error parsing JSON:', error);
+          }
 
+        } else {
+          that.postOppenValva(res.result)
+        }
+
+      },
+      fail(err) {
+        console.error(err);
+      }
+    })
+  },
+  confirmForceDialog() {
+    console.log("confirmForceDialog");
+    this.setData({
+      showForceConfirm: false
+    })
+    this.postOppenValva(this.data.lastIntakeName, true)
+  },
+  /**
+   * 
+   * @param {*} intakeName 
+   */
+  postOppenValva(intakeName, isforce) {
+    const that = this;
+    wx.showLoading({
+      title: '姝e湪寮�闃�璇风◢鍊�...', // 鍔犺浇鎻愮ず鏂囧瓧
+      mask: true // 鏄惁鏄剧ず閫忔槑钂欏眰锛岄槻姝㈣Е鎽哥┛閫忥紝榛樿涓� false
+    });
+    const app = getApp();
+    that.setData({
+      lastIntakeName: intakeName
+    })
+    const data = {
+      intakeName: intakeName, //鍙栨按鍙D
+      // vcId: vcId, //铏氭嫙鍗D
+      operator: app.globalData.operator, //鎿嶄綔鍛�
+      forceOpen: !!isforce // 浣跨敤閫昏緫闈炴搷浣滅 !! 鏉ョ‘淇� isForce 鏄竷灏斿��  
+    };
+    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 === "10005") {
+        that.setData({
+          showForceConfirm: true
+        })
+      } else {
+        that.setData({
+          showErrorDialog: true,
+          errorData: error.msg,
+          errorDialogTitle: "寮�闃�閿欒"
+        })
+      }
+    });
+  },
+  /**
+   * 鎵弿鍚庝繚瀛樼敤鎴穒d鍜宼ag
+   */
+  saveData(userData) {
+    storage.setItem("userData", userData).then(() => {
+      this.initData();
+    }).catch((error) => {});
+
+  },
+  //杩涘叆鐣岄潰鑾峰彇鐣岄潰鏁版嵁
+  initData() {
+    const app = getApp();
+    console.log("tag>>>>:" +app.globalData.tag)
+ 
+    if (storage.isHasKeySync("userData")) {
+      storage.getItem('userData').then((data) => {
+        let jsonObj = JSON.parse(data);
+        app.globalData.sessionId = jsonObj.sessionId;
+        app.globalData.tag = jsonObj.tag;
+        this.getUserDataBySession();
+        this.getOpenList();
+        console.log("userData>>>>>>>" + data)
+      }).catch((err) => {
+        console.error('Failed to load parameter:', err);
+      });
+    } else {
+      this.getUserDataBySession();
+      this.getOpenList();
+      console.log('Failed to load parameter:false');
+    }
+  
+  },
+  // 澶勭悊澶村儚鐐瑰嚮
+  handleAvatarTap() {
+    this.setData({
+      avatarTapCount: this.data.avatarTapCount + 1
+    });
+    
+    if (this.data.avatarTapCount >= 5) {
+      this.setData({
+        showProjectDialog: true,
+        avatarTapCount: 0
+      });
+    }
+  },
+
+  // 澶勭悊寮圭獥鍙鎬у彉鍖�
+  onVisibleChange(e) {
+    // 濡傛灉灏濊瘯鍏抽棴寮圭獥涓旀病鏈夐�夋嫨椤圭洰锛屽垯闃绘鍏抽棴
+    if (!e.detail.visible && !this.data.selectedProject) {
+      return;
+    }
+    this.setData({ showProjectDialog: e.detail.visible });
+  },
+
+  // 澶勭悊椤圭洰閫夋嫨鍙樺寲
+  onProjectChange(event) {
+    console.log('閫夋嫨鐨勯」鐩細', event.detail.value);
+    this.setData({
+      selectedProject: event.detail.value
+    });
+  },
+
+  // 澶勭悊椤圭洰閫夋嫨纭
+  handleProjectConfirm() {
+    if (!this.data.selectedProject) {
+      wx.showToast({
+        title: '璇烽�夋嫨椤圭洰',
+        icon: 'none'
+      });
+      return;
+    }
+    const projectName = this.data.selectedProject === 'JYG' ? '鍢夊唱鍏抽」鐩�' : '姘戝嫟椤圭洰';
+    
+    // 淇濆瓨椤圭洰閫夋嫨鍒版湰鍦板瓨鍌�
+    storage.setItem('selectedProject', this.data.selectedProject).then(() => {
+      // 鏇存柊 BASEURL
+      const { PROJECT_URLS } = require('../../api/config.js');
+      const baseUrl = PROJECT_URLS[this.data.selectedProject];
+      
+      // 鐩存帴淇敼鍏ㄥ眬鍙橀噺
+      getApp().globalData = getApp().globalData || {};
+      getApp().globalData.baseUrl = baseUrl;
+      getApp().globalData.selectedProject = this.data.selectedProject;
+      
+      // 鏍规嵁椤圭洰璁剧疆瀵瑰簲鐨則ag
+      if (this.data.selectedProject === 'JYG') {
+        getApp().globalData.tag = 'ym'; // 鍢夊唱鍏抽」鐩搴攖ag涓簓m
+      } else if (this.data.selectedProject === 'MQ') {
+        getApp().globalData.tag = 'mq'; // 姘戝嫟椤圭洰瀵瑰簲tag涓簃q
+      }
+      
+      console.log('宸插垏鎹㈠埌椤圭洰:', projectName, '鍩熷悕:', baseUrl, 'tag:', getApp().globalData.tag);
+      
+      this.setData({
+        showProjectDialog: false
+      });
+
+      wx.showToast({
+        title: `宸查�夋嫨${projectName}`,
+        icon: 'success',
+        duration: 2000
+      });
+
+      // 妫�鏌ョ櫥褰曠姸鎬�
+      // setTimeout(() => {
+      //   this.checkLoginStatus();
+      // }, 500);
+    }).catch(err => {
+      console.error('淇濆瓨椤圭洰閫夋嫨澶辫触锛�', err);
+      wx.showToast({
+        title: '淇濆瓨澶辫触锛岃閲嶈瘯',
+        icon: 'success',
+        duration: 2000
+      });
+    });
+  },
+  
+  // 妫�鏌ョ櫥褰曠姸鎬�
+  checkLoginStatus() {
+    const app = getApp();
+    
+    // 妫�鏌ユ槸鍚﹀凡鐧诲綍
+    if (app.globalData.isLoggedIn && app.globalData.sessionId) {
+      // 宸茬櫥褰曪紝閲嶆柊鍔犺浇椤甸潰
+      wx.reLaunch({
+        url: '/pages/home/home'
+      });
+    } else {
+      // 灏濊瘯浠庢湰鍦板瓨鍌ㄨ幏鍙栫敤鎴蜂俊鎭�
+      storage.getItem('userInfo').then(userInfoStr => {
+        if (userInfoStr) {
+          try {
+            const userInfo = JSON.parse(userInfoStr);
+            // 楠岃瘉鐢ㄦ埛淇℃伅鏄惁鏈夋晥
+            if (userInfo && userInfo.sessionId) {
+              // 鎭㈠鐧诲綍鐘舵��
+              app.globalData.sessionId = userInfo.sessionId;
+              app.globalData.token = userInfo.token;
+              app.globalData.userInfo = userInfo;
+              app.globalData.isLoggedIn = true;
+              
+              // 宸茬櫥褰曪紝閲嶆柊鍔犺浇椤甸潰
+              wx.reLaunch({
+                url: '/pages/home/home'
+              });
+              return;
+            }
+          } catch (e) {
+            console.error('瑙f瀽鐢ㄦ埛淇℃伅澶辫触:', e);
+          }
+        }
+        
+        // 鏈櫥褰曪紝璺宠浆鍒扮櫥褰曢〉闈�
+        wx.redirectTo({
+          url: `/pages/login/login?project=${this.data.selectedProject}`
+        });
+      }).catch(err => {
+        console.error('鑾峰彇鐢ㄦ埛淇℃伅澶辫触:', err);
+        // 鏈櫥褰曪紝璺宠浆鍒扮櫥褰曢〉闈�
+        wx.redirectTo({
+          url: `/pages/login/login?project=${this.data.selectedProject}`
+        });
+      });
+    }
+  },
+  wxLogin(){
+    if(!getApp().globalData.isLoggedIn){
+      const that = this;
+      wx.login({
+        success: function (res) {
+          if (res.code) {
+            var code = res.code;
+            console.log(code);
+            // 灏哻ode鍙戦�佸埌鏈嶅姟鍣ㄨ幏鍙杘penid
+            that.codeLogin(code);
+          } else {
+            console.log('鐧诲綍澶辫触锛�' + res.errMsg);
+        
+          }
+        }
+      });
+    }
+  },
+  //寰俊code鐧诲綍
+  codeLogin(codeData) {
+    wx.showLoading({
+      title: '姝e湪鐧诲綍璇风◢鍊�...', // 鍔犺浇鎻愮ず鏂囧瓧
+      mask: true // 鏄惁鏄剧ず閫忔槑钂欏眰锛岄槻姝㈣Е鎽哥┛閫忥紝榛樿涓� false
+    });
+    const data = {
+      code: codeData, //涓存椂鐧诲綍鍑瘉
+    };
+    post({
+      url: "wx/client/code_login",
+      data: data,
+    }).then(response => {
+      // 澶勭悊鎴愬姛鍝嶅簲
+      console.log('璇锋眰鎴愬姛:', response);
+      // 鍔犺浇瀹屾垚鍚庨殣钘忓姞杞藉姩鐢�
+      wx.hideLoading();
+      if (response.code === "0001") {
+        //鍋囧涓虹┖鍒欒烦杞埌缁戝畾鐣岄潰
+        if (response.content.client.clientId === "") {
+          wx.navigateTo({
+            url: '/pages/login/login'
+          })
+        } else {
+          //缂撳瓨鍦ㄦ湰鍦�
+          this.setData({
+            isLogin: true
+          })
+          getApp().globalData.sessionId = response.content.client.sessionId
+          storage.setItem("sessionId", response.content.client.sessionId)
+          getApp().globalData.clientId = response.content.client.clientId
+          storage.setItem("clientId", response.content.client.clientId)
+          this.initData();
+        }
+      } else {
+        wx.showToast({
+          title: 'title',
+          icon: 'error',
+          duration: 2000
+        })
+      }
+    }).catch(error => {
+      // 鍔犺浇瀹屾垚鍚庨殣钘忓姞杞藉姩鐢�
+      wx.hideLoading();
+      // 澶勭悊閿欒鍝嶅簲
+      console.error('璇锋眰澶辫触:', error);
+    });
+  },
 })
\ No newline at end of file

--
Gitblit v1.8.0