阿拉伯数字转汉字
//方案一
NumberToChinese(num) {
var chnNumChar = [
"零",
"一",
"二",
"三",
"四",
"五",
"六",
"七",
"八",
"九"
];
var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
var chnUnitChar = ["", "十", "百", "千"];
var unitPos = 0;
var strIns = "",
chnStr = "";
var needZero = false;
if (num === 0) {
return chnNumChar[0];
}
while (num > 0) {
var section = num % 10000;
if (needZero) {
chnStr = chnNumChar[0] + chnStr;
}
strIns = this.SectionToChinese(section);
strIns += section !== 0 ? chnUnitSection[unitPos] : chnUnitSection[0];
chnStr = strIns + chnStr;
needZero = section < 1000 && section > 0;
num = Math.floor(num / 10000);
unitPos++;
}
return chnStr;
},
lazyLoadScript(url) {
return new Promise((resolve, reject) => {
let script = document.createElement("script");
script.src = url;
script.type = "text/javascript";
let head = document.getElementsByTagName("head")[0];
head.appendChild(script);
script.onload = () => {
resolve();
};
script.onerror = err => {
reject(err);
};
});
},
//方案二
DX(n) {
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法";
if (n === 0) {
return "零";
}
let unit = "万千百拾亿千百拾万千百拾元角分";
let str = "";
n += "00";
let p = n.indexOf(".");
if (p >= 0) n = n.substring(0, p) + n.substr(p + 1, 2);
unit = unit.substr(unit.length - n.length);
for (var i = 0; i < n.length; i++) {
str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i);
}
return str
.replace(/零(千|百|拾|角)/g, "零")
.replace(/(零)+/g, "零")
.replace(/零(万|亿|元)/g, "$1")
.replace(/(亿)万|壹(拾)/g, "$1$2")
.replace(/^元零?|零分/g, "")
.replace(/元$/g, "元整");
},
其他常用
setSS: (key, value) => {
sessionStorage.setItem(key, JSON.stringify(value));
},
getSS: key => {
let value = sessionStorage.getItem(key);
try {
return JSON.parse(value);
} catch (err) {
return value;
}
},
delSS: key => {
sessionStorage.removeItem(key);
},
setLS: (key, value) => {
localStorage.setItem(key, JSON.stringify(value));
},
getLS: key => {
let value = localStorage.getItem(key);
try {
return JSON.parse(value);
} catch (err) {
return value;
}
},
delLS: key => {
localStorage.removeItem(key);
},
setCookie: (name, value) => {
const Days = 0.5;
const exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie =
name + "=" + escape(value) + ";expires=" + exp.toGMTString();
},
getCookie: name => {
const reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
const arr = document.cookie.match(reg);
if (arr) {
return unescape(arr[2]);
} else {
return null;
}
},
delCookie: name => {
const exp = new Date();
exp.setTime(exp.getTime() - 1);
const cval = utils.getCookie(name);
if (cval != null) {
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
},
downLoad: downLoadUrl => {
let a = document.createElement("a");
if (a.download) {
a.href = downLoadUrl;
if (document.createEvent) {
var evObj = document.createEvent("MouseEvents");
evObj.initEvent(
"click",
true,
true,
window,
1,
12,
345,
7,
220,
false,
false,
true,
false,
0,
null
);
a.dispatchEvent(evObj);
} else if (document.createEventObject) {
a.fireEvent("onmousemove");
}
} else {
window.open(downLoadUrl, "_blank");
}
},
postDownLoad: (url, options, fileName) => {
Message({
message: "开始下载",
type: "success"
});
axiosHttp()({
method: options.method || "post",
url: url,
responseType: "blob",
timeout: options.timeout || 10000,
params: options.params || {
},
data: options.data || {
}
})
.then(res => {
const blob = new Blob([res.data]);
// 兼容不同浏览器的URL对象
const url = window.URL || window.webkitURL || window.moxURL;
// 获取后台提供的文件名
let backendFileName = res.headers["content-disposition"];
if (/utf-8''/i.test(backendFileName)) {
backendFileName = backendFileName.split("-8''")[1];
} else if (backendFileName && backendFileName.length >= 2) {
backendFileName = backendFileName.split("=")[1];
}
backendFileName = decodeURIComponent(backendFileName);
// 兼容IE
if ("msSaveOrOpenBlob" in navigator) {
window.navigator.msSaveOrOpenBlob(blob, fileName || backendFileName);
return;
}
// 创建下载链接
const downloadHref = url.createObjectURL(blob);
// 创建a标签并为其添加属性
let downloadLink = document.createElement("a");
downloadLink.href = downloadHref;
downloadLink.download = fileName || backendFileName || "未知文件.txt";
// 触发点击事件执行下载
downloadLink.click();
})
.catch(_ => {
Message({
message: "下载失败",
type: "error"
});
});
},
imgView: downLoadUrl => {
window.open(<