From ab8b53407a4d2213b1b3cdaf3cf649ef6c457dae Mon Sep 17 00:00:00 2001
From: zuoxiao <zuoxiao>
Date: 星期一, 28 四月 2025 15:52:19 +0800
Subject: [PATCH] 优化首页项目选择逻辑,添加临时选择变量以支持未确认的项目选择;更新登录页面逻辑,确保从登录页返回时正确刷新数据并处理项目选择确认,提升用户体验。

---
 pages/home/home.js   |  142 ++++++++++++++++++++++++++---------
 pages/login/login.js |   72 +++++++++++++++---
 pages/home/home.wxml |    4 
 3 files changed, 166 insertions(+), 52 deletions(-)

diff --git a/pages/home/home.js b/pages/home/home.js
index a997e2b..1257aa1 100644
--- a/pages/home/home.js
+++ b/pages/home/home.js
@@ -39,6 +39,7 @@
     lastIntakeName: "",
     showProjectDialog: false,
     selectedProject: '',
+    tempSelectedProject: '', // 娣诲姞涓存椂閫夋嫨鍙橀噺锛岀敤浜庝繚瀛樼敤鎴峰湪瀵硅瘽妗嗕腑閫夋嫨浣嗗皻鏈‘璁ょ殑椤圭洰
     avatarTapCount: 0,
     isFromLogin: false,
     showTipDialog: '',
@@ -319,9 +320,34 @@
       });
     }
 
