常用方法封装(脱敏、复制、格式化日期)
1. 数字,文字脱敏
/**
* @Description 脱敏
* @param {string} str 字符串
* @param {number} beginStr 开始位置
* @param {number} endStr 结束位置
* @return {string} strVal 脱敏后字符串
*/
export function desensitization(str: string, beginStr: number, endStr: number) {
const len = str.length;
const leftStr = str.substring(0, beginStr);
const rightStr = str.substring(endStr, len);
let strVal = '';
let i = 0;
try {
for (i = 0; i < endStr - beginStr; i++) {
strVal = strVal + '*';
}
} catch (error) {}
strVal = leftStr + strVal + rightStr;
return strVal;
}
2.一键复制
clipboard官网
npm install clipboard
import Clipboard from 'clipboard';
<span class="copy" :data-clipboard-text="内容" @click="copy">
<i class="iconfont icon-shenqingliang"></i>
</span>
const copy = () => {
const clipboard = new Clipboard('.copy');
clipboard.on('success', function () {
message.success('复制成功');
clipboard.destroy();
});
clipboard.on('error', function () {
message.error('复制失败');
clipboard.destroy();
});
};
3.格式化日期
moment官网
npm i moment
import moment from "moment"
// 获取当前日期的前一天
let currentLastDate = moment().subtract(1, 'days').format('YYYY-MM-DD');
// 获取当前日期
let currentDate = moment().format('YYYY-MM-DD');