From 91751b1b9b16c0044e16ad80c237b7275a409046 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期四, 06 三月 2025 14:47:12 +0800 Subject: [PATCH] 修改首页和登录相关 --- pages/home/home.js | 444 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 367 insertions(+), 77 deletions(-) diff --git a/pages/home/home.js b/pages/home/home.js index dabd0d4..253cb13 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,8 +16,8 @@ myItem: {}, waterIntakeName: "", image: "/images/ic_head_bg.jpg", - userPhone: "158****0723", - userName: "寮犱笁", + userPhone: "", + userName: "璇风櫥褰�", scrollViewHeight: 0, listData: [], isRefreshing: false, @@ -27,7 +28,11 @@ content: '纭' }, errorDialogTitle: "鍏抽榾閿欒", - showForceConfirm: false //鏄惁寮哄埗寮�闃� + showForceConfirm: false, //鏄惁寮哄埗寮�闃� + lastIntakeName: "", + showProjectDialog: false, + selectedProject: '', + avatarTapCount: 0 }, openValve: function (e) { @@ -74,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(); + }, /** @@ -201,7 +239,7 @@ showForceConfirm: false }); }, - cancelDialog(){ + cancelDialog() { this.setData({ showForceConfirm: false, showConfirm: false @@ -223,7 +261,7 @@ rtuAddr: rtuAddr, vcNum: vcNum, //铏氭嫙鍗D orderNo: orderNo, - operator: app.globalData.sessionId //鎿嶄綔鍛� + operator: app.globalData.operator //鎿嶄綔鍛� }; console.log("postCloseValaue" + data); post({ @@ -263,7 +301,7 @@ const params = { url: 'wx/valve/get', data: { - operator: app.globalData.sessionId + operator: app.globalData.operator } }; get(params).then(data => { @@ -340,57 +378,309 @@ wx.scanCode({ success(res) { console.log(res.result); // 褰撲笖浠呭綋鎵爜涓洪潪寰俊浜岀淮鐮佹椂锛屼細杩斿洖result - wx.showLoading({ - title: '姝e湪寮�闃�璇风◢鍊�...', // 鍔犺浇鎻愮ず鏂囧瓧 - mask: true // 鏄惁鏄剧ず閫忔槑钂欏眰锛岄槻姝㈣Е鎽哥┛閫忥紝榛樿涓� false - }); - const app = getApp(); - const data = { - intakeName: res.result, //鍙栨按鍙D - // vcId: vcId, //铏氭嫙鍗D - operator: app.globalData.sessionId //鎿嶄綔鍛� - }; - post({ - url: "wx/valve/open_wx", - data: data, - timeout: 180000 - }).then(response => { + if (res.result.startsWith("content://")) { + let jsonStr = res.result.replace("content://", "") + try { - // 澶勭悊鎴愬姛鍝嶅簲 - 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: "寮�闃�閿欒" - }) + that.saveData(jsonStr) + } catch (error) { + console.error('Error parsing JSON:', error); } - - }); + } else { + that.postOppenValva(res.result) + } + }, fail(err) { console.error(err); } - }) }, - confirmForceDialog(){ + 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: 'error', + 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 { + + } + }).catch(error => { + // 鍔犺浇瀹屾垚鍚庨殣钘忓姞杞藉姩鐢� + wx.hideLoading(); + // 澶勭悊閿欒鍝嶅簲 + console.error('璇锋眰澶辫触:', error); + }); + }, }) \ No newline at end of file -- Gitblit v1.8.0