功能:新增微信小程序界面
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
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/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 ensureLogin(actionText) {
|
||||
if (auth.isLoggedIn()) {
|
||||
return true
|
||||
}
|
||||
|
||||
wx.showToast({
|
||||
title: `${actionText || '继续操作'}前请先登录`,
|
||||
icon: 'none'
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
openAuth()
|
||||
}, 120)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
function openProducts(filter) {
|
||||
const app = getApp()
|
||||
app.globalData.pendingProductFilter = filter || {}
|
||||
return switchTab('/pages/products/index')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildUrl,
|
||||
getCurrentPageUrl,
|
||||
isTabPage,
|
||||
navigate,
|
||||
switchTab,
|
||||
goBack,
|
||||
redirectAfterLogin,
|
||||
openAuth,
|
||||
ensureLogin,
|
||||
openProducts
|
||||
}
|
||||
Reference in New Issue
Block a user