You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.2 KiB

5 years ago
// pages/pbService/web/index.js
5 years ago
let app = getApp()
1 year ago
import commonApi from "../../../utils/https/common"
5 years ago
import userApi from "../../../utils/https/user"
1 year ago
import https from "../../../utils/https.js"
5 years ago
2 months ago
Page({
5 years ago
data: {
2 months ago
webUrl: "",
shareTitle: "",
11 months ago
shareUrl: "",
2 months ago
cachedOptions: null,
isWaitingLogin: false,
5 years ago
},
onLoad: function (options) {
2 months ago
// onLoad 只负责存参数,不执行任何跳转逻辑,防止和页面初始化冲突
console.log('onLoad options:', options);
this.data.cachedOptions = options;
},
1 year ago
2 months ago
onShow: function () {
// 将所有逻辑移到 onShow,确保页面加载完毕后再执行跳转
this.checkLoginAndLoad();
},
1 year ago
2 months ago
checkLoginAndLoad: function() {
// 1. 防白屏容错:检查参数
let options = this.data.cachedOptions;
if (!options || !options.weburl) {
setTimeout(() => { wx.navigateBack(); }, 100);
return;
1 year ago
}
8 months ago
2 months ago
let token = wx.getStorageSync('jstrip_token');
8 months ago
2 months ago
// 2. 有 Token:正常加载
if (token) {
// 如果是刚登录回来,或者页面还没加载过 URL
if (this.data.isWaitingLogin || !this.data.webUrl) {
console.log('已登录,开始加载页面');
this.data.isWaitingLogin = false;
this.handleUrlProcess();
11 months ago
}
2 months ago
return;
1 year ago
}
2 months ago
// 3. 无 Token:处理跳转
// 3.1 如果 isWaitingLogin 为 true,说明是刚从登录页返回(用户取消了登录)
if (this.data.isWaitingLogin) {
console.log('用户取消登录,返回上一页');
wx.navigateBack();
return;
}
1 year ago
2 months ago
// 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; // 重置标记以便重试
1 year ago
}
2 months ago
});
}, 300); // 延迟 300ms 足够让页面初始化完成
1 year ago
},
2 months ago
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]);
});
11 months ago
}
2 months ago
} catch (e) { console.log(e) }
11 months ago
2 months ago
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('&')}`;
}
11 months ago
2 months ago
if (Object.keys(webParam).length > 0) {
weburl += '&token=' + token
} else {
weburl += '?token=' + token
}
5 years ago
2 months ago
this.handleLocationAndSetUrl(weburl, webParam, token);
}
// 4. 其他
else {
this.setUrl(weburl)
}
5 years ago
},
2 months ago
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) {}
}
5 years ago
2 months ago
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);
}
5 years ago
},
2 months ago
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}`;
}
5 years ago
},
2 months ago
saveLoginInfo(data) {
wx.setStorageSync("jstrip_userid", data.id)
wx.setStorageSync("jstrip_token", data.token)
wx.setStorageSync("jstrip_userInfo", data)
5 years ago
},
2 months ago
setUrl(url) {
console.log('设置webUrl:', url)
if (!url) return;
this.setData({ webUrl: url })
5 years ago
},
2 months ago
handleMessage(data) {},
loginByCode() {},
5 years ago
onShareAppMessage: function () {
2 months ago
return {
title: this.data.shareTitle,
path: `/pages/pbService/web/index?weburl=${encodeURIComponent(this.data.webUrl)}`,
}
},
2 months ago
onShareTimeline: function () {
return {
title: this.data.shareTitle,
path: `/pages/pbService/web/index?weburl=${encodeURIComponent(this.data.webUrl)}`,
}
}
2 months ago
})