网络请求新增移除null类型参数

This commit is contained in:
不做码农 2023-06-17 16:04:31 +08:00
parent d1b1575535
commit 19edbc6703
3 changed files with 59 additions and 46 deletions

View File

@ -30,17 +30,7 @@ export function formatTime(time, option) {
if (option) { if (option) {
return parseTime(time, option) return parseTime(time, option)
} else { } else {
return ( return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'时' +
d.getMinutes() +
'分'
)
} }
} }
@ -85,7 +75,7 @@ export function cleanArray(actual) {
export function param(json) { export function param(json) {
if (!json) return '' if (!json) return ''
return cleanArray( return cleanArray(
Object.keys(json).map(key => { Object.keys(json).map((key) => {
if (json[key] === undefined) return '' if (json[key] === undefined) return ''
return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]) return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
}) })
@ -103,7 +93,7 @@ export function param2Obj(url) {
} }
const obj = {} const obj = {}
const searchArr = search.split('&') const searchArr = search.split('&')
searchArr.forEach(v => { searchArr.forEach((v) => {
const index = v.indexOf('=') const index = v.indexOf('=')
if (index !== -1) { if (index !== -1) {
const name = v.substring(0, index) const name = v.substring(0, index)
@ -137,7 +127,7 @@ export function objectMerge(target, source) {
if (Array.isArray(source)) { if (Array.isArray(source)) {
return source.slice() return source.slice()
} }
Object.keys(source).forEach(property => { Object.keys(source).forEach((property) => {
const sourceProperty = source[property] const sourceProperty = source[property]
if (typeof sourceProperty === 'object') { if (typeof sourceProperty === 'object') {
target[property] = objectMerge(target[property], sourceProperty) target[property] = objectMerge(target[property], sourceProperty)
@ -161,9 +151,7 @@ export function toggleClass(element, className) {
if (nameIndex === -1) { if (nameIndex === -1) {
classString += '' + className classString += '' + className
} else { } else {
classString = classString = classString.substr(0, nameIndex) + classString.substr(nameIndex + className.length)
classString.substr(0, nameIndex) +
classString.substr(nameIndex + className.length)
} }
element.className = classString element.className = classString
} }
@ -233,7 +221,7 @@ export function deepClone(source) {
throw new Error('error arguments', 'deepClone') throw new Error('error arguments', 'deepClone')
} }
const targetObj = source.constructor === Array ? [] : {} const targetObj = source.constructor === Array ? [] : {}
Object.keys(source).forEach(keys => { Object.keys(source).forEach((keys) => {
if (source[keys] && typeof source[keys] === 'object') { if (source[keys] && typeof source[keys] === 'object') {
targetObj[keys] = deepClone(source[keys]) targetObj[keys] = deepClone(source[keys])
} else { } else {
@ -297,19 +285,17 @@ export function makeMap(str, expectsLowerCase) {
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
map[list[i]] = true map[list[i]] = true
} }
return expectsLowerCase ? return expectsLowerCase ? (val) => map[val.toLowerCase()] : (val) => map[val]
val => map[val.toLowerCase()] :
val => map[val]
} }
// 首字母大小 // 首字母大小
export function titleCase(str) { export function titleCase(str) {
return str.replace(/( |^)[a-z]/g, L => L.toUpperCase()) return str.replace(/( |^)[a-z]/g, (L) => L.toUpperCase())
} }
// 下划转驼峰 // 下划转驼峰
export function camelCase(str) { export function camelCase(str) {
return str.replace(/_[a-z]/g, str1 => str1.substr(-1).toUpperCase()) return str.replace(/_[a-z]/g, (str1) => str1.substr(-1).toUpperCase())
} }
// 是否数字 // 是否数字
@ -324,11 +310,11 @@ export function isNumberStr(str) {
* @returns 返回处理后的颜色值 * @returns 返回处理后的颜色值
*/ */
export function getLightColor(color, level) { export function getLightColor(color, level) {
let reg = /^\#?[0-9A-Fa-f]{6}$/; let reg = /^\#?[0-9A-Fa-f]{6}$/
if (!reg.test(color)) return color; if (!reg.test(color)) return color
let rgb = hexToRgb(color); let rgb = hexToRgb(color)
for (let i = 0; i < 3; i++) rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i]); for (let i = 0; i < 3; i++) rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i])
return rgbToHex(rgb[0], rgb[1], rgb[2]); return rgbToHex(rgb[0], rgb[1], rgb[2])
} }
/** /**
@ -337,13 +323,13 @@ export function getLightColor(color, level) {
* @returns 返回处理后的颜色值 * @returns 返回处理后的颜色值
*/ */
export function hexToRgb(str) { export function hexToRgb(str) {
let hexs = ''; let hexs = ''
let reg = /^\#?[0-9A-Fa-f]{6}$/; let reg = /^\#?[0-9A-Fa-f]{6}$/
if (!reg.test(str)) return str; if (!reg.test(str)) return str
str = str.replace('#', ''); str = str.replace('#', '')
hexs = str.match(/../g); hexs = str.match(/../g)
for (let i = 0; i < 3; i++) hexs[i] = parseInt(hexs[i], 16); for (let i = 0; i < 3; i++) hexs[i] = parseInt(hexs[i], 16)
return hexs; return hexs
} }
/** /**
@ -354,12 +340,11 @@ export function hexToRgb(str) {
* @returns 返回处理后的颜色值 * @returns 返回处理后的颜色值
*/ */
export function rgbToHex(r, g, b) { export function rgbToHex(r, g, b) {
let reg = /^\d{1,3}$/; let reg = /^\d{1,3}$/
if (!reg.test(r) || !reg.test(g) || !reg.test(b)) return ""; if (!reg.test(r) || !reg.test(g) || !reg.test(b)) return ''
let hexs = [r.toString(16), g.toString(16), b.toString(16)]; let hexs = [r.toString(16), g.toString(16), b.toString(16)]
for (let i = 0; i < 3; i++) for (let i = 0; i < 3; i++) if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`
if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`; return `#${hexs.join('')}`
return `#${hexs.join('')}`;
} }
/** /**

View File

@ -3,7 +3,7 @@ import { ElMessageBox, ElMessage, ElLoading } from 'element-plus'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode' import errorCode from '@/utils/errorCode'
import useUserStore from '@/store/modules/user' import useUserStore from '@/store/modules/user'
import { blobValidate } from '@/utils/ruoyi' import { blobValidate, delEmptyQueryNodes } from '@/utils/ruoyi'
import { saveAs } from 'file-saver' import { saveAs } from 'file-saver'
let downloadLoadingInstance let downloadLoadingInstance
@ -28,6 +28,11 @@ service.interceptors.request.use(
config.headers['userid'] = useUserStore().userId config.headers['userid'] = useUserStore().userId
config.headers['userName'] = useUserStore().userName config.headers['userName'] = useUserStore().userName
} }
const method = config?.method || 'get'
if (method.toLowerCase() === 'post' || method.toLowerCase() === 'put') {
config.data = delEmptyQueryNodes(config.data)
}
return config return config
}, },
(error) => { (error) => {
@ -81,18 +86,23 @@ service.interceptors.response.use(
message = '后端接口连接异常' message = '后端接口连接异常'
} else if (message.includes('timeout')) { } else if (message.includes('timeout')) {
message = '系统接口请求超时' message = '系统接口请求超时'
} else if (message.includes('Request failed with status code 429')) { } else if (message.includes('code 429')) {
message = '请求过于频繁,请稍后再试' message = '请求过于频繁,请稍后再试'
} else if (message.includes('Request failed with status code')) { } else if (message.includes('Request failed with status code')) {
message = '系统接口' + message.substr(message.length - 3) + '异常,请联系管理员' message = '系统接口' + message.substr(message.length - 3) + '异常,请联系管理员'
if (import.meta.env.DEV) {
message = 'Oops,后端出错了,你不会连错误日志都不会看吧'
}
} }
ElMessage({ ElMessage({
message: message, message: message,
type: 'error', type: 'error',
duration: 3 * 1000, duration: 0,
showClose: true,
grouping: true grouping: true
}) })
return Promise.reject(error) return Promise.reject()
} }
) )

View File

@ -293,3 +293,21 @@ export function getWeek(num = 0) {
var week = ['日', '一', '二', '三', '四', '五', '六'] var week = ['日', '一', '二', '三', '四', '五', '六']
return '星期' + week[datas] return '星期' + week[datas]
} }
// 移除空字符串null, undefined
export const delEmptyQueryNodes = (obj = {}) => {
if (Array.isArray(obj)) {
return obj
}
const params = Object.keys(obj)
.filter((key) => obj[key] !== null && obj[key] !== undefined)
.reduce(
(acc, key) => ({
...acc,
[key]: obj[key]
}),
{}
)
// console.log('过滤后参数=', params)
return params
}