/** * Purpose: auth supports navigation.js by holding or executing the auth page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const auth = require('./auth') const { TAB_PAGES } = require('./config') /** * Purpose: waitingForAutoLoginNavigation supports navigation.js by holding or executing the waiting for auto login navigation page or API role. * Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow. */ let waitingForAutoLoginNavigation = false /** * Purpose: buildUrl supports navigation.js by holding or executing the build url page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function buildUrl(path, params) { const query = Object.keys(params || {}) .filter(key => params[key] !== undefined && params[key] !== null && params[key] !== '') .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) .join('&') return query ? `${path}?${query}` : path } /** * Purpose: getCurrentPageUrl supports navigation.js by holding or executing the get current page url page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function getCurrentPageUrl() { const pages = getCurrentPages() const current = pages[pages.length - 1] if (!current) { return '/pages/home/index' } return buildUrl(`/${current.route}`, current.options || {}) } /** * Purpose: isTabPage supports navigation.js by holding or executing the is tab page page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function isTabPage(path) { const purePath = String(path || '').split('?')[0] return TAB_PAGES.includes(purePath) } /** * Purpose: navigate supports navigation.js by holding or executing the navigate page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function navigate(path, params) { return new Promise((resolve, reject) => { wx.navigateTo({ url: buildUrl(path, params), success: resolve, fail: reject }) }) } /** * Purpose: switchTab supports navigation.js by holding or executing the switch tab page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function switchTab(path) { return new Promise((resolve, reject) => { wx.switchTab({ url: path, success: resolve, fail: reject }) }) } /** * Purpose: normalizeActionPath supports navigation.js by holding or executing the normalize action path page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function normalizeActionPath(value) { const rawPath = String(value || '').trim() if (!rawPath || !rawPath.startsWith('/') || rawPath.startsWith('//')) { return '' } if (rawPath.startsWith('/orders')) { const queryIndex = rawPath.indexOf('?') const query = queryIndex >= 0 ? rawPath.slice(queryIndex) : '' return `/pages/orders/index${query}` } if (rawPath.startsWith('/product/')) { const id = rawPath.split('/')[2] || '' return buildUrl('/pages/product-detail/index', { id }) } if (rawPath.startsWith('/shop/')) { const id = rawPath.split('/')[2] || '' return buildUrl('/pages/shop-detail/index', { id }) } if (rawPath === '/coupons' || rawPath.startsWith('/coupons?')) { return rawPath.replace('/coupons', '/pages/coupons/index') } if (rawPath === '/vip-center' || rawPath.startsWith('/vip-center?')) { return rawPath.replace('/vip-center', '/pages/vip-center/index') } if (rawPath === '/cart' || rawPath.startsWith('/cart?')) { return rawPath.replace('/cart', '/pages/cart/index') } if (rawPath.startsWith('/pages/')) { return rawPath } return '' } /** * Purpose: buildMessageActionPath converts backend system-message business fields into a mini-program route. * Usage: call it when actionPath is absent so review and artist-change popup messages still land on orders. */ function buildMessageActionPath(message) { if (!message) { return '' } const actionPath = normalizeActionPath(message.actionPath) if (actionPath) { return actionPath } const businessType = String(message.businessType || '').toUpperCase() const businessId = message.businessId || '' if (businessType === 'ORDER_ARTIST_REVIEW' || businessType === 'ORDER_ARTIST_CHANGE' || businessType === 'ORDER') { return buildUrl('/pages/orders/index', businessId ? { orderId: businessId } : {}) } if (businessType === 'PRODUCT') { return buildUrl('/pages/product-detail/index', businessId ? { id: businessId } : {}) } if (businessType === 'SHOP') { return buildUrl('/pages/shop-detail/index', businessId ? { id: businessId } : {}) } if (businessType === 'VIP') { return '/pages/vip-center/index' } if (businessType === 'COUPON') { return '/pages/coupons/index' } if (businessType === 'CART') { return '/pages/cart/index' } return '' } /** * Purpose: openActionPath supports navigation.js by holding or executing the open action path page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function openActionPath(value) { const target = normalizeActionPath(value) if (!target) { return Promise.resolve(false) } const purePath = target.split('?')[0] const openPromise = isTabPage(purePath) ? switchTab(purePath) : navigate(target) return openPromise .then(() => true) .catch(error => { console.error('Open message action path failed', error) wx.showToast({ title: '无法打开消息链接', icon: 'none' }) return false }) } /** * Purpose: openSystemMessage opens a backend system-message action using actionPath first and business fields as fallback. * Usage: pass a full system-message record from popup or notification list taps. */ function openSystemMessage(message) { return openActionPath(buildMessageActionPath(message)) } /** * Purpose: goBack supports navigation.js by holding or executing the go back page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function goBack(fallbackPath) { const pages = getCurrentPages() if (pages.length > 1) { wx.navigateBack() return } if (isTabPage(fallbackPath || '/pages/home/index')) { wx.switchTab({ url: fallbackPath || '/pages/home/index' }) return } wx.redirectTo({ url: fallbackPath || '/pages/home/index' }) } /** * Purpose: parseQuery supports navigation.js by holding or executing the parse query page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function parseQuery(url) { const queryString = String(url || '').split('?')[1] || '' if (!queryString) { return {} } return queryString.split('&').reduce((result, item) => { const parts = item.split('=') const key = decodeURIComponent(parts[0] || '') const value = decodeURIComponent(parts[1] || '') if (key) { result[key] = value } return result }, {}) } /** * Purpose: decodeRedirectValue supports navigation.js by holding or executing the decode redirect value page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function decodeRedirectValue(value) { let current = String(value || '') for (let index = 0; index < 3; index += 1) { try { const decoded = decodeURIComponent(current) if (decoded === current) { return decoded } current = decoded } catch (error) { return current } } return current } /** * Purpose: redirectAfterLogin supports navigation.js by holding or executing the redirect after login page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function redirectAfterLogin(redirect) { const target = redirect ? decodeRedirectValue(redirect) : '' if (!target) { return switchTab('/pages/home/index') } const purePath = target.split('?')[0] if (purePath === '/pages/products/index') { const app = getApp() app.globalData.pendingProductFilter = parseQuery(target) return switchTab('/pages/products/index') } if (isTabPage(target)) { return switchTab(purePath) } return new Promise((resolve, reject) => { wx.redirectTo({ url: target, success: resolve, fail: reject }) }) } /** * Purpose: openAuth supports navigation.js by holding or executing the open auth page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function openAuth(redirect) { return navigate('/pages/auth/index', { redirect: redirect || getCurrentPageUrl() }) } /** * Purpose: showLoginToast supports navigation.js by holding or executing the show login toast page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function showLoginToast(actionText) { wx.showToast({ title: `${actionText || '继续操作'}前请先登录`, icon: 'none' }) } /** * Purpose: scheduleOpenAuth supports navigation.js by holding or executing the schedule open auth page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function scheduleOpenAuth(actionText) { showLoginToast(actionText) setTimeout(() => { openAuth() }, 120) } /** * Purpose: ensureLogin supports navigation.js by holding or executing the ensure login page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function ensureLogin(actionText) { if (auth.isLoggedIn()) { return true } const app = getApp() const hasAutoLoginHelpers = app && typeof app.isWechatMiniAutoLoginPending === 'function' && typeof app.waitForWechatMiniAutoLogin === 'function' if (hasAutoLoginHelpers && app.isWechatMiniAutoLoginPending()) { if (waitingForAutoLoginNavigation) { return false } waitingForAutoLoginNavigation = true Promise.resolve(app.waitForWechatMiniAutoLogin()) .catch(() => false) .then(() => { if (!auth.isLoggedIn()) { scheduleOpenAuth(actionText) } }) .finally(() => { waitingForAutoLoginNavigation = false }) return false } scheduleOpenAuth(actionText) return false } module.exports = { buildUrl, getCurrentPageUrl, isTabPage, navigate, switchTab, normalizeActionPath, buildMessageActionPath, openActionPath, openSystemMessage, goBack, redirectAfterLogin, openAuth, ensureLogin, /** * Purpose: openProducts supports navigation.js by holding or executing the open products page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ openProducts(filter) { const app = getApp() app.globalData.pendingProductFilter = filter || {} return switchTab('/pages/products/index') } }