diff --git a/src/utils/index.js b/src/utils/index.js index 0aa8e12..3a4cf6e 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -30,17 +30,7 @@ export function formatTime(time, option) { if (option) { return parseTime(time, option) } else { - return ( - d.getMonth() + - 1 + - '月' + - d.getDate() + - '日' + - d.getHours() + - '时' + - d.getMinutes() + - '分' - ) + return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分' } } @@ -85,7 +75,7 @@ export function cleanArray(actual) { export function param(json) { if (!json) return '' return cleanArray( - Object.keys(json).map(key => { + Object.keys(json).map((key) => { if (json[key] === undefined) return '' return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]) }) @@ -103,7 +93,7 @@ export function param2Obj(url) { } const obj = {} const searchArr = search.split('&') - searchArr.forEach(v => { + searchArr.forEach((v) => { const index = v.indexOf('=') if (index !== -1) { const name = v.substring(0, index) @@ -137,7 +127,7 @@ export function objectMerge(target, source) { if (Array.isArray(source)) { return source.slice() } - Object.keys(source).forEach(property => { + Object.keys(source).forEach((property) => { const sourceProperty = source[property] if (typeof sourceProperty === 'object') { target[property] = objectMerge(target[property], sourceProperty) @@ -161,9 +151,7 @@ export function toggleClass(element, className) { if (nameIndex === -1) { classString += '' + className } else { - classString = - classString.substr(0, nameIndex) + - classString.substr(nameIndex + className.length) + classString = classString.substr(0, nameIndex) + classString.substr(nameIndex + className.length) } element.className = classString } @@ -233,7 +221,7 @@ export function deepClone(source) { throw new Error('error arguments', 'deepClone') } const targetObj = source.constructor === Array ? [] : {} - Object.keys(source).forEach(keys => { + Object.keys(source).forEach((keys) => { if (source[keys] && typeof source[keys] === 'object') { targetObj[keys] = deepClone(source[keys]) } else { @@ -297,19 +285,17 @@ export function makeMap(str, expectsLowerCase) { for (let i = 0; i < list.length; i++) { map[list[i]] = true } - return expectsLowerCase ? - val => map[val.toLowerCase()] : - val => map[val] + return expectsLowerCase ? (val) => map[val.toLowerCase()] : (val) => map[val] } // 首字母大小 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) { - 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 返回处理后的颜色值 */ export function getLightColor(color, level) { - let reg = /^\#?[0-9A-Fa-f]{6}$/; - if (!reg.test(color)) return color; - let rgb = hexToRgb(color); - 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]); + let reg = /^\#?[0-9A-Fa-f]{6}$/ + if (!reg.test(color)) return color + let rgb = hexToRgb(color) + 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]) } /** @@ -337,13 +323,13 @@ export function getLightColor(color, level) { * @returns 返回处理后的颜色值 */ export function hexToRgb(str) { - let hexs = ''; - let reg = /^\#?[0-9A-Fa-f]{6}$/; - if (!reg.test(str)) return str; - str = str.replace('#', ''); - hexs = str.match(/../g); - for (let i = 0; i < 3; i++) hexs[i] = parseInt(hexs[i], 16); - return hexs; + let hexs = '' + let reg = /^\#?[0-9A-Fa-f]{6}$/ + if (!reg.test(str)) return str + str = str.replace('#', '') + hexs = str.match(/../g) + for (let i = 0; i < 3; i++) hexs[i] = parseInt(hexs[i], 16) + return hexs } /** @@ -354,12 +340,11 @@ export function hexToRgb(str) { * @returns 返回处理后的颜色值 */ export function rgbToHex(r, g, b) { - let reg = /^\d{1,3}$/; - if (!reg.test(r) || !reg.test(g) || !reg.test(b)) return ""; - let hexs = [r.toString(16), g.toString(16), b.toString(16)]; - for (let i = 0; i < 3; i++) - if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`; - return `#${hexs.join('')}`; + let reg = /^\d{1,3}$/ + if (!reg.test(r) || !reg.test(g) || !reg.test(b)) return '' + let hexs = [r.toString(16), g.toString(16), b.toString(16)] + for (let i = 0; i < 3; i++) if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}` + return `#${hexs.join('')}` } /** @@ -378,4 +363,4 @@ export function webNotify(optinos) { if (isSupported) { show() } -} \ No newline at end of file +} diff --git a/src/utils/request.js b/src/utils/request.js index db6f329..3d507b2 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -3,7 +3,7 @@ import { ElMessageBox, ElMessage, ElLoading } from 'element-plus' import { getToken } from '@/utils/auth' import errorCode from '@/utils/errorCode' import useUserStore from '@/store/modules/user' -import { blobValidate } from '@/utils/ruoyi' +import { blobValidate, delEmptyQueryNodes } from '@/utils/ruoyi' import { saveAs } from 'file-saver' let downloadLoadingInstance @@ -28,6 +28,11 @@ service.interceptors.request.use( config.headers['userid'] = useUserStore().userId config.headers['userName'] = useUserStore().userName } + const method = config?.method || 'get' + + if (method.toLowerCase() === 'post' || method.toLowerCase() === 'put') { + config.data = delEmptyQueryNodes(config.data) + } return config }, (error) => { @@ -81,18 +86,23 @@ service.interceptors.response.use( message = '后端接口连接异常' } else if (message.includes('timeout')) { message = '系统接口请求超时' - } else if (message.includes('Request failed with status code 429')) { + } else if (message.includes('code 429')) { message = '请求过于频繁,请稍后再试' } else if (message.includes('Request failed with status code')) { message = '系统接口' + message.substr(message.length - 3) + '异常,请联系管理员' + + if (import.meta.env.DEV) { + message = 'Oops,后端出错了,你不会连错误日志都不会看吧' + } } ElMessage({ message: message, type: 'error', - duration: 3 * 1000, + duration: 0, + showClose: true, grouping: true }) - return Promise.reject(error) + return Promise.reject() } ) diff --git a/src/utils/ruoyi.js b/src/utils/ruoyi.js index 39f1bbb..decde9c 100644 --- a/src/utils/ruoyi.js +++ b/src/utils/ruoyi.js @@ -293,3 +293,21 @@ export function getWeek(num = 0) { var week = ['日', '一', '二', '三', '四', '五', '六'] 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 +}