+    // 灏濊瘯鑾峰彇eventChannel锛岀敤浜庣洃鍚櫥褰曢〉闈㈠彂鍥炵殑娑堟伅
+    try {
+      const eventChannel = currentPage.getOpenerEventChannel();
+      if (eventChannel && eventChannel.on) {
+        eventChannel.on('fromLogin', (data) => {
+          console.log('onShow: 鎺ユ敹鍒扮櫥褰曢〉闈㈣繑鍥炵殑娑堟伅', data);
+          if (data && data.fromLogin) {
+            fromLogin = true;
+            this.setData({
+              isFromLogin: true
+            });
+            
+            // 鐧诲綍鎴愬姛鍚庡埛鏂版暟鎹�
+            console.log('鐧诲綍鎴愬姛鍚庡埛鏂版暟鎹�');
+            this.initData();
+          }
+        });
+      }
+    } catch (e) {
+      console.error('鑾峰彇eventChannel澶辫触:', e);
+    }
+
     // 鍒濆鍖栧鐞�
     if (fromLogin || this.data.isFromLogin) {
       console.log('onShow: 浠庣櫥褰曢〉杩斿洖锛屼笉杩涜鐧诲綍妫�鏌�');
+      
+      // 濡傛灉鏄粠鐧诲綍椤佃繑鍥烇紝閲嶆柊鍔犺浇鏁版嵁
+      this.initData();
     } else {
       console.log('onShow: 姝e父鏄剧ず椤甸潰');
       // 寤惰繜妫�鏌ョ櫥褰曠姸鎬侊紝纭繚鑳芥纭瘑鍒复鏃舵爣璁�
@@ -656,12 +682,6 @@
   },
   //鏍规嵁session鑾峰彇鍐滄埛淇℃伅
   getUserDataBySession() {
-    // 鍏堟鏌ユ槸鍚︿粠鐧诲綍椤佃繑鍥�
-    if (this.getFromLogin()) {
-      console.log('getUserDataBySession: 妫�娴嬪埌浠庣櫥褰曢〉杩斿洖鐨勬爣璁帮紝涓嶆墽琛孉PI璇锋眰');
-      return;
-    }
-
     const app = getApp();
 
     // 妫�鏌ユ槸鍚︽湁sessionId
@@ -825,35 +845,67 @@
 
     // 棣栧厛妫�鏌ユ槸鍚︿粠鐧诲綍椤佃繑鍥�
     if (this.getFromLogin()) {
-      console.log('initData: 妫�娴嬪埌浠庣櫥褰曢〉杩斿洖鐨勬爣璁帮紝浠呰幏鍙栧熀鏈暟鎹�');
+      console.log('initData: 妫�娴嬪埌浠庣櫥褰曢〉杩斿洖鐨勬爣璁帮紝鎵ц瀹屾暣鐨勭敤鎴锋暟鎹幏鍙�');
 
-      // 鍗充娇浠庣櫥褰曢〉杩斿洖锛屼篃灏濊瘯鑾峰彇寮�闃�鍒楄〃浠ユ樉绀哄熀鏈琔I
-      // 浣嗗厛妫�鏌ユ槸鍚︽湁瀹㈡埛绔疘D鍙敤
-      if (!app.globalData.clientId) {
-        console.log('initData: 浠庣櫥褰曢〉杩斿洖涓旀棤clientId锛屽皾璇曚粠瀛樺偍鎭㈠');
-        // 灏濊瘯浠庡瓨鍌ㄦ仮澶峜lientId
-        storage.getItemSafe('clientId')
-          .then(clientId => {
-            if (clientId) {
-              console.log('initData: 浠庡瓨鍌ㄦ仮澶峜lientId鎴愬姛:', clientId);
-              app.globalData.clientId = clientId;
-              this.getOpenList();
-            } else {
-              console.log('initData: 鏃犳硶鎭㈠clientId锛屾樉绀虹┖鍒楄〃');
-              this.setData({
-                listData: []
-              });
-            }
-          })
-          .catch(err => {
-            console.error('initData: 鎭㈠clientId澶辫触:', err);
-            this.setData({
-              listData: []
-            });
-          });
-      } else {
+      // 妫�鏌ユ槸鍚︽湁sessionId鍜宑lientId锛屼紭鍏堜娇鐢ㄥ叏灞�鍙橀噺
+      if (app.globalData.sessionId && app.globalData.clientId) {
+        console.log('initData: 鍏ㄥ眬鍙橀噺涓湁鐧诲綍淇℃伅锛岀洿鎺ヨ幏鍙栫敤鎴锋暟鎹拰寮�闃�鍒楄〃');
+        this.getUserDataBySession();
         this.getOpenList();
+        return;
       }
+
+      // 濡傛灉娌℃湁鍏ㄥ眬鍙橀噺锛屽皾璇曚粠鏈湴瀛樺偍涓仮澶�
+      Promise.all([
+        storage.getItemSafe('userData'),
+        storage.getItemSafe('sessionId'),
+        storage.getItemSafe('clientId')
+      ]).then(([userData, sessionId, clientId]) => {
+        if (userData) {
+          try {
+            // 瑙f瀽userData骞惰缃叏灞�鍙橀噺
+            let jsonObj = JSON.parse(userData);
+            app.globalData.sessionId = jsonObj.sessionId;
+            app.globalData.clientId = jsonObj.clientId || clientId;
+            app.globalData.tag = jsonObj.tag;
+            app.globalData.isLoggedIn = true;
+            
+            // 鐩存帴浠巙serData璁剧疆鐢ㄦ埛淇℃伅
+            if (jsonObj.clientName && jsonObj.phone) {
+              this.setData({
+                userName: jsonObj.clientName,
+                userPhone: this.maskPhoneNumber(jsonObj.phone)
+              });
+            } else {
+              // 濡傛灉userData涓病鏈夌敤鎴蜂俊鎭紝璋冪敤鎺ュ彛鑾峰彇
+              this.getUserDataBySession();
+            }
+            
+            console.log("userData宸插姞杞�:", userData);
+          } catch (e) {
+            console.error('userData瑙f瀽澶辫触:', e);
+            if (sessionId && clientId) {
+              app.globalData.sessionId = sessionId;
+              app.globalData.clientId = clientId;
+              app.globalData.isLoggedIn = true;
+              this.getUserDataBySession();
+            }
+          }
+        } else if (sessionId && clientId) {
+          // 濡傛灉娌℃湁userData浣嗘湁session鍜宑lient ID
+          app.globalData.sessionId = sessionId;
+          app.globalData.clientId = clientId;
+          app.globalData.isLoggedIn = true;
+          this.getUserDataBySession();
+        }
+        
+        // 鏃犺濡備綍閮藉皾璇曡幏鍙栧紑闃�鍒楄〃
+        this.getOpenList();
+      }).catch(err => {
+        console.error('鎭㈠鐧诲綍鐘舵�佸け璐�:', err);
+        this.getOpenList();
+      });
+      
       return;
     }
 
@@ -984,7 +1036,8 @@
     if (this.data.avatarTapCount >= 5) {
       this.setData({
         showProjectDialog: true,
-        avatarTapCount: 0
+        avatarTapCount: 0,
+        tempSelectedProject: this.data.selectedProject // 鍒濆鍖栦复鏃堕�夋嫨涓哄綋鍓嶉�夋嫨
       });
     }
   },
@@ -996,23 +1049,28 @@
       return;
     }
     this.setData({
-      showProjectDialog: e.detail.visible
+      showProjectDialog: e.detail.visible,
+      // 褰撴樉绀哄璇濇鏃讹紝鍒濆鍖栦复鏃堕�夋嫨涓哄綋鍓嶉�夋嫨
+      tempSelectedProject: e.detail.visible ? this.data.selectedProject : ''
     });
   },
 
   // 澶勭悊椤圭洰閫夋嫨鍙樺寲
   onProjectChange(event) {
     console.log('閫夋嫨鐨勯」鐩細', event.detail.value);
+    // 涓嶇珛鍗宠缃埌state锛岃�屾槸瀛樺偍鍒颁复鏃跺彉閲忎腑
     this.setData({
-      selectedProject: event.detail.value
+      tempSelectedProject: event.detail.value
     });
   },
 
   // 澶勭悊椤圭洰閫夋嫨纭
   handleProjectConfirm() {
-    console.log('寮�濮嬪鐞嗛」鐩�夋嫨纭', this.data.selectedProject, this.data.projectList);
+    // 浠庝复鏃跺彉閲忚幏鍙栭�夋嫨鐨勯」鐩�
+    const selectedProject = this.data.tempSelectedProject || this.data.selectedProject;
+    console.log('寮�濮嬪鐞嗛」鐩�夋嫨纭', selectedProject, this.data.projectList);
 
-    if (!this.data.selectedProject) {
+    if (!selectedProject) {
       wx.showToast({
         title: '璇烽�夋嫨椤圭洰',
         icon: 'none'
@@ -1022,7 +1080,7 @@
 
     // 鑾峰彇褰撳墠宸查�夐」鐩拰鏂伴�夋嫨鐨勯」鐩�
     const currentProject = getApp().globalData.selectedProject;
-    const newProject = this.data.selectedProject;
+    const newProject = selectedProject;
     console.log('褰撳墠椤圭洰:', currentProject, '鏂伴�夋嫨椤圭洰:', newProject);
     console.log('PROJECT_CONFIG 鍐呭:', PROJECT_CONFIG);
 
@@ -1052,11 +1110,19 @@
       cancelText: '鍙栨秷',
       success: (res) => {
         if (res.confirm) {
+          // 鐢ㄦ埛纭閫夋嫨鍚庯紝鏇存柊鐘舵�佸苟缁х画澶勭悊
+          this.setData({
+            selectedProject: newProject
+          });
           // 鐢ㄦ埛纭閫夋嫨锛岀户缁鐞�
           this.processProjectSelection(currentProject, newProject, projectConfig, isProjectChanged);
         } else {
           // 鐢ㄦ埛鍙栨秷锛屼笉杩涜浠讳綍鎿嶄綔
           console.log('鐢ㄦ埛鍙栨秷浜嗛」鐩�夋嫨');
+          // 鎭㈠涔嬪墠鐨勯�夋嫨鐘舵��
+          this.setData({
+            tempSelectedProject: this.data.selectedProject
+          });
         }
       }
     });
diff --git a/pages/home/home.wxml b/pages/home/home.wxml
index 97715ff..611bf0d 100644
--- a/pages/home/home.wxml
+++ b/pages/home/home.wxml
@@ -106,8 +106,8 @@
       <view class="project-modal-body">
         <scroll-view scroll-y class="project-scroll-view">
           <radio-group class="project-radio-group" bindchange="onProjectChange">
-            <label wx:for="{{projectList}}" wx:key="index" class="project-radio {{selectedProject === item.key ? 'project-radio-selected' : ''}}">
-              <radio value="{{item.key}}" checked="{{selectedProject === item.key}}" color="#1890FF" />
+            <label wx:for="{{projectList}}" wx:key="index" class="project-radio {{(tempSelectedProject || selectedProject) === item.key ? 'project-radio-selected' : ''}}">
+              <radio value="{{item.key}}" checked="{{(tempSelectedProject || selectedProject) === item.key}}" color="#1890FF" />
               <text>{{item.displayName}}</text>
             </label>
           </radio-group>
diff --git a/pages/login/login.js b/pages/login/login.js
index 090adb9..f5c4db2 100644
--- a/pages/login/login.js
+++ b/pages/login/login.js
@@ -287,11 +287,13 @@
         console.log('妫�娴嬪埌鏈変笂涓�椤碉紝浣跨敤navigateBack杩斿洖');
         wx.navigateBack({
           delta: 1,
-          success: () => console.log('navigateBack鎴愬姛'),
-          fail: (err) => {
-            console.error('navigateBack澶辫触:', err);
-            // 濡傛灉navigateBack澶辫触锛屽皾璇時eLaunch
-            this.backupReturnToHome();
+          success: () => {
+            // 閫氳繃eventChannel鍚戜笂涓�涓〉闈紶閫掓暟鎹�
+            const eventChannel = pages[pages.length - 2].getOpenerEventChannel();
+            if (eventChannel && eventChannel.emit) {
+              eventChannel.emit('fromLogin', { fromLogin: true });
+            }
+            console.log('宸叉垚鍔熻繑鍥炲埌涓婁竴椤甸潰');
           }
         });
       } else {
@@ -438,7 +440,11 @@
             // 淇濆瓨userData淇℃伅锛屽寘鍚玸essionId鍜宼ag
             const userData = JSON.stringify({
               sessionId: String(data.content.sessionId),
-              tag: tag
+              tag: this.data.selectedProject === 'JYG' ? 'ym' : (this.data.selectedProject === 'MQ' ? 'mq' : 'ym'),
+              project: this.data.selectedProject,
+              phone: this.data.phone,
+              clientName: data.content.clientName || '',
+              clientId: String(data.content.clientId)
             });
             return storage.setItem("userData", userData);
           })
@@ -488,12 +494,54 @@
       success: () => {
         // 寤惰繜璺宠浆锛岀‘淇漈oast鏄剧ず瀹屾垚
         setTimeout(() => {
-          console.log('鐧诲綍鎴愬姛锛屽噯澶囪烦杞埌棣栭〉');
-          // 璺宠浆鍒伴椤碉紝浣跨敤reLaunch鑰屼笉鏄痳edirectTo
-          wx.redirectTo({
-            url: '/pages/home/home'
-          });
-        });
+          console.log('鐧诲綍鎴愬姛锛屽噯澶囪繑鍥炲埌棣栭〉');
+          
+          // 鑾峰彇褰撳墠椤甸潰鏍�
+          const pages = getCurrentPages();
+          
+          // 妫�鏌ユ槸鍚︽湁涓婁竴涓〉闈㈠彲杩斿洖
+          if (pages.length > 1) {
+            // 搴旂敤涓�涓妧宸э紝鍏堣缃墠涓�涓〉闈㈢殑鍙傛暟
+            try {
+              if (pages.length > 1) {
+                const prevPage = pages[pages.length - 2];
+                if (prevPage && prevPage.options) {
+                  // 璁剧疆 fromLogin 鍙傛暟
+                  prevPage.options.fromLogin = 'true';
+                  
+                  // 濡傛灉椤甸潰鏈塷nLoad鏂规硶锛屽垯鍙兘闇�瑕佷紶閫掑弬鏁�
+                  if (typeof prevPage.onLoad === 'function') {
+                    let newOptions = {...prevPage.options};
+                    newOptions.fromLogin = 'true';
+                    prevPage.onLoad(newOptions);
+                  }
+                }
+              }
+            } catch (e) {
+              console.error('璁剧疆鍓嶄竴椤靛弬鏁板け璐�:', e);
+            }
+            
+            // 杩斿洖鍒颁笂涓�涓〉闈�
+            wx.navigateBack({
+              delta: 1,
+              success: () => {
+                console.log('宸叉垚鍔熻繑鍥炲埌涓婁竴椤甸潰');
+              },
+              fail: (err) => {
+                console.error('navigateBack澶辫触:', err);
+                // 濡傛灉杩斿洖澶辫触锛屼娇鐢ㄩ噸瀹氬悜
+                wx.redirectTo({
+                  url: '/pages/home/home?fromLogin=true'
+                });
+              }
+            });
+          } else {
+            // 濡傛灉娌℃湁涓婁竴涓〉闈紝鍒欓噸鏂版墦寮�home椤甸潰
+            wx.redirectTo({
+              url: '/pages/home/home?fromLogin=true'
+            });
+          }
+        }, 1500);
       }
     });
   },

--
Gitblit v1.8.0