Files
tk-mini-program/components/ChatId.js
pengxiaolong 31527ba8a6 优化
2025-07-18 13:06:06 +08:00

21 lines
904 B
JavaScript

export function generateId() {
// 1. 随机首字母(大写或小写)
const isUpperCase = Math.random() >= 0.5;
const randomLetter = String.fromCharCode(
Math.floor(Math.random() * 26) + (isUpperCase ? 65 : 97)
);
// 2. 生成随机字符部分(包含字母和数字)
const randomPart = Array.from({ length: 5 }, () => {
const type = Math.random();
if (type < 0.3) return String.fromCharCode(Math.floor(Math.random() * 26) + 97); // 小写字母
else if (type < 0.6) return String.fromCharCode(Math.floor(Math.random() * 26) + 65); // 大写字母
else return Math.floor(Math.random() * 10); // 数字
}).join('');
// 3. 时间戳处理(取后四位十六进制)
const timestamp = Date.now().toString(16).slice(-4);
// 4. 组合成最终 ID
return `${randomLetter}${randomPart}${timestamp}`;
}