This commit is contained in:
dap
2024-11-25 11:29:58 +08:00
53 changed files with 170 additions and 271 deletions

View File

@@ -1,75 +1,5 @@
import { isObject, isString } from '@vben/utils';
import { requestClient } from './request';
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
export function joinTimestamp<T extends boolean>(
join: boolean,
restful: T,
): T extends true ? string : object;
export function joinTimestamp(join: boolean, restful = false): object | string {
if (!join) {
return restful ? '' : {};
}
const now = Date.now();
if (restful) {
return `?_t=${now}`;
}
return { _t: now };
}
/**
* @description: Format request parameter time
*/
export function formatRequestDate(params: Record<string, any>) {
if (Object.prototype.toString.call(params) !== '[object Object]') {
return;
}
for (const key in params) {
const format = params[key]?.format ?? null;
if (format && typeof format === 'function') {
params[key] = params[key].format(DATE_TIME_FORMAT);
}
if (isString(key)) {
const value = params[key];
if (value) {
try {
params[key] = isString(value) ? value.trim() : value;
} catch (error: any) {
throw new Error(error);
}
}
}
if (isObject(params[key])) {
formatRequestDate(params[key]);
}
}
}
/**
* Add the object as a parameter to the URL
* @param baseUrl url
* @param obj
* @returns {string}
* eg:
* let obj = {a: '3', b: '4'}
* setObjToUrlParams('www.baidu.com', obj)
* ==>www.baidu.com?a=3&b=4
*/
export function setObjToUrlParams(baseUrl: string, obj: any): string {
let parameters = '';
for (const key in obj) {
parameters += `${key}=${encodeURIComponent(obj[key])}&`;
}
parameters = parameters.replace(/&$/, '');
return /\?$/.test(baseUrl)
? baseUrl + parameters
: baseUrl.replace(/\/?$/, '?') + parameters;
}
/**
* @description: contentType
*/

View File

@@ -12,7 +12,6 @@ import {
RequestClient,
} from '@vben/request';
import { useAccessStore } from '@vben/stores';
import { isString } from '@vben/utils';
import { message, Modal } from 'ant-design-vue';
import { isEmpty, isNull } from 'lodash-es';
@@ -27,8 +26,6 @@ import {
} from '#/utils/encryption/crypto';
import * as encryptUtil from '#/utils/encryption/jsencrypt';
import { formatRequestDate, joinTimestamp, setObjToUrlParams } from './helper';
const { apiURL, clientId, enableEncrypt } = useAppConfig(
import.meta.env,
import.meta.env.PROD,
@@ -46,16 +43,10 @@ function createRequestClient(baseURL: string) {
baseURL,
// 消息提示类型
errorMessageMode: 'message',
// 格式化提交参数时间
formatDate: true,
// 是否返回原生响应 比如:需要获取响应头时使用该属性
isReturnNativeResponse: false,
// 需要对返回数据进行处理
isTransformResponse: true,
// post请求的时候添加参数到url
joinParamsToUrl: false,
// 是否加入时间戳
joinTime: false,
});
/**
@@ -99,54 +90,11 @@ function createRequestClient(baseURL: string) {
*/
const language = preferences.app.locale.replace('-', '_');
config.headers['Accept-Language'] = language;
config.headers['Content-Language'] = language;
// 添加全局clientId
config.headers.clientId = clientId;
const { encrypt, formatDate, joinParamsToUrl, joinTime = true } = config;
const params = config.params || {};
const data = config.data || false;
// TODO: 这块要重构 复杂度太高了
formatDate && data && !isString(data) && formatRequestDate(data);
if (config.method?.toUpperCase() === 'GET') {
if (isString(params)) {
// 兼容restful风格
config.url = `${config.url + params}${joinTimestamp(joinTime, true)}`;
config.params = undefined;
} else {
// 给 get 请求加上时间戳参数,避免从缓存中拿数据。
config.params = Object.assign(
params || {},
joinTimestamp(joinTime, false),
);
}
} else {
if (isString(params)) {
// 兼容restful风格
config.url = config.url + params;
config.params = undefined;
} else {
formatDate && formatRequestDate(params);
if (
Reflect.has(config, 'data') &&
config.data &&
(Object.keys(config.data).length > 0 ||
config.data instanceof FormData)
) {
config.data = data;
config.params = params;
} else {
// 非GET请求如果没有提供data则将params视为data
config.data = params;
config.params = undefined;
}
if (joinParamsToUrl) {
config.url = setObjToUrlParams(
config.url as string,
Object.assign({}, config.params, config.data),
);
}
}
}
const { encrypt } = config;
// 全局开启请求加密功能 && 该请求开启 && 是post/put请求
if (
enableEncrypt &&

View File

@@ -48,14 +48,6 @@ function handleChange(info: Record<string, any>) {
const name = file?.name;
switch (status) {
case 'uploading': {
if (!uploading) {
emit('uploading', name);
uploading = true;
}
break;
}
case 'done': {
// http 200会走到这里 需要再次判断
const { response } = file;
@@ -77,6 +69,14 @@ function handleChange(info: Record<string, any>) {
break;
}
case 'uploading': {
if (!uploading) {
emit('uploading', name);
uploading = true;
}
break;
}
// No default
}
}

View File

@@ -88,7 +88,10 @@ const avatar = computed(() => {
});
async function handleLogout() {
await authStore.logout();
/**
* 主动登出不需要带跳转地址
*/
await authStore.logout(false);
resetRoutes();
}