jhzh
2025-04-03 db12897dc68c68d40c557aa59ad78022e2b30ac2
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
/**
 * 字符串方法的扩展
 *
 * @author lautin
 * @created 2019-11-22 15:24:20
 */
 
/**
 * 生成一组随机值的方法,用于上传文件名等场景
 * @param {number} len
 */
function random(count = null) {
    let len = count || 32,
        $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678',
        ret = '',
        time = new Date().getTime().toString();
 
    if (len / 2 > time.length) {
        // 控制总长度不超出
        len = len - time.length
    } else {
        len = len / 2
    }
    for (let i = 0; i < len; i++) {
        ret += $chars.charAt(Math.floor(Math.random() * $chars.length))
        if (time[i]) {
            ret += time[time.length - i - 1]
        }
    }
    return ret;
}
/**
 * 截取字符串之前之后
 * @param {string} str 
 * @param {string} targe 
 * @param {number} index 
 */
function getCaptionLength(str, targe, index) {
    if (!index) {
        return str.substring(0, str.indexOf(targe))
    } else {
        return str.substring(str.lastIndexOf(targe) + 1)
    }
 
}
 
// 绑定为静态方法
Object.assign(String, {
    random,
    getCaptionLength
});
 
// fontcolor设置别名
String.prototype.color = Object.prototype.fontcolor;
 
export default {
    random,
    getCaptionLength
}