|
@@ -1,4 +1,5 @@
|
|
|
// common-page/pages/web-view/index.js
|
|
// common-page/pages/web-view/index.js
|
|
|
|
|
+const { requestedTarget, withQuery } = require('../../../utils/web-view-target');
|
|
|
const Parse = getApp().Parse;
|
|
const Parse = getApp().Parse;
|
|
|
const company = getApp().globalData.company;
|
|
const company = getApp().globalData.company;
|
|
|
const DEBUG_WEBVIEW = false;
|
|
const DEBUG_WEBVIEW = false;
|
|
@@ -36,91 +37,36 @@ Page({
|
|
|
onLoad: async function (options) {
|
|
onLoad: async function (options) {
|
|
|
debugLog('======= web-view 页面加载 =======');
|
|
debugLog('======= web-view 页面加载 =======');
|
|
|
|
|
|
|
|
- // 1. 先检查用户登录状态
|
|
|
|
|
- const loginCheck = await this.checkUserLogin(options);
|
|
|
|
|
- if (!loginCheck) {
|
|
|
|
|
- debugLog('⚠️ 用户未登录或没有手机号,已跳转到授权页面');
|
|
|
|
|
- return; // 停止后续加载
|
|
|
|
|
- }
|
|
|
|
|
- debugLog('✅ 用户已登录且有手机号,继续加载 web-view');
|
|
|
|
|
-
|
|
|
|
|
- // 2. 解码 URL
|
|
|
|
|
- let path = decodeURIComponent(options.path || '');
|
|
|
|
|
- const isSkipAuth = options.skipAuth === 'true';
|
|
|
|
|
- const isHomePath = /^https?:\/\/www\.yuban\.co\/home([/?#]|$)/.test(path);
|
|
|
|
|
-
|
|
|
|
|
- // 引导页直达 H5 首页时走快速路径:
|
|
|
|
|
- // 仅补齐缺失的登录/欢迎参数,避免额外 Parse 查询造成首开延迟。
|
|
|
|
|
- if (isSkipAuth && isHomePath) {
|
|
|
|
|
- path = this.ensureHomeAuthQuery(path);
|
|
|
|
|
- this.setData({ path });
|
|
|
|
|
|
|
+ const destination = requestedTarget(options);
|
|
|
|
|
+ if (!destination) {
|
|
|
|
|
+ wx.showToast({title:'链接不可用',icon:'none'});
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- debugLog('原始 options.path:', options.path);
|
|
|
|
|
- debugLog('解码后的 path:', path);
|
|
|
|
|
-
|
|
|
|
|
- // 拼接额外参数(避免重复添加已存在的参数)
|
|
|
|
|
- let hasQuery = path.indexOf('?') !== -1;
|
|
|
|
|
- let parsm = hasQuery ? '&' : '?';
|
|
|
|
|
- let params = [];
|
|
|
|
|
-
|
|
|
|
|
- // 提取 path 中已有的参数
|
|
|
|
|
- let existingParams = new Set();
|
|
|
|
|
- if (hasQuery) {
|
|
|
|
|
- const queryString = path.split('?')[1];
|
|
|
|
|
- if (queryString) {
|
|
|
|
|
- queryString.split('&').forEach(param => {
|
|
|
|
|
- const key = param.split('=')[0];
|
|
|
|
|
- existingParams.add(key);
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 只添加 path 中不存在的参数
|
|
|
|
|
- for (const key in options) {
|
|
|
|
|
- if(key != 'path' && key != 'url' && !existingParams.has(key)){
|
|
|
|
|
- params.push(key + '=' + options[key]);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 添加用户信息,确保 H5 能获取到当前用户身份(用于扣减流量等操作)
|
|
|
|
|
- const currentUser = Parse.User.current();
|
|
|
|
|
- if (currentUser) {
|
|
|
|
|
- // 添加 userId
|
|
|
|
|
- if (!existingParams.has('userId')) {
|
|
|
|
|
- params.push('userId=' + currentUser.id);
|
|
|
|
|
- }
|
|
|
|
|
- // 添加 token (sessionToken)
|
|
|
|
|
- const sessionToken = currentUser.getSessionToken();
|
|
|
|
|
- if (sessionToken && !existingParams.has('token')) {
|
|
|
|
|
- params.push('token=' + sessionToken);
|
|
|
|
|
- }
|
|
|
|
|
- // 添加 mobile
|
|
|
|
|
- const mobile = currentUser.get('mobile');
|
|
|
|
|
- if (mobile && !existingParams.has('mobile')) {
|
|
|
|
|
- params.push('mobile=' + mobile);
|
|
|
|
|
|
|
+ const isHome = /^https:\/\/www\.yuban\.co\/home(?:[/?#]|$)/.test(destination);
|
|
|
|
|
+ const homeStartup = isHome && options.skipAuth === 'true';
|
|
|
|
|
+ if (!homeStartup && !await this.checkUserLogin(options)) return;
|
|
|
|
|
+
|
|
|
|
|
+ let user = Parse.User.current();
|
|
|
|
|
+ if (homeStartup && !user) {
|
|
|
|
|
+ const saved = wx.getStorageSync('sessionToken');
|
|
|
|
|
+ if (saved) {
|
|
|
|
|
+ try { user = await Parse.User.become(saved); }
|
|
|
|
|
+ catch (_) { wx.removeStorageSync('sessionToken'); wx.removeStorageSync('userLogin'); }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- if(params.length > 0) {
|
|
|
|
|
- parsm = parsm + params.join('&');
|
|
|
|
|
- path = path + parsm;
|
|
|
|
|
|
|
+ const params = homeStartup ? {fromStartup:'1',source:'wechat-miniprogram'} : {};
|
|
|
|
|
+ if (user) {
|
|
|
|
|
+ params.token = user.getSessionToken();
|
|
|
|
|
+ params.userId = user.id;
|
|
|
|
|
+ params.mobile = user.get('mobile');
|
|
|
}
|
|
}
|
|
|
|
|
+ this.setData({ path: withQuery(destination, params) });
|
|
|
|
|
+ if (homeStartup) return;
|
|
|
|
|
|
|
|
- debugLog('最终 web-view URL:', path);
|
|
|
|
|
- debugLog('URL 长度:', path.length);
|
|
|
|
|
-
|
|
|
|
|
- this.setData({
|
|
|
|
|
- path: path
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- // 立即设置标题
|
|
|
|
|
const passedStoreName = options.storeName ? decodeURIComponent(options.storeName) : '';
|
|
const passedStoreName = options.storeName ? decodeURIComponent(options.storeName) : '';
|
|
|
const passedStoreId = options.storeId || '';
|
|
const passedStoreId = options.storeId || '';
|
|
|
// /home 场景下默认不需要门店标题:跳过 ShopStore 的 Parse 查询以提速。
|
|
// /home 场景下默认不需要门店标题:跳过 ShopStore 的 Parse 查询以提速。
|
|
|
// 这里根据 H5 path 判断:例如 https://www.yuban.co/home?token=...&skipAuth=true...
|
|
// 这里根据 H5 path 判断:例如 https://www.yuban.co/home?token=...&skipAuth=true...
|
|
|
- const isHome = isHomePath;
|
|
|
|
|
|
|
|
|
|
if (passedStoreName) {
|
|
if (passedStoreName) {
|
|
|
this.setNavigationTitle(passedStoreName);
|
|
this.setNavigationTitle(passedStoreName);
|
|
@@ -144,12 +90,6 @@ Page({
|
|
|
try {
|
|
try {
|
|
|
debugLog('🔍 开始检查用户登录状态...');
|
|
debugLog('🔍 开始检查用户登录状态...');
|
|
|
|
|
|
|
|
- // 检查是否有 skipAuth 参数(用于跳过登录检查)
|
|
|
|
|
- if (options.skipAuth === 'true') {
|
|
|
|
|
- debugLog('ℹ️ 检测到 skipAuth 参数,跳过登录检查');
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
// 检查是否是扫码进入
|
|
// 检查是否是扫码进入
|
|
|
const isScanEntry = options && (options.storeId && (options.scanCount || options.partnerId || options.userId || options.employeeId));
|
|
const isScanEntry = options && (options.storeId && (options.scanCount || options.partnerId || options.userId || options.employeeId));
|
|
|
if (isScanEntry) {
|
|
if (isScanEntry) {
|
|
@@ -189,11 +129,8 @@ Page({
|
|
|
debugLog('⚠️ 用户未完整登录,准备跳转到授权页面');
|
|
debugLog('⚠️ 用户未完整登录,准备跳转到授权页面');
|
|
|
|
|
|
|
|
// 构建返回 URL(登录成功后返回当前页面)
|
|
// 构建返回 URL(登录成功后返回当前页面)
|
|
|
- const currentPath = options.path || '';
|
|
|
|
|
- const returnUrl = encodeURIComponent(currentPath);
|
|
|
|
|
-
|
|
|
|
|
- debugLog('🔗 returnUrl:', returnUrl);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ const returnUrl = encodeURIComponent(requestedTarget(options));
|
|
|
|
|
+
|
|
|
// 跳转到授权页面,并传递 returnUrl
|
|
// 跳转到授权页面,并传递 returnUrl
|
|
|
wx.redirectTo({
|
|
wx.redirectTo({
|
|
|
url: `/components/app-auth/index?returnUrl=${returnUrl}`,
|
|
url: `/components/app-auth/index?returnUrl=${returnUrl}`,
|
|
@@ -234,7 +171,7 @@ Page({
|
|
|
debugLog('checkUserLogin error');
|
|
debugLog('checkUserLogin error');
|
|
|
|
|
|
|
|
// 出错时也跳转到授权页面
|
|
// 出错时也跳转到授权页面
|
|
|
- const returnUrl = options.path ? encodeURIComponent(options.path) : '';
|
|
|
|
|
|
|
+ const returnUrl = encodeURIComponent(requestedTarget(options));
|
|
|
wx.redirectTo({
|
|
wx.redirectTo({
|
|
|
url: `/components/app-auth/index?returnUrl=${returnUrl}`,
|
|
url: `/components/app-auth/index?returnUrl=${returnUrl}`,
|
|
|
fail: () => {
|
|
fail: () => {
|
|
@@ -284,70 +221,6 @@ 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');
|
|
|
|
|
- }
|
|
|
|
|
- if (!existingParams.has('source')) {
|
|
|
|
|
- append.push('source=wechat-miniprogram');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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 页面的消息
|
|
* 处理来自 H5 页面的消息
|
|
|
*/
|
|
*/
|