读取文件
/**
* 读取文件
* @return 返回未解析的文件对象,需搭配解析方法使用
*/
async readFile(): Promise<{ files, value }> {
return new Promise((resolve, reject) => {
let input = document.createElement("input")
input.setAttribute("type", "file")
input.onchange = (e: any) => {
let data = {
files: e.target.files,
value: e.target.value
}
resolve(data)
}
input.click()
})
}
上传文件
async uploadFile(file: File, url: string) {
return new Promise((res, _rej) => {
let form = new FormData()
form.append("file", file)
let xhr = new XMLHttpRequest()
xhr.open("post", url, true)
xhr.addEventListener("readystatechange", () => {
let r = xhr
if (r.status != 200) {
console.warn("上传失败", r.status, r.statusText, r.response)
}
else if (r.readyState == 4) {
console.log("上传成功")
res(undefined)
}
})
xhr.send(form)
})
}
复制文本
/**
* 复制字符串到剪切板
* @param str 字符串
* @returns 返回是否成功
*/
copyStr(str: string | number, isMobile?: boolean): boolean {
if (isMobile) {
let div = document.createElement("div")
div.innerHTML = str.toString()
document.body.appendChild(div)
let range = document.createRange()
range.selectNode(div)
const selection = window.getSelection()
if (selection.rangeCount > 0) {
selection.removeAllRanges()
}
selection.addRange(range);
let check = document.execCommand("copy")
document.body.removeChild(div)
return check
}
let textArea = document.createElement('textarea')
textArea.innerHTML = str.toString()
document.body.appendChild(textArea)
textArea.select();
let check = document.execCommand('copy')
document.body.removeChild(textArea)
return check
}
解析ini
/** @type {(str:string)=>any} 解析ini格式为json,好方便转换 */
let decodeINI = (str) => {
var regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*/,
param: /^\s*([\w\.\-\_]+)\s*=\s*((\"((\\\")|.)*?\")|(\d*))\s*/,
comment: /^\s*(;|#).*$/
};
var value = {};
var lines = str.split(/\r\n|\r|\n/);
var section = null;
lines.forEach(function (line) {
// line = line.split("#")[0]
if (regex.comment.test(line)) {
return;
} else if (regex.param.test(line)) {
var match = line.match(regex.param);
if (section) {
value[section][match[1]] = eval(match[2]);
} else {
value[match[1]] = eval(match[2]);
}
} else if (regex.section.test(line)) {
var match = line.match(regex.section);
value[match[1]] = {};
section = match[1];
} else if (line.length == 0 && section) {
section = null;
};
});
return value;
}
解析op
/** @type {(str:string)=>any} 解析op格式为json,好方便转换 */
let decodeOP = (str) => {
let regex = {
config: /^\s*config\s+.*?(\s+|$)(\'.*?\')?$/,
option: /^\s*option\s+.*?(\s+|$)(\'.*?\')?$/
};
let value = {}
let lines = str.split(/\r\n|\r|\n/);
lines.push('\n')
let config = null;
let configName = null
lines.forEach(line => {
if (regex.option.test(line) && config && configName) {
let match = line.match(regex.option)
/** @type {string[]} 注解 */
let arr = match[0].split(/'/)[0].split(' ')
arr.filter(c => c != " ")
config.option[arr[1]] = match[0].split(/'/)?.[1] || ""
}
else if (regex.config.test(line)) {
let match = line.match(regex.config)
/** @type {string[]} 注解 */
let arr = match[0].split(/'/)[0].split(' ')
arr.filter(c => c != " ")
config = {
val: match[0].split(/'/)?.[1] || "",
option: {}
}
configName = arr[1]
}
else {
if (!config || !configName) {
return
}
if (value[configName]) {
if (Array.isArray(value[configName])) {
value[configName].push(config)
}
else {
console.log(JSON.stringify(value[configName]))
value[configName] = [value[configName], config]
}
}
else {
value[configName] = config
}
config = null
configName = null
}
})
return value
}