功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
const auth = require('./auth')
|
||||
const { TAB_PAGES } = require('./config')
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
function getCurrentPageUrl() {
|
||||
const pages = getCurrentPages()
|
||||
const current = pages[pages.length - 1]
|
||||
if (!current) {
|
||||
return '/pages/workbench/index'
|
||||
}
|
||||
|
||||
return buildUrl(`/${current.route}`, current.options || {})
|
||||
}
|
||||
|
||||
function isTabPage(path) {
|
||||
const purePath = String(path || '').split('?')[0]
|
||||
return TAB_PAGES.includes(purePath)
|
||||
}
|
||||
|
||||
function navigate(path, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.navigateTo({
|
||||
url: buildUrl(path, params),
|
||||
success: resolve,
|
||||
fail: reject
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function redirect(path, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.redirectTo({
|
||||
url: buildUrl(path, params),
|
||||
success: resolve,
|
||||
fail: reject
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function switchTab(path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.switchTab({
|
||||
url: path,
|
||||
success: resolve,
|
||||
fail: reject
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
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 })
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function openLogin(redirectPath) {
|
||||
return navigate('/pages/login/index', {
|
||||
redirect: redirectPath || getCurrentPageUrl()
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user