js数字,英文,中文混排
https://zhuanlan.zhihu.com/p/599058139
鼠标滚动时执行一次点击
阿拉伯数组转中文数字
export const numberToLocaleUpperCase = (n) => {
// if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)){
// return "数据非法"; //判断数据是否大于0
// }
var unit = "千百拾亿千百拾万千百拾元角分", str = "";
n += "00";
var indexpoint = n.indexOf('.'); // 如果是小数,截取小数点前面的位数
if (indexpoint >= 0){
n = n.substring(0, indexpoint) + n.substr(indexpoint+1, 2); // 若为小数,截取需要使用的unit单位
}
unit = unit.substr(unit.length - n.length); // 若为整数,截取需要使用的unit单位
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, "元整");
}
Vue.prototype.$numberToLocaleUpperCase = numberToLocaleUpperCase
格式化毫秒时间和如何全局使用工具方法
首先在工具类js文件中 定义并export工具方法,并将该方法注册为vue原型方法,如下
export const formart = (key, formart) => {
let foms = formart || 'yyyy-MM-dd';
var t = new Date(key);
var y = t.getFullYear();
var M = zero(t.getMonth() + 1);
var d = zero(t.getDate());
var h = zero(t.getHours());
var m = zero(t.getMinutes());
var s = zero(t.getSeconds());
foms = foms.replace('yyyy', y);
foms = foms.replace('MM', M);
foms = foms.replace('dd', d);
foms = foms.replace('hh', h);
foms = foms.replace('mm', m);
foms = foms.replace('ss', s);
if (key != '' && key != null && key) {
return foms;
} else {
return '';
}
function zero(val) {
let v = val < 10 ? '0' + val : val;
return v;
};
};
//格式化时间
Vue.prototype.$formart = formart
然后将该文件引入到main.js
import '@/assets/utils/function'
根据开始年月 + 月数 计算结束年月
/* 根据开始时间 和 期数 计算 结束时间 */
StartPeoirdForEndDate (start, peoird) {
/* start yyyy-MM */
let year = Number(start.split('-')[0])
let month = Number(start.split('-')[1])
let yearMonth = ''
let peoird2 = peoird
if (peoird % 12 != 0) {
if (peoird / 12 > 1) {
year += parseInt(peoird / 12)
peoird2 = peoird % 12
}
yearMonth = month + peoird2 <= 9 ?
year + '-' + '0' + (month + peoird2) :
month + peoird2 <= 12 ?
year + '-' + (month + peoird2) :
(month + peoird2 - 12) < 9 ?
(year + 1) + '-' + '0' + (month + peoird2 - 12) :
(year + 1) + '-' + (month + peoird2 - 12)
} else {
yearMonth = year + (peoird / 12) + '-' + month
}
return yearMonth
}
图片转base64
function image2Base64(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.width, img.height);
var dataURL = canvas.toDataURL("image/png");
return dataURL;
}
export const imageUrl2Base64 = (url) => {
return new Promise((resolve, reject) => {
let image = new Image()
image.crossOrigin = '*'
image.src = url
image.onload = () => {
resolve(image2Base64(image))
}
})
}
Vue.prototype.$imageUrl2Base64 = imageUrl2Base64
let url = 'https://files.mankoo.cn/group1/M00/25/93/rBGknV9Pa6iAHq7HAADkcOdlfpk.159904'
this.$imageUrl2Base64(url).then(base64 => {
this.bulletChatList.push({
msg: 'xxxxxxx',
headPortrait: base64
})
})
base64转file
dataURLtoFile (dataurl, filename) {
var arr= dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}
毫秒转天、小时、分、秒
formatDuring(mss) {
let days = Math.floor(mss / (1000 * 60 * 60 * 24))
let hours = Math.floor((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
let minutes = Math.floor((mss % (1000 * 60 * 60)) / (1000 * 60))
let seconds = Math.round((mss % (1000 * 60)) / 1000)
return days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒"
}
判断文本是否换行(超出宽度)用元素的高度对比lineHeight属性