2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
const auth = require('./auth')
|
|
|
|
|
const { TAB_PAGES } = require('./config')
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-02 13:20:28 +08:00
|
|
|
let waitingForAutoLoginNavigation = false
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
function getCurrentPageUrl() {
|
|
|
|
|
const pages = getCurrentPages()
|
|
|
|
|
const current = pages[pages.length - 1]
|
|
|
|
|
if (!current) {
|
|
|
|
|
return '/pages/home/index'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buildUrl(`/${current.route}`, current.options || {})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
function isTabPage(path) {
|
|
|
|
|
const purePath = String(path || '').split('?')[0]
|
|
|
|
|
return TAB_PAGES.includes(purePath)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
function navigate(path, params) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: buildUrl(path, params),
|
|
|
|
|
success: resolve,
|
|
|
|
|
fail: reject
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
function switchTab(path) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
wx.switchTab({
|
|
|
|
|
url: path,
|
|
|
|
|
success: resolve,
|
|
|
|
|
fail: reject
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-05-23 15:16:40 +08:00
|
|
|
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}`
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
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')
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 15:16:40 +08:00
|
|
|
if (rawPath.startsWith('/pages/')) {
|
|
|
|
|
return rawPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-05-23 15:16:40 +08:00
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
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'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
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
|
|
|
|
|
}, {})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
function openAuth(redirect) {
|
|
|
|
|
return navigate('/pages/auth/index', {
|
|
|
|
|
redirect: redirect || getCurrentPageUrl()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-02 13:20:28 +08:00
|
|
|
function showLoginToast(actionText) {
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: `${actionText || '继续操作'}前请先登录`,
|
|
|
|
|
icon: 'none'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-02 13:20:28 +08:00
|
|
|
function scheduleOpenAuth(actionText) {
|
|
|
|
|
showLoginToast(actionText)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
openAuth()
|
|
|
|
|
}, 120)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-01 15:18:35 +08:00
|
|
|
function ensureLogin(actionText) {
|
|
|
|
|
if (auth.isLoggedIn()) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const app = getApp()
|
2026-04-02 13:20:28 +08:00
|
|
|
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
|
2026-04-01 15:18:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
buildUrl,
|
|
|
|
|
getCurrentPageUrl,
|
|
|
|
|
isTabPage,
|
|
|
|
|
navigate,
|
|
|
|
|
switchTab,
|
2026-05-23 15:16:40 +08:00
|
|
|
normalizeActionPath,
|
2026-05-23 18:45:41 +08:00
|
|
|
buildMessageActionPath,
|
2026-05-23 15:16:40 +08:00
|
|
|
openActionPath,
|
2026-05-23 18:45:41 +08:00
|
|
|
openSystemMessage,
|
2026-04-01 15:18:35 +08:00
|
|
|
goBack,
|
|
|
|
|
redirectAfterLogin,
|
|
|
|
|
openAuth,
|
|
|
|
|
ensureLogin,
|
2026-05-23 18:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-04-02 13:20:28 +08:00
|
|
|
openProducts(filter) {
|
|
|
|
|
const app = getApp()
|
|
|
|
|
app.globalData.pendingProductFilter = filter || {}
|
|
|
|
|
return switchTab('/pages/products/index')
|
|
|
|
|
}
|
2026-04-01 15:18:35 +08:00
|
|
|
}
|