195 lines
5.8 KiB
JavaScript
195 lines
5.8 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')
|
|
|
|
/**
|
|
* 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/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(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: 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: 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: 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 = fallbackPath || '/pages/workbench/index'
|
|
if (isTabPage(target)) {
|
|
wx.switchTab({ url: 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 ? decodeRedirectValue(redirectValue) : ''
|
|
if (!target) {
|
|
return switchTab('/pages/workbench/index')
|
|
}
|
|
|
|
const purePath = target.split('?')[0]
|
|
if (isTabPage(target)) {
|
|
return switchTab(purePath)
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
wx.redirectTo({
|
|
url: target,
|
|
success: resolve,
|
|
fail: reject
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 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 || 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 = {
|
|
buildUrl,
|
|
getCurrentPageUrl,
|
|
isTabPage,
|
|
navigate,
|
|
redirect,
|
|
switchTab,
|
|
goBack,
|
|
redirectAfterLogin,
|
|
openLogin,
|
|
ensureLogin
|
|
}
|