Files
JayuCloud/WeChatProgram/admin/utils/navigation.js
T

227 lines
6.7 KiB
JavaScript

/**
* 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')
const ADMIN_PAGE_PREFIX = '/admin/pages/'
const ADMIN_TAB_PAGES = TAB_PAGES
/**
* 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}${String(path).includes('?') ? '&' : '?'}${query}` : path
}
function splitUrl(value) {
const target = String(value || '')
const queryIndex = target.indexOf('?')
if (queryIndex < 0) {
return {
path: target,
query: ''
}
}
return {
path: target.slice(0, queryIndex),
query: target.slice(queryIndex)
}
}
function toAdminPagePath(value) {
const { path, query } = splitUrl(value)
if (path.startsWith(ADMIN_PAGE_PREFIX)) {
return `${path}${query}`
}
if (path.startsWith('/pages/')) {
return `/admin${path}${query}`
}
return `${path}${query}`
}
/**
* 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 '/admin/pages/workbench/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(toAdminPagePath(path) || '').split('?')[0]
return ADMIN_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: toAdminPagePath(buildUrl(path, params)),
success: resolve,
fail: reject
})
})
}
/**
* Purpose: redirect supports navigation.js by holding or executing the redirect 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 redirect(path, params) {
return new Promise((resolve, reject) => {
wx.redirectTo({
url: toAdminPagePath(buildUrl(path, params)),
success: resolve,
fail: reject
})
})
}
/**
* Purpose: switchTab keeps the old admin-tab API while using page redirects in the unified mini-program.
* Usage: call it for admin workbench/orders/manage/profile root pages; these pages are no longer native tabBar entries.
*/
function switchTab(path) {
const target = toAdminPagePath(path)
return new Promise((resolve, reject) => {
wx.redirectTo({
url: target,
success: resolve,
fail(error) {
wx.reLaunch({
url: target,
success: resolve,
fail: reject
})
}
})
})
}
/**
* 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
}
const target = toAdminPagePath(fallbackPath || '/pages/workbench/index')
if (isTabPage(target)) {
switchTab(target)
return
}
wx.redirectTo({ url: target })
}
/**
* 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(redirectValue) {
const target = redirectValue ? toAdminPagePath(decodeRedirectValue(redirectValue)) : ''
if (!target) {
return switchTab('/pages/workbench/index')
}
const purePath = target.split('?')[0]
if (isTabPage(target)) {
return switchTab(purePath)
}
return redirect(target)
}
/**
* Purpose: openLogin supports navigation.js by holding or executing the open 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 openLogin(redirectPath) {
return navigate('/pages/login/index', {
redirect: redirectPath ? toAdminPagePath(redirectPath) : getCurrentPageUrl()
})
}
/**
* 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
}
wx.showToast({
title: `${actionText || '继续操作'}前请先登录`,
icon: 'none'
})
setTimeout(() => {
openLogin()
}, 160)
return false
}
module.exports = {
ADMIN_TAB_PAGES,
buildUrl,
getCurrentPageUrl,
isTabPage,
toAdminPagePath,
navigate,
redirect,
switchTab,
goBack,
redirectAfterLogin,
openLogin,
ensureLogin
}