You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

132 lines
4.0 KiB

/**
* 获取 location hash 参数
* @param {string} variable 参数名
* @returns {string} 参数值
*/
export const getQueryVariable = (variable) => {
const query = window.location.hash.split('?');
if (query.length < 2) {
return '';
}
const vars = query[1].split('&');
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
if (pair[0] === variable) {
return decodeURI(pair[1]);
}
}
return '';
};
/**
* 滚动至指定dom底部
*/
export const scrollToBottom = (sDom, sTop) => {
if (!sDom) return;
sDom.scrollTo({
top: sTop
// behavior: 'smooth'
});
};
/**
* 数组去重
*/
export const arrayUnique = (arr, replaceKey, holdKey) => {
let temp = {};
return arr.reduce((prev, cur) => {
if (!temp[cur[replaceKey]]) {
temp[cur[replaceKey]] = {index: prev.length};
prev.push(cur);
} else {
const oldItem = temp[cur[replaceKey]];
cur[holdKey] = oldItem[holdKey];
prev.splice(oldItem['index'], 1, cur);
}
return prev;
}, []);
};
export const generateRequestId1 = (length = 32) => {
const data =
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let nums = '';
for (let i = 0; i < length; i++) {
const r = parseInt(Math.random() * 61, 10);
nums += data[r];
}
return nums //+ '-' + parseInt(Math.random() * 10000000000, 10);
};
export const generateRequestId = (length = 10) => {
const data =
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let nums = '';
for (let i = 0; i < length; i++) {
const r = parseInt(Math.random() * 61, 10);
nums += data[r];
}
return nums + '-' + parseInt(Math.random() * 10000000000, 10);
};
function escapeHtml (str) {
return str.replace(/[&<>"'/]/g, function (match) {
return {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;'
}[match];
});
}
export const splitTextForTTS = (text, maxLength = 25) => {
// 定义优先分割的标点符号
const punctuation = ['。', ',', ';', '?', '!', ',', ';', '?', '!', '、'];
let segments = [];
let segment = '';
let tempSegment = '';
for (let i = 0; i < text.length; i++) {
tempSegment += text[i]; // 如果超过最大长度,则尝试在上一个标点符号处分割
if (tempSegment.length > maxLength) {
let lastPunctuationIndex = -1;
for (let j = tempSegment.length - 1; j >= 0; j--) {
if (punctuation.includes(tempSegment[j])) {
lastPunctuationIndex = j;
break;
}
} // 如果找到标点符号,则在标点符号后分割
if (lastPunctuationIndex !== -1) {
segments.push(tempSegment.slice(0, lastPunctuationIndex + 1).trim());
tempSegment = tempSegment.slice(lastPunctuationIndex + 1).trim();
} else {
// 如果没有找到标点符号,则在最大长度处分割
segments.push(tempSegment.slice(0, maxLength).trim());
tempSegment = tempSegment.slice(maxLength).trim();
}
}
} // 添加最后一个段落
if (tempSegment.length > 0) {
segments.push(tempSegment.trim());
}
return segments;
}