Files
AlgerMusicPlayer/src/utils/index.ts
T

86 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-05-23 17:12:35 +08:00
import { computed } from 'vue';
import store from '@/store';
2021-07-21 15:01:39 +08:00
// 设置歌手背景图片
export const setBackgroundImg = (url: String) => {
return `background-image:url(${url})`;
};
2021-07-21 15:01:39 +08:00
// 设置动画类型
export const setAnimationClass = (type: String) => {
return `animate__animated ${type}`;
};
2021-07-21 15:01:39 +08:00
// 设置动画延时
export const setAnimationDelay = (index: number = 6, time: number = 50) => {
return `animation-delay:${index * time}ms`;
};
2021-07-21 22:30:55 +08:00
// 将秒转换为分钟和秒
2021-07-21 22:30:55 +08:00
export const secondToMinute = (s: number) => {
2021-11-09 11:34:09 +08:00
if (!s) {
return '00:00';
2021-11-09 11:34:09 +08:00
}
const minute: number = Math.floor(s / 60);
const second: number = Math.floor(s % 60);
const minuteStr: string = minute > 9 ? minute.toString() : `0${minute.toString()}`;
const secondStr: string = second > 9 ? second.toString() : `0${second.toString()}`;
return `${minuteStr}:${secondStr}`;
};
2023-12-27 18:21:01 +08:00
// 格式化数字 千,万, 百万, 千万,亿
const units = [
{ value: 1e8, symbol: '亿' },
{ value: 1e4, symbol: '万' },
];
2024-05-21 11:01:23 +08:00
export const formatNumber = (num: string | number) => {
num = Number(num);
for (let i = 0; i < units.length; i++) {
if (num >= units[i].value) {
return `${(num / units[i].value).toFixed(1)}${units[i].symbol}`;
}
2023-12-27 18:21:01 +08:00
}
return num.toString();
};
const windowData = window as any;
export const getIsMc = () => {
2023-12-28 10:45:11 +08:00
if (!windowData.electron) {
return false;
2023-12-28 10:45:11 +08:00
}
if (windowData.electron.ipcRenderer.getStoreValue('set').isProxy) {
return true;
2023-12-28 10:45:11 +08:00
}
return false;
};
const ProxyUrl = import.meta.env.VITE_API_PROXY || 'http://110.42.251.190:9856';
2023-12-20 16:19:16 +08:00
export const getMusicProxyUrl = (url: string) => {
if (!getIsMc()) {
return url;
2023-12-20 16:19:16 +08:00
}
const PUrl = url.split('').join('+');
return `${ProxyUrl}/mc?url=${PUrl}`;
};
2023-12-20 16:19:16 +08:00
export const getImgUrl = computed(() => (url: string | undefined, size: string = '') => {
const bdUrl = 'https://image.baidu.com/search/down?url=';
2024-06-05 15:35:31 +08:00
const imgUrl = `${url}?param=${size}`;
if (!getIsMc()) {
return imgUrl;
}
return `${bdUrl}${encodeURIComponent(imgUrl)}`;
});
2024-05-23 17:12:35 +08:00
export const isMobile = computed(() => {
const flag = navigator.userAgent.match(
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i,
);
store.state.isMobile = !!flag;
// 给html标签 添加mobile
if (flag) document.documentElement.classList.add('mobile');
return !!flag;
});