|
@@ -0,0 +1,174 @@
|
|
|
+import axios from "axios";
|
|
|
+/**
|
|
|
+ * @description 防抖
|
|
|
+ * @param func 功能函数(即要防抖的函数)
|
|
|
+ * @param delay 延时时间
|
|
|
+ * @param thisArg 功能函数内this的对象
|
|
|
+ */
|
|
|
+export function debounce(func: Function, delay: number, thisArg?: any) {
|
|
|
+ let timer: number;
|
|
|
+ return function (...args: any[]) {
|
|
|
+ if (timer) {
|
|
|
+ window.clearTimeout(timer);
|
|
|
+ }
|
|
|
+ timer = window.setTimeout(function () {
|
|
|
+ func.apply(thisArg, args);
|
|
|
+ }, delay);
|
|
|
+ };
|
|
|
+}
|
|
|
+/**
|
|
|
+ * @description 节流(原理同上)
|
|
|
+ * @param func 功能函数(即要节流的函数)
|
|
|
+ * @param delay 延时时间
|
|
|
+ * @param thisArg 功能函数内this的对象
|
|
|
+ */
|
|
|
+export function throttle(func: Function, delay: number, thisArg?: any) {
|
|
|
+ let timeStamp: number;
|
|
|
+ return function (...args: any[]) {
|
|
|
+ const nowTimeStamp = Date.now();
|
|
|
+ if (!timeStamp || nowTimeStamp - timeStamp >= delay) {
|
|
|
+ func.apply(thisArg, args);
|
|
|
+ timeStamp = nowTimeStamp;
|
|
|
+ }
|
|
|
+ };
|
|
|
+}
|
|
|
+/**
|
|
|
+ * @description vite引入图片
|
|
|
+ * @param {string} name ../assets/images 后面的文件路径+文件名后缀
|
|
|
+ * @returns {string} 返回处理后的文件地址
|
|
|
+ */
|
|
|
+export function getImageUrl(name: string) {
|
|
|
+ return new URL(`../assets/images/${name}`, import.meta.url).href;
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 手机号脱敏处理
|
|
|
+ * @param { string} phoneNumber 手机号码
|
|
|
+ * @returns {string} 返回处理后的手机号码
|
|
|
+ */
|
|
|
+export function desensitizationPhone(phoneNumber: string) {
|
|
|
+ if (!phoneNumber) return '';
|
|
|
+ return phoneNumber.replace(/^(.{3})(\d+)(.{4})$/, '$1****$2');
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 姓名脱敏处理
|
|
|
+ * @param {string} name 姓名
|
|
|
+ * @returns {string} 返回处理后的姓名
|
|
|
+ */
|
|
|
+export function desensitizationName(name: string) {
|
|
|
+ if (!name) return '';
|
|
|
+ const len = name.length;
|
|
|
+ return len <= 3
|
|
|
+ ? '*' + name.substring(1, len)
|
|
|
+ : len > 3 && len <= 6
|
|
|
+ ? '**' + name.substring(2, len)
|
|
|
+ : len > 6
|
|
|
+ ? name.substring(0, 2) + '****' + name.substring(6, len)
|
|
|
+ : '';
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 检查文件类型
|
|
|
+ * @param {string} fileValue 姓名
|
|
|
+ * @returns {string} 返回处理后的姓名
|
|
|
+ */
|
|
|
+export function checkFile(fileValue: string): string {
|
|
|
+ let index = fileValue.lastIndexOf('.'); //(考虑严谨用lastIndexOf(".")得到)得到"."在第几位
|
|
|
+ let fileValueSuffix = fileValue.substring(index); //截断"."之前的,得到后缀
|
|
|
+ if (/(.*)\.(mp4|avi|wmv|MP4|AVI|WMV)$/.test(fileValueSuffix)) {
|
|
|
+ //根据后缀,判断是否符合视频格式
|
|
|
+ return 'video';
|
|
|
+ } else if (/(.*)\.(jpg|JPG|bmp|BMP|mpg|MPG|mpeg|MPEG|tis|TIS|svg|png|jpeg|webp)$/.test(fileValueSuffix)) {
|
|
|
+ //根据后缀,判断是否符合图片格式
|
|
|
+ return 'image';
|
|
|
+ } else if (/(.*)\.(xls|XLS|xlsx|XLSX)$/.test(fileValueSuffix)) {
|
|
|
+ //根据后缀,判断是否符合OFFICE格式
|
|
|
+ return 'xls';
|
|
|
+ } else if (/(.*)\.(doc|DOC|docx|DOCX)$/.test(fileValueSuffix)) {
|
|
|
+ // 文档类型
|
|
|
+ return 'doc';
|
|
|
+ } else if (/(.*)\.(pdf|PDF)$/.test(fileValueSuffix)) {
|
|
|
+ // PDF
|
|
|
+ return 'pdf';
|
|
|
+ } else if (/(.*)\.(PPT|PPTX|ppt|pptx)$/.test(fileValueSuffix)) {
|
|
|
+ // ppt
|
|
|
+ return 'ppt';
|
|
|
+ }
|
|
|
+ return 'file';
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 传入类型返回对应的文件类型图标
|
|
|
+ * @param {string} file 文件名
|
|
|
+ * @returns {string} 返回处理后的图标
|
|
|
+ */
|
|
|
+export function fileType(file: string): string {
|
|
|
+ switch (file) {
|
|
|
+ case 'video':
|
|
|
+ return 'ele-VideoCamera';
|
|
|
+ case 'image':
|
|
|
+ return 'ele-Picture';
|
|
|
+ case 'xls':
|
|
|
+ return 'iconfont icon-excel';
|
|
|
+ case 'doc':
|
|
|
+ return 'ele-Document';
|
|
|
+ case 'pdf':
|
|
|
+ return 'iconfont icon-pdf';
|
|
|
+ case 'ppt':
|
|
|
+ return 'iconfont icon-ppt';
|
|
|
+ default:
|
|
|
+ return 'ele-Document';
|
|
|
+ }
|
|
|
+}
|
|
|
+/**
|
|
|
+ * @description 生成guid
|
|
|
+ * @returns {string} 返回guid
|
|
|
+ */
|
|
|
+export function guid(): string {
|
|
|
+ const S4 = (): string => {
|
|
|
+ return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
|
|
+ };
|
|
|
+ return S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4();
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 根据id排除自己(树形结构选择 修改时用)
|
|
|
+ * @param {any} arr 需要排除的数组
|
|
|
+ * @param {string} id 当前id
|
|
|
+ * @returns {arr} 返回排除后的数组
|
|
|
+ */
|
|
|
+export function excludeSelfById(arr: Array<any>, id: string) {
|
|
|
+ if (!arr) return [];
|
|
|
+ return arr.filter((v: any) => {
|
|
|
+ if (v.id === id) return false;
|
|
|
+ if (v.children && v.children.length) {
|
|
|
+ v.children = excludeSelfById(v.children, id);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+}
|
|
|
+/**
|
|
|
+ * @description 下载文件
|
|
|
+ * @param src 文件地址
|
|
|
+ * @param filename 文件名
|
|
|
+ */
|
|
|
+export function downloadFile(src: string, filename: string) {
|
|
|
+ if(!src) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let fileName: string = filename || '' // 文件名
|
|
|
+ axios({
|
|
|
+ method: 'get',
|
|
|
+ url: src,
|
|
|
+ responseType: 'blob',
|
|
|
+ headers: { 'content-type': 'audio/mpeg' },
|
|
|
+ }).then((res: any) => {
|
|
|
+ let blob:Blob = new Blob([res.data], { type: res.data.type }) // 创建blob 设置blob文件类型 data 设置为后端返回的文件(例如mp3,jpeg) type:这里设置后端返回的类型 为 mp3
|
|
|
+ let down: HTMLAnchorElement = document.createElement('a') // 创建A标签
|
|
|
+ let href:string = window.URL.createObjectURL(blob) // 创建下载的链接
|
|
|
+ down.href = href // 下载地址
|
|
|
+ down.download = fileName // 下载文件名
|
|
|
+ document.body.appendChild(down)
|
|
|
+ down.click() // 模拟点击A标签
|
|
|
+ document.body.removeChild(down) // 下载完成移除元素
|
|
|
+ window.URL.revokeObjectURL(href) // 释放blob对象
|
|
|
+ }).catch(function (error) {
|
|
|
+ console.log(error)
|
|
|
+ })
|
|
|
+}
|