粒子研究院后台前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.2 KiB

import { _local } from "./util.db";
3 years ago
import { Message } from "element-ui";
import store from "@/store";
2 years ago
import Setting from '@/setting'
import JSEncrypt from "jsencrypt"
3 years ago
// 文件后缀集合
const exts = {
1 year ago
video: 'mp4,mov',
audio: 'mp3',
img: 'jpg,png,gif',
doc: 'doc,docx,xls,xlsx'
3 years ago
}
const util = {
local: _local,
3 years ago
// 筛选出有权限的站点
getSite(list) {
2 years ago
// 开启了动态路由的需要根据权限筛选站点
if (Setting.dynamicRoute) {
const result = []
const auth = store.state.auth.btns
list.map(e => {
auth.find(n => n.includes(e.siteName)) && result.push(e)
})
return result
} else {
return list
}
3 years ago
},
//返回格式化时间,传参例如:"yyyy-MM-dd hh:mm:ss"
formatDate(fmt, date) {
var date = date ? date : new Date();
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
},
3 years ago
// 传入文件后缀判断是否是视频
isVideo(ext) {
if (exts.video.includes(ext)) return true;
return false;
},
// 传入文件后缀判断是否是音频
isAudio(ext) {
if (exts.audio.includes(ext)) return true;
return false;
},
// 传入文件后缀判断是否是图片
isImg(ext) {
3 years ago
if (exts.img.includes(ext.toLowerCase())) return true;
3 years ago
return false;
},
// 传入文件后缀判断是否是pdf以外的文档
isDoc(ext) {
if (exts.doc.includes(ext)) return true;
return false;
},
// 传入文件名和路径,下载图片视频,支持跨域,a标签加download不支持跨域
downloadFile(fileName, url) {
var x = new XMLHttpRequest();
x.open("GET", url, true);
x.responseType = "blob";
x.onload = function(e) {
var url = window.URL.createObjectURL(x.response);
var a = document.createElement("a");
a.href = url;
a.download = fileName;
a.click();
};
x.send();
},
// 传入文件名和数据,下载文件
downloadFileDirect(fileName, data) {
if ("download" in document.createElement("a")) { // 非IE下载
const elink = document.createElement("a");
elink.download = fileName;
elink.style.display = "none";
elink.href = URL.createObjectURL(data);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
} else { // IE10+下载
navigator.msSaveBlob(data, fileName);
}
},
// 成功提示
successMsg(message, duration = 3000) {
Message.closeAll();
return Message.success({ message, showClose: true, offset: (document.documentElement.clientHeight - 40) / 2, duration });
},
// 警告提示
warningMsg(message, duration = 3000) {
Message.closeAll();
return Message.warning({ message, showClose: true, offset: (document.documentElement.clientHeight - 40) / 2, duration });
},
// 错误提示
errorMsg(message, duration = 3000) {
Message.closeAll();
return Message.error({ message, showClose: true, offset: (document.documentElement.clientHeight - 40) / 2, duration });
},
// rsa加密
rsa(data) {
const jse = new JSEncrypt(); // 实例化一个 jsEncrypt 对象
jse.setPublicKey(Setting.publicKey); //配置公钥
return jse.encrypt(JSON.stringify(data))
},
3 years ago
};
export default util;