// pages/pbService/web/index.js let app = getApp() import commonApi from "../../../utils/https/common" import userApi from "../../../utils/https/user" import https from "../../../utils/https.js" Page({ data: { webUrl: "", shareTitle: "", shareUrl: "", cachedOptions: null, isWaitingLogin: false, }, onLoad: function (options) { // onLoad 只负责存参数,不执行任何跳转逻辑,防止和页面初始化冲突 console.log('onLoad options:', options); this.data.cachedOptions = options; }, onShow: function () { // 将所有逻辑移到 onShow,确保页面加载完毕后再执行跳转 this.checkLoginAndLoad(); }, checkLoginAndLoad: function() { // 1. 防白屏容错:检查参数 let options = this.data.cachedOptions; if (!options || !options.weburl) { setTimeout(() => { wx.navigateBack(); }, 100); return; } let token = wx.getStorageSync('jstrip_token'); // 2. 有 Token:正常加载 if (token) { // 如果是刚登录回来,或者页面还没加载过 URL if (this.data.isWaitingLogin || !this.data.webUrl) { console.log('已登录,开始加载页面'); this.data.isWaitingLogin = false; this.handleUrlProcess(); } return; } // 3. 无 Token:处理跳转 // 3.1 如果 isWaitingLogin 为 true,说明是刚从登录页返回(用户取消了登录) if (this.data.isWaitingLogin) { console.log('用户取消登录,返回上一页'); wx.navigateBack(); return; } // 3.2 第一次检测到未登录,跳转登录页 console.log('未登录,延时跳转登录页'); this.data.isWaitingLogin = true; // ★关键修复★:使用 setTimeout 延迟跳转,解决 "navigateTo with an already exist webviewId" 报错 setTimeout(() => { wx.navigateTo({ url: '/pages/login/index', fail: (err) => { console.error('跳转失败:', err); this.data.isWaitingLogin = false; // 重置标记以便重试 } }); }, 300); // 延迟 300ms 足够让页面初始化完成 }, handleUrlProcess: function () { let options = this.data.cachedOptions; // 双重检查 if (!options || !options.weburl) return; let weburl = decodeURIComponent(options.weburl) let webParam = {} let baseUrl = "" try { baseUrl = weburl.split('?')[0] let paramStr = weburl.split('?')[1] if (paramStr) { paramStr.split('&').forEach((param) => { let parts = param.split('='); webParam[(parts[0])] = (parts[1]); }); } } catch (e) { console.log(e) } let lowerUrl = weburl.toLowerCase() // 此时 Token 肯定存在 let token = wx.getStorageSync('jstrip_token'); // 1. 宠你有礼 if (lowerUrl.indexOf('petyou2024') != -1 && webParam.cnylCode) { commonApi._get("uservice/user/loginByCnyl", { cnylCode: webParam.cnylCode }) .then(res => { if (res.data && res.data.id && res.data.token) { this.saveLoginInfo(res.data); weburl += '&token=' + res.data.token } }).finally(() => { this.setUrl(weburl) }) } // 2. 遇见昆山 else if (lowerUrl.indexOf('couponAndKs') != -1 && webParam.ksCode) { commonApi._get("uservice/user/loginBykunshan", { code: webParam.ksCode }) .then(res => { if (res.data && res.data.id && res.data.token) { this.saveLoginInfo(res.data); weburl += '&token=' + res.data.token } }).finally(() => { this.setUrl(weburl) }) } // 3. 君到苏州 else if (lowerUrl.indexOf('m.cloud.sz-trip.com') != -1) { delete webParam.token delete webParam.lon delete webParam.lat weburl = baseUrl if (Object.keys(webParam).length > 0) { const newParamsArray = []; for (let key in webParam) { newParamsArray.push(`${key}=${(webParam[key])}`); } weburl = `${baseUrl}?${newParamsArray.join('&')}`; } if (Object.keys(webParam).length > 0) { weburl += '&token=' + token } else { weburl += '?token=' + token } this.handleLocationAndSetUrl(weburl, webParam, token); } // 4. 其他 else { this.setUrl(weburl) } }, handleLocationAndSetUrl(weburl, webParam, token) { let that = this; try { let lonAndLat = wx.getStorageSync('lonAndLat') let locObj = null; if (lonAndLat) { try { locObj = JSON.parse(lonAndLat); } catch(e) {} } if (locObj && locObj.lon && locObj.lat) { weburl = that.appendLocation(weburl, locObj.lon, locObj.lat, webParam, token); that.setUrl(weburl); return; } wx.getLocation({ type: 'gcj02', success: (res) => { let newLoc = { lat: res.latitude, lon: res.longitude }; wx.setStorageSync('lonAndLat', JSON.stringify(newLoc)); weburl = that.appendLocation(weburl, newLoc.lon, newLoc.lat, webParam, token); that.setUrl(weburl); }, fail: (err) => { console.log('定位失败'); that.setUrl(weburl); } }) } catch (e) { console.error(e); that.setUrl(weburl); } }, appendLocation(url, lon, lat, webParam, token) { if (Object.keys(webParam).length > 0 || token) { return url + `&lon=${lon}&lat=${lat}`; } else { return url + `?lon=${lon}&lat=${lat}`; } }, saveLoginInfo(data) { wx.setStorageSync("jstrip_userid", data.id) wx.setStorageSync("jstrip_token", data.token) wx.setStorageSync("jstrip_userInfo", data) }, setUrl(url) { console.log('设置webUrl:', url) if (!url) return; this.setData({ webUrl: url }) }, handleMessage(data) {}, loginByCode() {}, onShareAppMessage: function () { return { title: this.data.shareTitle, path: `/pages/pbService/web/index?weburl=${encodeURIComponent(this.data.webUrl)}`, } }, onShareTimeline: function () { return { title: this.data.shareTitle, path: `/pages/pbService/web/index?weburl=${encodeURIComponent(this.data.webUrl)}`, } } })