Merge branch 'dev' of github.com:jetlinks/jetlinks-ui-vue into dev

This commit is contained in:
JiangQiming 2023-03-24 17:05:19 +08:00
commit ebd304fdba
3 changed files with 120 additions and 128 deletions

View File

@ -120,7 +120,7 @@ export interface SearchItemData {
export const handleParamsToString = (terms:SearchItemData[] = []) => {
const _terms: any[] = [
{ terms: [null,null,null]},
{ terms: [null,null,null]}
{ terms: [null,null,null], type: 'and'}
]
let termsIndex = 0
let termsStar = 0

View File

@ -1,22 +1,22 @@
import axios from 'axios'
import {BASE_API_PATH, TOKEN_KEY} from '@/utils/variable'
import { notification as Notification } from 'ant-design-vue'
import { BASE_API_PATH, TOKEN_KEY } from '@/utils/variable'
import { notification as Notification } from 'ant-design-vue'
import router from '@/router'
import { LoginPath } from '@/router/menu'
import {LocalStore} from "@/utils/comm";
import { cleanToken, getToken, LocalStore } from '@/utils/comm'
import type { AxiosInstance, AxiosResponse } from 'axios'
interface AxiosResponseRewrite<T = any[]> extends AxiosResponse<T, any> {
result: T
success: boolean
result: T
success: boolean
}
export const SUCCESS_CODE = 200 // 成功代码
export const request = axios.create({
withCredentials: false,
baseURL: BASE_API_PATH,
timeout: 1000 * 60 * 5
withCredentials: false,
baseURL: BASE_API_PATH,
timeout: 1000 * 60 * 5
})
/**
@ -30,15 +30,15 @@ export const request = axios.create({
}
* @returns {AxiosInstance}
*/
export const post = function<T>(url: string, data = {}, params = {}, ext={}) {
ext = typeof ext === 'string' ? { responseType: ext } : ext
return request<any, AxiosResponseRewrite<T>>({
...ext,
params,
method: 'POST',
url,
data
})
export const post = function <T>(url: string, data = {}, params = {}, ext = {}) {
ext = typeof ext === 'string' ? { responseType: ext } : ext
return request<any, AxiosResponseRewrite<T>>({
...ext,
params,
method: 'POST',
url,
data
})
}
/**
@ -47,12 +47,12 @@ export const post = function<T>(url: string, data = {}, params = {}, ext={}) {
* @param {Object} [data]
* @returns {AxiosInstance}
*/
export const put = function<T>(url: string, data = {},) {
return request<any, AxiosResponseRewrite<T>>({
method: 'PUT',
url,
data
})
export const put = function <T>(url: string, data = {}) {
return request<any, AxiosResponseRewrite<T>>({
method: 'PUT',
url,
data
})
}
/**
@ -61,12 +61,12 @@ export const put = function<T>(url: string, data = {},) {
* @param {Object} [data]
* @returns {AxiosInstance}
*/
export const patch = function<T>(url: string, data = {}) {
return request<any, AxiosResponseRewrite<T>>({
method: 'PATCH',
url,
data
})
export const patch = function <T>(url: string, data = {}) {
return request<any, AxiosResponseRewrite<T>>({
method: 'PATCH',
url,
data
})
}
/**
* GET method request
@ -75,13 +75,13 @@ export const patch = function<T>(url: string, data = {}) {
* @param {Object} [ext]
* @returns {AxiosInstance}
*/
export const get = function<T>(url: string, params = {}, ext?: any) {
return request<any, AxiosResponseRewrite<T>>({
method: 'GET',
url,
params,
...ext
})
export const get = function <T>(url: string, params = {}, ext?: any) {
return request<any, AxiosResponseRewrite<T>>({
method: 'GET',
url,
params,
...ext
})
}
/**
@ -91,13 +91,13 @@ export const get = function<T>(url: string, params = {}, ext?: any) {
* @param {Object} [ext]
* @returns {AxiosInstance}
*/
export const remove = function<T>(url: string, params = {}, ext?: any) {
return request<any, AxiosResponseRewrite<T>>({
method: 'DELETE',
url,
params,
...ext
})
export const remove = function <T>(url: string, params = {}, ext?: any) {
return request<any, AxiosResponseRewrite<T>>({
method: 'DELETE',
url,
params,
...ext
})
}
/**
@ -107,25 +107,25 @@ export const remove = function<T>(url: string, params = {}, ext?: any) {
* @return {*}
*/
export const getStream = function(url: string, params = {}) {
return get<any>(url, params, {
responseType: 'arraybuffer' // 设置请求数据类型返回blob可解析类型
})
return get<any>(url, params, {
responseType: 'arraybuffer' // 设置请求数据类型返回blob可解析类型
})
}
export const postStream = function(url: string, data={}, params = {}) {
return post<any>(url, data, params, {
responseType: 'arraybuffer' // 设置请求数据类型返回blob可解析类型
})
export const postStream = function(url: string, data = {}, params = {}) {
return post<any>(url, data, params, {
responseType: 'arraybuffer' // 设置请求数据类型返回blob可解析类型
})
}
const showNotification = ( message: string, description: string, key?: string, show: boolean = true ) => {
if (show) {
Notification.error({
key,
message,
description
})
}
const showNotification = (message: string, description: string, key?: string, show: boolean = true) => {
if (show) {
Notification.error({
key,
message,
description
})
}
}
/**
@ -134,87 +134,79 @@ const showNotification = ( message: string, description: string, key?: string, s
* @returns {Promise<never>}
*/
const errorHandler = (error: any) => {
if (error.response) {
const data = error.response.data
const status = error.response.status
if (status === 403) {
showNotification( 'Forbidden', (data.message + '').substr(0, 90), '403')
} else if (status === 500) {
showNotification( 'Server Side Error', (data.message + '').substr(0, 90), '500')
} else if (status === 400) {
showNotification( 'Request Error', (data.message + '').substr(0, 90), '400')
} else if (status === 401) {
showNotification( 'Unauthorized', '用户未登录', '401')
console.log('showNotification')
setTimeout(() => {
location.href = `/#${LoginPath}`
}, 0)
}
} else if (error.response === undefined) {
showNotification( error.message, (error.stack + '').substr(0, 90), undefined)
if (error.response) {
const data = error.response.data
const status = error.response.status
if (status === 403) {
showNotification('Forbidden', (data.message + '').substr(0, 90), '403')
} else if (status === 500) {
showNotification('Server Side Error', (data.message + '').substr(0, 90), '500')
} else if (status === 400) {
showNotification('Request Error', (data.message + '').substr(0, 90), '400')
} else if (status === 401) {
showNotification('Unauthorized', '用户未登录', '401')
setTimeout(() => {
cleanToken()
router.replace({
path: LoginPath
})
}, 0)
}
return Promise.reject(error)
} else if (error.response === undefined) {
showNotification(error.message, (error.stack + '').substr(0, 90), undefined)
}
return Promise.reject(error)
}
// request interceptor
request.interceptors.request.use(config => {
// 如果 token 存在
// 让每个请求携带自定义 token 请根据实际情况自行修改
const token = LocalStore.get(TOKEN_KEY)
// const token = store.$state.tokenAlias
if (!token) {
// setTimeout(() => {
// router.replace({
// path: LoginPath
// })
// }, 0)
setTimeout(() => {
location.href = `/#${LoginPath}`
}, 0)
return config
}
config.headers![TOKEN_KEY] = token
// 如果 token 存在
// 让每个请求携带自定义 token 请根据实际情况自行修改
const token = getToken()
if (!token) {
setTimeout(() => {
cleanToken()
router.replace({
path: LoginPath
})
}, 0)
return config
}
config.headers![TOKEN_KEY] = token
return config
}, errorHandler)
/**
* response interceptor
*/
request.interceptors.response.use(response => {
if (response.data instanceof ArrayBuffer) {
return response
} else {
const { status, message } = response.data
// 增加业务接口处理成功判断方式只需要判断返回参数包含success为true
if (typeof response.data === 'object' && typeof response.data.success === 'undefined') {
response.data.success = status === SUCCESS_CODE
}
// 统一显示异常业务信息
if (status !== SUCCESS_CODE && message) {
Notification.error({
message: 'Server Errors: ' + status,
description: message
})
}
// 如果返回的的是文件流那么return值则为response
if (response.headers['content-type'] === 'application/octet-stream; charset=UTF-8' || response.headers['content-type'] === 'application/vnd.ms-excel;charset=UTF-8') {
return response.data
} else {
return response.data
}
if (response.data instanceof ArrayBuffer) {
return response
} else {
const { status, message } = response.data
// 增加业务接口处理成功判断方式只需要判断返回参数包含success为true
if (typeof response.data === 'object' && typeof response.data.success === 'undefined') {
response.data.success = status === SUCCESS_CODE
}
// 如果返回的的是文件流那么return值则为response
if (response.headers['content-type'] === 'application/octet-stream; charset=UTF-8' || response.headers['content-type'] === 'application/vnd.ms-excel;charset=UTF-8') {
return response.data
} else {
return response.data
}
}
}, errorHandler)
export default {
request: axios,
post,
get,
patch,
put,
remove,
getStream,
postStream
request: axios,
post,
get,
patch,
put,
remove,
getStream,
postStream
}

View File

@ -3700,8 +3700,8 @@ jetlinks-store@^0.0.3:
jetlinks-ui-components@^1.0.5:
version "1.0.5"
resolved "http://47.108.170.157:9013/jetlinks-ui-components/-/jetlinks-ui-components-1.0.5.tgz#f5a8ce70859a9c523b2e50b1e3560ae11c21fd0b"
integrity sha512-raeJHyYE2LLa47FhlO3V7P/86+VDhaABlhB2ShJJZGwpR1mRpr+Y/r+iuW2PDsPST7shgk7lb2xsKrzJ8aZN6A==
resolved "http://47.108.170.157:9013/jetlinks-ui-components/-/jetlinks-ui-components-1.0.5.tgz#6dc396bc8a1b6f5a08accf5f46aec2099c15f481"
integrity sha512-mnVN6MfHfyZf82miEoZV8+ud6RBH29x0A8PfpcraFQUl9Wat6XcjGppr8FOkmFbGw8laCGK5jlbiX3cYJUwaxw==
dependencies:
"@vueuse/core" "^9.12.0"
ant-design-vue "^3.2.15"