考试平台前端
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.
 
 
 
 
 

188 lines
5.9 KiB

import { _local } from "./util.db";
import { Message } from "element-ui";
import store from "@/store";
import Setting from '@/setting'
import axios from 'axios'
import api from '@/api'
// 文件后缀集合
const exts = {
video: 'mp4,3gp,mov,m4v,avi,dat,mkv,flv,vob,rmvb,rm,qlv',
audio: 'mp3,aac,ape,flac,wav,wma,amr,mid',
img: 'jpg,jpeg,png,gif,svg,psd',
doc: 'doc,docx,txt,xls,xlsx,csv,xml,ppt,pptx'
}
const util = {
local: _local,
// 筛选出有权限的站点
getSite (list) {
const result = []
const auth = store.state.auth.btns
list.map(e => {
auth.find(n => n.includes(e.siteName)) && result.push(e)
})
return result
},
//返回格式化时间,传参例如:"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;
},
// 传入文件后缀判断是否是视频
isVideo (ext) {
if (exts.video.includes(ext)) return true;
return false;
},
// 传入文件后缀判断是否是音频
isAudio (ext) {
if (exts.audio.includes(ext)) return true;
return false;
},
// 传入文件后缀判断是否是图片
isImg (ext) {
if (exts.img.includes(ext.toLowerCase())) return true;
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 });
},
// 阿拉伯数字转化为中文数字
arabicToChinese (num) {
const arr1 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
const arr2 = ['', '十', '百', '千', '万', '亿', '点', ''];
const a = `${num}`.replace(/(^0*)/g, '').split('.');
let k = 0;
let re = '';
for (let i = a[0].length - 1; i >= 0; i--) {
switch (k) {
case 0:
re = arr2[7] + re;
break;
case 4:
if (!new RegExp(`0{4}//d{${a[0].length - i - 1}}$`).test(a[0])) re = arr2[4] + re;
break;
case 8:
re = arr2[5] + re;
arr2[7] = arr2[5];
k = 0;
break;
default:
}
if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = arr1[0] + re;
if (a[0].charAt(i) != 0) re = arr1[a[0].charAt(i)] + arr2[k % 4] + re;
k++;
}
return num > 9 && num < 20 ? re.slice(1) : re;
},
// 阿拉伯数字转化为英文字母
numToLetter (num) {
let result = ''
if (num > 26) {
result += numberToLetter((num / 26) >> 0 - 1)
}
result += String.fromCharCode(65 + (num % 26))
return result
},
// 去掉html里的标签及空格
delTag (html) {
const el = document.createElement('div')
el.innerHTML = html
return el.innerText
},
// 循环去掉列表里html里的标签及空格
removeTag (list, prop = 'stem') {
list.map(e => {
const el = document.createElement('div')
el.innerHTML = e[prop]
e[prop] = el.innerText
})
return list
},
// 传入文件名获取文件后缀
getFileExt (fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
},
// 获取当前时间
getNow () {
return new Promise(async (resolve, reject) => {
const { data } = await axios.get(Setting.apiBaseURL + api.getCurrentTime, {
headers: {
token: _local.get(Setting.tokenKey)
}
})
resolve(new Date(data.currentTime))
})
},
getUuid () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
})
}
};
export default util;