feat: utils

This commit is contained in:
xingyu4j
2025-11-11 13:43:53 +08:00
parent 9c8faf1db2
commit b6d6edeeeb
4 changed files with 198 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
import type { Recordable } from '@vben/types';
export * from './rangePickerProps';
export * from './routerHelper';
/**
* 查找数组对象的某个下标
* @param {Array} ary 查找的数组
* @param {Function} fn 判断的方法
*/
type Fn<T = any> = (item: T, index: number, array: Array<T>) => boolean;
export const findIndex = <T = Recordable<any>>(
ary: Array<T>,
fn: Fn<T>,
): number => {
if (ary.findIndex) {
return ary.findIndex((item, index, array) => fn(item, index, array));
}
let index = -1;
ary.some((item: T, i: number, ary: Array<T>) => {
const ret: boolean = fn(item, i, ary);
if (ret) {
index = i;
return true;
}
return false;
});
return index;
};

View File

@@ -0,0 +1,68 @@
import dayjs from 'dayjs';
import { $t } from '#/locales';
/** 时间段选择器拓展 */
export function getRangePickerDefaultProps() {
return {
// 设置日期格式,为数组时支持多格式匹配,展示以第一个为准。配置参考 dayjs支持自定义格式
format: 'YYYY-MM-DD HH:mm:ss',
// 绑定值的格式,对 value、defaultValue、defaultPickerValue 起作用。不指定则绑定值为 dayjs 对象
valueFormat: 'YYYY-MM-DD HH:mm:ss',
// 输入框提示文字
placeholder: [
$t('utils.rangePicker.beginTime'),
$t('utils.rangePicker.endTime'),
],
// 快捷时间范围
presets: [
{
label: $t('utils.rangePicker.today'),
value: [dayjs().startOf('day'), dayjs().endOf('day')],
},
{
label: $t('utils.rangePicker.last7Days'),
value: [
dayjs().subtract(7, 'day').startOf('day'),
dayjs().endOf('day'),
],
},
{
label: $t('utils.rangePicker.last30Days'),
value: [
dayjs().subtract(30, 'day').startOf('day'),
dayjs().endOf('day'),
],
},
{
label: $t('utils.rangePicker.yesterday'),
value: [
dayjs().subtract(1, 'day').startOf('day'),
dayjs().subtract(1, 'day').endOf('day'),
],
},
{
label: $t('utils.rangePicker.thisWeek'),
value: [dayjs().startOf('week'), dayjs().endOf('day')],
},
{
label: $t('utils.rangePicker.thisMonth'),
value: [dayjs().startOf('month'), dayjs().endOf('day')],
},
{
label: $t('utils.rangePicker.lastWeek'),
value: [
dayjs().subtract(1, 'week').startOf('day'),
dayjs().endOf('day'),
],
},
],
showTime: {
defaultValue: [
dayjs('00:00:00', 'HH:mm:ss'),
dayjs('23:59:59', 'HH:mm:ss'),
],
format: 'HH:mm:ss',
},
};
}

View File

@@ -0,0 +1,38 @@
import type {
RouteLocationNormalized,
RouteRecordNormalized,
} from 'vue-router';
import { defineAsyncComponent } from 'vue';
const modules = import.meta.glob('../views/**/*.{vue,tsx}');
/**
* 注册一个异步组件
* @param componentPath 例:/bpm/oa/leave/detail
*/
export function registerComponent(componentPath: string) {
for (const item in modules) {
if (item.includes(componentPath)) {
// 使用异步组件的方式来动态加载组件
return defineAsyncComponent(modules[item] as any);
}
}
}
export const getRawRoute = (
route: RouteLocationNormalized,
): RouteLocationNormalized => {
if (!route) return route;
const { matched, ...opt } = route;
return {
...opt,
matched: (matched
? matched.map((item) => ({
meta: item.meta,
name: item.name,
path: item.path,
}))
: undefined) as RouteRecordNormalized[],
};
};

View File

@@ -0,0 +1,63 @@
import { message } from 'ant-design-vue';
enum UploadType {
Image = 'image',
Video = 'video',
Voice = 'voice',
}
const useBeforeUpload = (type: UploadType, maxSizeMB: number) => {
const fn = (file: File): boolean => {
let allowTypes: string[] = [];
let name = '';
switch (type) {
case UploadType.Image: {
allowTypes = [
'image/jpeg',
'image/png',
'image/gif',
'image/bmp',
'image/jpg',
];
maxSizeMB = 2;
name = '图片';
break;
}
case UploadType.Video: {
allowTypes = ['video/mp4'];
maxSizeMB = 10;
name = '视频';
break;
}
case UploadType.Voice: {
allowTypes = [
'audio/mp3',
'audio/mpeg',
'audio/wma',
'audio/wav',
'audio/amr',
];
maxSizeMB = 2;
name = '语音';
break;
}
}
// 格式不正确
if (!allowTypes.includes(file.type)) {
message.error(`上传${name}格式不对!`);
return false;
}
// 大小不正确
if (file.size / 1024 / 1024 > maxSizeMB) {
message.error(`上传${name}大小不能超过${maxSizeMB}M!`);
return false;
}
return true;
};
return fn;
};
export { UploadType, useBeforeUpload };