Files
FOFCompiler/js/GeneralMethod.js
2025-08-14 15:43:15 +08:00

356 lines
9.3 KiB
JavaScript
Raw Permalink 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.

// 处理replace方法替换字符串含有$元素
Object.defineProperty(String.prototype, "replaceHack", {
value: function (substr, newSubStr) {
this.name = 'replaceHack';
this.enumerable = false;
try {
if (typeof newSubStr == 'string' && /\$/g.test(newSubStr)) {
newSubStr = newSubStr.replace(/\$/g, "$$$$");
};
return this.replace(substr, newSubStr)
} catch (error) {
console.log(error);
}
},
enumerable: false // 定义了一个不可枚举的属性
});
//禁止用F5键
document.onkeydown = function (e) {
var ev = window.event || e;
var code = ev.keyCode || ev.which;
if (code == 116) {
ev.keyCode ? ev.keyCode = 0 : ev.which = 0;
cancelBubble = true;
return false;
}
}
//不敏感大小写查询方法
function indexOfT(T1, T2) {
let Str = T1.toLowerCase();
let Str1 = T2.toLowerCase();
if (Str.indexOf(Str1) != -1) {
return true;
} else {
return false;
}
}
//生成32/64位随机字符串
function generateRandomNumberString(length = 32) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const alphaChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
// 确保生成的字符串长度在32到64位之间
length = Math.max(32, Math.min(64, length));
// 生成第一个字符(必须是字母)
let uuid = alphaChars.charAt(Math.floor(Math.random() * alphaChars.length));
// 生成中间部分
for (let i = 1; i < length - 1; i++) {
uuid += chars.charAt(Math.floor(Math.random() * chars.length));
}
// 生成最后一个字符(必须是字母)
uuid += alphaChars.charAt(Math.floor(Math.random() * alphaChars.length));
return uuid;
}
//根据字符串生成临时的UUID
function StrCodeUuid(hexDigits) {
return generateRandomNumberString();
}
//时间转时间戳10位
function Time_Strformat(shijianchuo) {
var data = new Date(shijianchuo)
var time3 = Date.parse(data);
time3 = time3 / 1000;
return time3;
}
//取时间戳13位
function Time_GetTime_1() {
var timestamp = (new Date()).valueOf();
return timestamp;
}
//取时间戳10位
function Time_GetTime_2() {
ts = Math.round(new Date().getTime() / 1000).toString();
return parseInt(ts);
}
/* 十六进制转文本辅助函数 */
let readUTF_10_6 = function (arr) {
if (typeof arr === 'string') {
return arr;
}
let UTF = '',
_arr = arr;
for (let i = 0; i < ObjLengthCount(_arr); i++) {
let one = _arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if (v && ObjLengthCount(one) == 8) {
let bytesLength = ObjLengthCount(v[0]);
let store = _arr[i].toString(2).slice(7 - bytesLength);
for (let st = 1; st < bytesLength; st++) {
store += _arr[st + i].toString(2).slice(2)
}
UTF += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1
} else {
UTF += String.fromCharCode(_arr[i])
}
}
return UTF
}
//十六进制转文本
let hexToString_16 = function (str) {
if (ObjLengthCount(str) % 2 != 0) {
return "";
}
let buf = [];
for (let i = 0; i < ObjLengthCount(str); i += 2) {
buf.push(parseInt(str.substring(i, i + 2), 16));
}
return readUTF_10_6(buf);
}
//文本转十六进制
let bin2Hex_16 = function (str) {
let re = /[\u4E00-\u9FA5]/
let ar = []
for (let i = 0; i < ObjLengthCount(str); i++) {
let a = ''
if (re.test(str.charAt(i))) { // 中文
a = encodeURI(str.charAt(i)).replaceHack(/%/g, '')
} else {
a = str.charCodeAt(i).toString(16)
}
ar.push(a)
}
str = ar.join('')
return str
}
function bin2Hex_16_2(str) {
let ar = [];
for (let i = 0; i < str.length; i++) {
let code = str.charCodeAt(i);
if (code > 0x7F) { // 如果是多字节字符(包括中文、特殊符号等)
let encoded = encodeURIComponent(str.charAt(i)).replace(/%/g, '');
for (let j = 0; j < encoded.length; j += 2) {
ar.push(encoded.substr(j, 2));
}
} else {
ar.push(('0' + code.toString(16)).slice(-2)); // 确保每个字符都是两位的十六进制数
}
}
return ar.join('');
}
//取对象或数组成员数
function ObjLengthCount(o) {
var t = typeof o;
if (t == 'string') {
return o.length;
} else if (t == 'object') {
var n = 0;
for (var i in o) {
n++;
}
return n;
}
if (o) {
return 0;
}
if (o.length) {
return o.length;
} else {
return 0;
}
}
//获取所有中间文本注意start和end要进行特殊符号转义
function TakeTheMiddleOfTheTextAll(str, start, end, ifRep = false) {
let strStr2 = str;
strStr2 = strStr2.replaceAll("\r\n", " " + ComNewlineCharacterFOFStuDiorn + " ")
strStr2 = strStr2.replaceAll("\n", " " + ComNewlineCharacterFOFStuDion + " ")
strStr2 = strStr2.replaceAll("\r", " " + ComNewlineCharacterFOFStuDior + "r ")
if (ifRep == true) {
strStr2 = strStr2.replaceAll("$", "12345ssdlhlhmzj12345")
start = start.replaceAll("$", "12345ssdlhlhmzj12345")
end = end.replaceAll("$", "12345ssdlhlhmzj12345")
}
let res = strStr2.match(new RegExp(`${start}(.*?)${end}`, "gm"))
if (res) {
for (let index = 0; index < res.length; index++) {
res[index] = res[index].replaceAll(" " + ComNewlineCharacterFOFStuDiorn + " ", "\r\n")
res[index] = res[index].replaceAll(" " + ComNewlineCharacterFOFStuDion + " ", "\n")
res[index] = res[index].replaceAll(" " + ComNewlineCharacterFOFStuDior + "r ", "\r")
if (ifRep == true) {
res[index] = res[index].replaceAll("12345ssdlhlhmzj12345", "$")
}
}
return res;
} else {
return [];
}
}
//获取第一个中间文本注意start和end要进行特殊符号转义
function TakeTheMiddleOfTheText2(str, start, end) {
let ls = TakeTheMiddleOfTheTextAll(str, start, end);
if (ls != undefined && ls != null) {
if (ls.length >= 1) {
return ls[0];
} else {
return "";
}
} else {
return "";
}
}
//判断是否正常JSON文本
function isJSONparse(jsonContent) {
jsonContent = jsonContent.replaceAll(" ", "");
if (typeof jsonContent == 'string') {
try {
var obj = JSON.parse(jsonContent);
if (jsonContent.indexOf('{') > -1 || jsonContent.indexOf('[') > -1) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
return false;
};
//生成二维码
function qrcodeImg(elText, qrcodeID, width, height) {
var qrcode = new QRCode(document.getElementById(qrcodeID), {
width: width,
height: height
});
if (elText == undefined) {
return;
}
qrcode.makeCode(elText);
}
function FilePath(str)//取全路径去除名称的路径
{
if (str.indexOf("/") != -1) {
str = str.replaceAll("/", "\\")
}
let strAll = str.split("\\");
if (strAll) {
if (strAll.length >= 1) {
str = str.replace(strAll[strAll.length - 1], "")
str = str.replaceAll("\\", "/")
return str;
}
}
return "";
}
function FilePathName(str)//取全路径中的名称区域
{
if (str.indexOf("/") != -1) {
str = str.replaceAll("/", "\\")
}
let strAll = str.split("\\");
if (strAll) {
if (strAll.length >= 1) {
return strAll[strAll.length - 1];
}
}
return "";
}
function FilePathName2(str)//文件名称去掉路径和后缀名
{
if (str.indexOf("/") != -1) {
str = str.replaceAll("/", "\\")
}
let strAll = str.split("\\");
if (strAll) {
if (strAll.length >= 1) {
let StrType = strAll[strAll.length - 1].split(".");
if (StrType) {
if (StrType.length >= 1) {
StrType[StrType.length - 1] = "." + StrType[StrType.length - 1]
let RetName = strAll[strAll.length - 1].substring(0, strAll[strAll.length - 1].length - StrType[StrType.length - 1].length)
return RetName
}
}
}
}
return "";
}
function FilePathName3(str)//获取后缀名
{
let strAll = str.split(".");
if (strAll) {
if (strAll.length >= 1) {
return "." + strAll[strAll.length - 1]
}
}
return "";
}
/*
msg:判断图片后缀格式【判断是否是图片格式】,返回真代表图片格式,否则不是图片格式
@FileExtension-文件后缀或文件名称
*/
function IfProjectImageType(FileExtension) {
let LsStrImage = `bmp、jpg、jpeg、tiff、gif、pcx、tga、exif、fpx、svg、psd、cdr、pcd、dxf、ufo、eps、ai、raw`;
let IfImageObj = false;
let ImageData = LsStrImage.split("、");
for (let index = 0; index < ObjLengthCount(ImageData); index++) {
if (ImageData[index] == FileExtension) {
IfImageObj = true;
break;
}
ImageData[index] = "." + ImageData[index]
}
for (let index = 0; index < ObjLengthCount(ImageData); index++) {
if (ImageData[index].indexOf(FileExtension) != -1) {
IfImageObj = true;
break;
}
}
return IfImageObj;
}
//判断是否包含中文
function TextisChinese(s) {
try {
let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g")
if (reg.test(s)) {
return true
} else {
return false
}
} catch (error) {
return true
}
}
//判断是否有符号
function hasSpecialStr(str) {
var specialChars = "~·`!@#$¥%^…&*()()—-_=+[]{}【】、|\\;:'\"“‘,./<>《》??,。";
var len = specialChars.length;
for (var i = 0; i < len; i++) {
if (str.indexOf(specialChars.substring(i, i + 1)) != -1) {
return true;
}
}
return false;
}
//获取文本内容的指定行内容
function getCodeTextLine(text, lineNumber) {
try {
const lines = text.split(/\r?\n/);
if (lineNumber > 0 && lineNumber <= lines.length) {
return lines[lineNumber - 1];
} else {
return null;
}
} catch (error) {
return null;
}
}
//判断有没有中文
function FOFcontainsChinese(str) {
var reg = /[\u4E00-\u9FA5]/;
return reg.test(str);
}
function FOFgetRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}