From de0598e73db8ee2f6d20251c588adfafeb421494 Mon Sep 17 00:00:00 2001 From: wangfukang <15630117759@163.com> Date: Wed, 24 Jun 2026 17:54:43 +0800 Subject: [PATCH] 1 --- utils/authRedirect.js | 27 +++++++++++++ utils/sharePath.js | 89 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 utils/authRedirect.js create mode 100644 utils/sharePath.js diff --git a/utils/authRedirect.js b/utils/authRedirect.js new file mode 100644 index 0000000..6b98883 --- /dev/null +++ b/utils/authRedirect.js @@ -0,0 +1,27 @@ +export function hasLogin() { + return !!uni.getStorageSync('id') +} + +export function getCurrentPagePath() { + const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : [] + const page = pages[pages.length - 1] + if (!page || !page.route) return '/pages/index/index' + const options = page.options || {} + const query = Object.keys(options) + .filter(key => options[key] !== undefined && options[key] !== null && options[key] !== '') + .map(key => key + '=' + encodeURIComponent(String(options[key]))) + .join('&') + return '/' + page.route + (query ? '?' + query : '') +} + +export function requireLoginToCurrentPage() { + if (hasLogin()) return true + const redirectKey = 'login_redirect_' + Date.now() + const targetPath = getCurrentPagePath() + uni.setStorageSync(redirectKey, targetPath) + uni.setStorageSync('authPendingTargetPath', targetPath) + uni.redirectTo({ + url: '/package2/login/login?redirect=' + redirectKey + }) + return false +} diff --git a/utils/sharePath.js b/utils/sharePath.js new file mode 100644 index 0000000..6db04ae --- /dev/null +++ b/utils/sharePath.js @@ -0,0 +1,89 @@ +export function buildHomeSharePath(targetPath) { + if (!targetPath) return '/pages/index/index' + return '/pages/index/index?targetPath=' + encodeURIComponent(targetPath) +} + +export function buildGroupHomeSharePath(params) { + params = params || {} + const query = [ + 'shareType=group', + 'type=shop', + 'shopId=' + encodeURIComponent(params.shopId || ''), + 'groupId=' + encodeURIComponent(params.groupId || ''), + 'targetMembers=' + encodeURIComponent(params.targetMembers || 2), + 'isFaceToFace=' + encodeURIComponent(params.isFaceToFace || 0) + ] + if (params.orderScene) { + query.push('orderScene=' + encodeURIComponent(params.orderScene)) + } + return '/pages/index/index?' + query.join('&') +} + +export function buildGroupTargetPath(params) { + params = params || {} + if (!params.shopId || !params.groupId) return '' + let targetPath = '/package2/group/groupBuySingle?type=shop' + + '&shopId=' + encodeURIComponent(params.shopId) + + '&groupId=' + encodeURIComponent(params.groupId) + + '&targetMembers=' + encodeURIComponent(params.targetMembers || 2) + + '&isFaceToFace=' + encodeURIComponent(params.isFaceToFace || 0) + if (params.orderScene) { + targetPath += '&orderScene=' + encodeURIComponent(params.orderScene) + } + return targetPath +} + +export function normalizeShareTargetPath(value) { + let targetPath = String(value || '') + if (targetPath.indexOf('%2F') === 0 || targetPath.indexOf('%2f') === 0) { + try { + targetPath = decodeURIComponent(targetPath) + } catch (e) {} + } + if (!targetPath || targetPath.charAt(0) !== '/') return '' + if (targetPath.indexOf('/pages/index/index') === 0) return '' + return targetPath +} + +export function resolveHomeShareTarget(query) { + query = query || {} + if (query.shareType === 'group') { + return buildGroupTargetPath(query) + } + if (query.targetPath) { + return normalizeShareTargetPath(query.targetPath) + } + return '' +} + +export function isShareEnterScene(options) { + if (!options || options.scene == null) return false + const scene = Number(options.scene) + return scene === 1007 || scene === 1008 || scene === 1044 || scene === 1154 +} + +export function getEnterShareTarget(options) { + if (!options) { + try { + if (uni.getEnterOptionsSync) { + options = uni.getEnterOptionsSync() + } else if (uni.getLaunchOptionsSync) { + options = uni.getLaunchOptionsSync() + } + } catch (e) {} + } + if (!isShareEnterScene(options)) return '' + return resolveHomeShareTarget(options && options.query) +} + +export function getCurrentEnterShareTarget() { + let options = null + try { + if (uni.getEnterOptionsSync) { + options = uni.getEnterOptionsSync() + } else if (uni.getLaunchOptionsSync) { + options = uni.getLaunchOptionsSync() + } + } catch (e) {} + return getEnterShareTarget(options) +}