xyyd-fatfox/src/utils/tool.ts

59 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import useLayoutStore from "@/layout/useLayoutStore";
import request from "./request";
/**
* 生成随机数
* @param min 最小值
* @param max 最大值
* @returns 随机数
*/
export function randomNum(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* 生成一个长度在 0 到 n 之间的随机字符串。
* @param n 最大字符串长度。
* @returns 随机生成的字符串。
*/
export function generateRandomString(n: number): string {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const length = Math.floor(Math.random() * (n + 1));
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
/**
* 将秒转换为“分:秒”的格式
* @param seconds - 输入的秒数
* @returns 格式化后的字符串格式为“MM:SS”
*/
export function formatSeconds(seconds: number): string {
// 计算分钟数
const minutes = Math.floor(seconds / 60);
// 计算剩余的秒数
const remainingSeconds = seconds % 60;
// 返回格式化后的字符串,确保分钟和秒数都是两位数
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}
export function sendEvent(list: {
widget: string;
space?: string;
action: string;
key: string;
}[]) {
const store = useLayoutStore()
request('POST', '/api/app/statistics', {
data: list.map((item) => ({
widget: item.widget,
space: item.space || store.state.current === 0 ? 'TAB_GAME' : store.state.current === 1 ? 'TAB_WORK' : 'TAB_EAZY',
action: item.action,
key: item.key
}))
})
}