页面跳转,跳转拦截

This commit is contained in:
Jason
2022-09-09 14:44:55 +08:00
parent cda8b3e3b3
commit cc8fa61bea
15 changed files with 233 additions and 47 deletions

View File

@@ -23,7 +23,7 @@ const requestHooks: RequestHooks = {
return options
},
responseInterceptorsHook(response, config) {
const { isTransformResponse, isReturnDefaultResponse } = config
const { isTransformResponse, isReturnDefaultResponse, isAuth } = config
//返回默认响应,当需要获取响应头及其他数据时可使用
if (isReturnDefaultResponse) {
@@ -54,6 +54,11 @@ const requestHooks: RequestHooks = {
case RequestCodeEnum.TOKEN_INVALID:
case RequestCodeEnum.TOKEN_EMPTY:
logout()
if (isAuth && !getToken()) {
uni.navigateTo({
url: '/pages/login/login'
})
}
return Promise.reject()
default:
@@ -77,6 +82,7 @@ const defaultOptions: HttpRequestOptions = {
ignoreCancel: false,
// 是否携带token
withToken: true,
isAuth: false,
requestHooks: requestHooks
}

View File

@@ -14,6 +14,7 @@ export interface RequestConfig {
urlPrefix: string
ignoreCancel: boolean
withToken: boolean
isAuth: boolean
}
export interface RequestHooks {

View File

@@ -1,3 +1,5 @@
import { isObject } from '@vue/shared'
/**
* @description 获取元素节点信息在组件中的元素必须要传ctx
* @param { String } selector 选择器 '.app' | '#app'
@@ -23,3 +25,82 @@ export const getRect = (selector: string, all = false, context?: any) => {
.exec()
})
}
/**
* @description 获取当前页面实例
*/
export function currentPage() {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
return currentPage || {}
}
/**
* @description 后台选择链接专用跳转
*/
interface Link {
path: string
name?: string
type: string
isTab: boolean
query?: Record<string, any>
}
export enum LinkTypeEnum {
'SHOP_PAGES' = 'shop',
'CUSTOM_LINK' = 'custom'
}
export function navigateTo(link: Link) {
console.log(objectToQuery(link.query), link)
let url: string
switch (link.type) {
case LinkTypeEnum.SHOP_PAGES:
url = link.query ? `${link.path}?${objectToQuery(link.query)}` : link.path
if (link.isTab) {
uni.switchTab({ url })
} else {
uni.navigateTo({ url })
}
break
case LinkTypeEnum.CUSTOM_LINK:
uni.navigateTo({ url: `/pages/webview/webview?url=${link.path}` })
}
}
/**
* @description 是否为空
* @param {unknown} value
* @return {Boolean}
*/
export const isEmpty = (value: unknown) => {
return value == null && typeof value == 'undefined'
}
/**
* @description 对象格式化为Query语法
* @param { Object } params
* @return {string} Query语法
*/
export function objectToQuery(params: Record<string, any>): string {
let query = ''
for (const props of Object.keys(params)) {
const value = params[props]
const part = encodeURIComponent(props) + '='
if (!isEmpty(value)) {
console.log(encodeURIComponent(props), isObject(value))
if (isObject(value)) {
for (const key of Object.keys(value)) {
if (!isEmpty(value[key])) {
const params = props + '[' + key + ']'
const subPart = encodeURIComponent(params) + '='
query += subPart + encodeURIComponent(value[key]) + '&'
}
}
} else {
console.log(part + encodeURIComponent(value), '####')
query += part + encodeURIComponent(value) + '&'
}
}
}
return query.slice(0, -1)
}