index.ts
2.5 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
function getWeekday(date: string | number | Date) {
if (!date) return '异常日期';
const daysOfWeek = ['日', '一', '二', '三', '四', '五', '六']; // 星期几的中文表示
const day = new Date(date).getDay(); // 获取星期几,0: 星期日, 1: 星期一, ..., 6: 星期六
return '周' + daysOfWeek[day];
}
/**
* 将16进制颜色和透明度值合并为RGBA格式
* @param {string} hex 16进制颜色(例如:#000000)
* @param {number} alpha 透明度值(0-1之间)
* @returns {string} RGBA格式的颜色值
*/
function hexToRgba(hex: string, alpha: number) {
// 去掉 # 符号
hex = hex.replace('#', '');
// 如果是三位数的6进制,转为6位数(例如:#fff -> #ffffff)
if (hex.length === 3) {
hex = hex.split('').map(function (hex) {
return hex + hex;
}).join('');
}
// 解析颜色值
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
// 返回RGBA格式的颜色值
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
// 防抖
function debounce (fn: any, time: number) {
let timer: number;
return function (this: any, ...args: any[]) {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
clearTimeout(timer);
}, time);
};
}
// 节流
function throttle (fn: any, time: number) {
let inThrottle: any;
return function (this: any, ...args: any[]) {
// const context = this;
// const args = arguments;
if (!inThrottle) {
fn.apply(this, args);
inThrottle = true;
setTimeout(function () {
inThrottle = false;
}, time);
}
};
}
// 获取周几
import * as moment from 'moment';
function getWeek (date: any) { // 参数时间戳
let week = moment.default(date).day();
switch (week) {
case 1:
return '周一';
case 2:
return '周二';
case 3:
return '周三';
case 4:
return '周四';
case 5:
return '周五';
case 6:
return '周六';
case 0:
return '周日';
}
}
function getTeamErrorLogo(team_logo:any){
if(team_logo){
return `this.src='${team_logo}';this.onerror=null`;
}else{
return "this.src='https://img2.ydniu.com/app/images/team_logo.png';this.onerror=null"
}
}
function getTeamLazyErrorLogo(team_logo:any){
if(team_logo){
return team_logo;
}else{
return "https://img2.ydniu.com/app/images/team_logo.png";
}
}
export { getWeekday, hexToRgba, debounce, throttle, getWeek, getTeamErrorLogo, getTeamLazyErrorLogo };