199 lines
4.2 KiB
JavaScript
199 lines
4.2 KiB
JavaScript
const auth = require('./auth')
|
|
const { TAB_PAGES } = require('./config')
|
|
|
|
let waitingForAutoLoginNavigation = false
|
|
|
|
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/home/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 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
|
|
}
|
|
|
|
if (isTabPage(fallbackPath || '/pages/home/index')) {
|
|
wx.switchTab({
|
|
url: fallbackPath || '/pages/home/index'
|
|
})
|
|
return
|
|
}
|
|
|
|
wx.redirectTo({
|
|
url: fallbackPath || '/pages/home/index'
|
|
})
|
|
}
|
|
|
|
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
|
|
}, {})
|
|
}
|
|
|
|
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(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
|
|
})
|
|
})
|
|
}
|
|
|
|
function openAuth(redirect) {
|
|
return navigate('/pages/auth/index', {
|
|
redirect: redirect || getCurrentPageUrl()
|
|
})
|
|
}
|
|
|
|
function showLoginToast(actionText) {
|
|
wx.showToast({
|
|
title: `${actionText || '继续操作'}前请先登录`,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
|
|
function scheduleOpenAuth(actionText) {
|
|
showLoginToast(actionText)
|
|
setTimeout(() => {
|
|
openAuth()
|
|
}, 120)
|
|
}
|
|
|
|
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,
|
|
goBack,
|
|
redirectAfterLogin,
|
|
openAuth,
|
|
ensureLogin,
|
|
openProducts(filter) {
|
|
const app = getApp()
|
|
app.globalData.pendingProductFilter = filter || {}
|
|
return switchTab('/pages/products/index')
|
|
}
|
|
}
|