|
|
|
|
// 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: "", // 暂时没用到,可以用 currentUrl 替代更清晰
|
|
|
|
|
currentShareUrl: "", // 【新增】用于存储 H5 传回来的最新 URL
|
|
|
|
|
currentShareImg: "", // 【新增】用于存储 H5 传回来的分享图片
|
|
|
|
|
|
|
|
|
|
cachedOptions: null,
|
|
|
|
|
isWaitingLogin: false,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onLoad: function (options) {
|
|
|
|
|
console.log('onLoad options:', options);
|
|
|
|
|
this.data.cachedOptions = options;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onShow: function () {
|
|
|
|
|
this.checkLoginAndLoad();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
checkLoginAndLoad: function() {
|
|
|
|
|
// ... (你的原有逻辑保持不变) ...
|
|
|
|
|
let options = this.data.cachedOptions;
|
|
|
|
|
if (!options || !options.weburl) {
|
|
|
|
|
setTimeout(() => { wx.navigateBack(); }, 100);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let token = wx.getStorageSync('jstrip_token');
|
|
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
|
if (this.data.isWaitingLogin || !this.data.webUrl) {
|
|
|
|
|
console.log('已登录,开始加载页面');
|
|
|
|
|
this.data.isWaitingLogin = false;
|
|
|
|
|
this.handleUrlProcess();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.data.isWaitingLogin) {
|
|
|
|
|
console.log('用户取消登录,返回上一页');
|
|
|
|
|
wx.navigateBack();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('未登录,延时跳转登录页');
|
|
|
|
|
this.data.isWaitingLogin = true;
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: '/pages/login/index',
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
console.error('跳转失败:', err);
|
|
|
|
|
this.data.isWaitingLogin = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, 300);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
let token = wx.getStorageSync('jstrip_token');
|
|
|
|
|
|
|
|
|
|
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) })
|
|
|
|
|
}
|
|
|
|
|
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) })
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
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 })
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 1. 【修改】处理 H5 传来的消息
|
|
|
|
|
handleMessage(e) {
|
|
|
|
|
console.log('接收到 H5 消息:', e.detail);
|
|
|
|
|
if (e.detail && e.detail.data) {
|
|
|
|
|
const dataArr = e.detail.data;
|
|
|
|
|
// 取最后一条消息(通常是最新的状态)
|
|
|
|
|
const lastMsg = dataArr[dataArr.length - 1];
|
|
|
|
|
|
|
|
|
|
if (lastMsg.type === 'share') {
|
|
|
|
|
this.setData({
|
|
|
|
|
shareTitle: lastMsg.title || this.data.shareTitle,
|
|
|
|
|
currentShareUrl: lastMsg.url,
|
|
|
|
|
currentShareImg: lastMsg.imgUrl || ""
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loginByCode() {},
|
|
|
|
|
|
|
|
|
|
// 2. 【修改】分享给好友
|
|
|
|
|
onShareAppMessage: function (options) {
|
|
|
|
|
// 优先使用 H5 传回来的 URL,其次使用 webViewUrl (微信自动捕获的),最后兜底使用初始 webUrl
|
|
|
|
|
let shareLink = this.data.currentShareUrl || options.webViewUrl || this.data.webUrl;
|
|
|
|
|
|
|
|
|
|
console.log('分享好友 URL:', shareLink);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: this.data.shareTitle,
|
|
|
|
|
path: `/pages/pbService/web/index?weburl=${encodeURIComponent(shareLink)}`,
|
|
|
|
|
imageUrl: this.data.currentShareImg || "" // 如果 H5 传了封面图就用,没有就默认截图
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 3. 【修改】分享到朋友圈
|
|
|
|
|
onShareTimeline: function () {
|
|
|
|
|
let shareLink = this.data.currentShareUrl || this.data.webUrl;
|
|
|
|
|
console.log('分享朋友圈 URL:', shareLink);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: this.data.shareTitle,
|
|
|
|
|
query: `weburl=${encodeURIComponent(shareLink)}`,
|
|
|
|
|
imageUrl: this.data.currentShareImg || ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|