ソースを参照

Enhance web-view component: Implemented ensureHomeAuthQuery function to append missing authentication parameters (token, userId, mobile) when navigating to the home page. Updated home component to use '1' for fromStartup parameter and improved user data retrieval from storage.

徐福静0235668 2 ヶ月 前
コミット
02bfcbca9f
2 ファイル変更87 行追加4 行削除
  1. 63 1
      common-page/pages/web-view/index.js
  2. 24 3
      yuban/components/home/index.js

+ 63 - 1
common-page/pages/web-view/index.js

@@ -50,8 +50,9 @@ Page({
         const isHomePath = /^https?:\/\/www\.yuban\.co\/home([/?#]|$)/.test(path);
 
         // 引导页直达 H5 首页时走快速路径:
-        // 仅设置 web-view URL,避免额外参数拼接和 Parse 查询造成首开延迟。
+        // 仅补齐缺失的登录/欢迎参数,避免额外 Parse 查询造成首开延迟。
         if (isSkipAuth && isHomePath) {
+            path = this.ensureHomeAuthQuery(path);
             this.setData({ path });
             return;
         }
@@ -283,6 +284,67 @@ Page({
 
     },
 
+    /**
+     * 直达 /home 时补齐 token/userId/mobile/fromStartup,不覆盖 URL 已有值。
+     */
+    ensureHomeAuthQuery: function (path) {
+        try {
+            const hasQuery = path.indexOf('?') !== -1;
+            const existingParams = new Set();
+            if (hasQuery) {
+                const queryString = path.split('?')[1] || '';
+                queryString.split('&').forEach((param) => {
+                    const key = param.split('=')[0];
+                    if (key) existingParams.add(key);
+                });
+            }
+
+            const append = [];
+            if (!existingParams.has('fromStartup')) {
+                append.push('fromStartup=1');
+            }
+
+            let token = null;
+            let userId = null;
+            let mobile = null;
+            try {
+                const currentUser = Parse && Parse.User ? Parse.User.current() : null;
+                if (currentUser) {
+                    token = currentUser.getSessionToken() || null;
+                    userId = currentUser.id || null;
+                    mobile = currentUser.get('mobile') || null;
+                }
+            } catch (e) {
+                // ignore
+            }
+            try {
+                if (!token) token = wx.getStorageSync('sessionToken') || null;
+                if (!userId) userId = wx.getStorageSync('userLogin') || null;
+                if (!mobile) {
+                    const userInfo = wx.getStorageSync('userInfo') || {};
+                    mobile = userInfo.mobile || null;
+                }
+            } catch (e) {
+                // ignore
+            }
+
+            if (token && !existingParams.has('token')) {
+                append.push('token=' + encodeURIComponent(token));
+            }
+            if (userId && !existingParams.has('userId')) {
+                append.push('userId=' + encodeURIComponent(userId));
+            }
+            if (mobile && !existingParams.has('mobile')) {
+                append.push('mobile=' + encodeURIComponent(mobile));
+            }
+
+            if (append.length === 0) return path;
+            return path + (hasQuery ? '&' : '?') + append.join('&');
+        } catch (e) {
+            return path;
+        }
+    },
+
     /**
      * 处理来自 H5 页面的消息
      */

+ 24 - 3
yuban/components/home/index.js

@@ -1,7 +1,6 @@
 const H5_BASE = 'https://www.yuban.co/home';
 const STARTUP_SOURCE_PARAMS = {
-  fromStartup: 'true',
-  startupWelcome: 'true',
+  fromStartup: '1',
   source: 'wechat-miniprogram',
 };
 
@@ -68,18 +67,40 @@ Component({
 
     buildWebViewPath() {
       let token = null;
+      let userId = null;
+      let mobile = null;
       try {
         const app = getApp();
         if (app && app.Parse) {
           const currentUser = app.Parse.User.current();
-          token = currentUser ? currentUser.getSessionToken() : null;
+          if (currentUser) {
+            token = currentUser.getSessionToken() || null;
+            userId = currentUser.id || null;
+            mobile = currentUser.get('mobile') || null;
+          }
         }
       } catch (e) {
         // Parse 不可用时直接游客模式进入 H5
       }
+
+      // 兜底:storage 里可能已有登录态
+      try {
+        if (!token) token = wx.getStorageSync('sessionToken') || null;
+        if (!userId) userId = wx.getStorageSync('userLogin') || null;
+        if (!mobile) {
+          const userInfo = wx.getStorageSync('userInfo') || {};
+          mobile = userInfo.mobile || null;
+        }
+      } catch (e) {
+        // ignore
+      }
+
+      // 约定:直达 /home,勿进 /startup;fromStartup=1 触发 H5 欢迎语桥接
       const h5Url = this.appendQuery(H5_BASE, {
         ...STARTUP_SOURCE_PARAMS,
         token,
+        userId,
+        mobile,
       });
       const encodedUrl = encodeURIComponent(h5Url);
       return `/common-page/pages/web-view/index?path=${encodedUrl}&skipAuth=true`;