wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { last } from 'lodash-es';
import { extname } from '/@/cool/utils';
import { module } from '/@/cool';
 
// 模块参数
const { options } = module.get('upload');
 
// 规则列表
const rules: Upload.Rule[] = options.rules || [];
 
// 文件大小
export function fileSize(size = 0): string {
    const num = 1024.0;
 
    if (size < num) return size + 'B';
    if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + 'K';
    if (size < Math.pow(num, 3)) return (size / Math.pow(num, 2)).toFixed(2) + 'M';
    if (size < Math.pow(num, 4)) return (size / Math.pow(num, 3)).toFixed(2) + 'G';
    return (size / Math.pow(num, 4)).toFixed(2) + 'T';
}
 
// 文件规则
export function fileRule(path?: string) {
    const d = rules.find(e => {
        return e.exts.find(a => a == extname(path || '').toLocaleLowerCase());
    });
 
    if (path?.includes('data:image/')) {
        return rules.find(e => e.type == 'image')!;
    }
 
    return (d || rules.find(e => e.type == 'file')!)!;
}
 
// 获取规则
export function getRule(type?: string) {
    return (rules.find(e => e.type == type?.replace('application/', '')) || last(rules))!;
}
 
// 获取类型
export function getType(path: string) {
    return fileRule(path).type;
}
 
// 拼接数组下的url
export function getUrls(list: any[]) {
    return list.map(e => e.url.replace(/,/g, encodeURIComponent(',')));
}
 
// 路径拼接
export function pathJoin(...parts: string[]): string {
    if (parts.length === 0) {
        return '';
    }
 
    const firstPart = parts[0];
    let isAbsolute = false;
 
    // 检查第一个部分是否以 "http" 开头,以确定路径类型(绝对还是相对)
    if (firstPart.startsWith('http')) {
        isAbsolute = true;
    }
 
    // 标准化路径,去除任何开头或结尾的斜杠
    const normalizedParts = parts.map(part => part.replace(/(^\/+|\/+$)/g, ''));
 
    if (isAbsolute) {
        // 如果是绝对路径,使用斜杠连接部分
        return normalizedParts.join('/');
    } else {
        // 如果是相对路径,使用平台特定的分隔符连接部分
        return normalizedParts.join('/');
    }
}