|
|
|
@ -1,118 +1,124 @@ |
|
|
|
|
import Config from '@/config' |
|
|
|
|
import Cookie from 'js-cookie' |
|
|
|
|
|
|
|
|
|
export default { |
|
|
|
|
getCookie(cookie_name) { |
|
|
|
|
//获取cookie中指定key的value
|
|
|
|
|
var allcookies = document.cookie; //索引长度,开始索引的位置
|
|
|
|
|
var cookie_pos = allcookies.indexOf(cookie_name); // 如果找到了索引,就代表cookie存在,否则不存在
|
|
|
|
|
|
|
|
|
|
if (cookie_pos != -1) { |
|
|
|
|
// 把cookie_pos放在值的开始,只要给值加1即可
|
|
|
|
|
//计算取cookie值得开始索引,加的1为“=”
|
|
|
|
|
cookie_pos = cookie_pos + cookie_name.length + 1; //计算取cookie值得结束索引
|
|
|
|
|
var cookie_end = allcookies.indexOf(";", cookie_pos); |
|
|
|
|
|
|
|
|
|
if (cookie_end == -1) { |
|
|
|
|
cookie_end = allcookies.length; |
|
|
|
|
} //得到想要的cookie的值
|
|
|
|
|
var value = unescape(allcookies.substring(cookie_pos, cookie_end)); |
|
|
|
|
} |
|
|
|
|
return value; |
|
|
|
|
}, |
|
|
|
|
// 返回格式化时间,传参例如:"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 ("mp4,3gp,mov,m4v,avi,dat,mkv,flv,vob,rmvb,rm,qlv".includes(ext)) return true; |
|
|
|
|
return false; |
|
|
|
|
}, |
|
|
|
|
// 传入文件后缀判断是否是图片
|
|
|
|
|
isImg(ext) { |
|
|
|
|
if ("jpg,jpeg,png,gif,svg,psd".includes(ext)) return true; |
|
|
|
|
return false; |
|
|
|
|
}, |
|
|
|
|
// 传入文件后缀判断是否是pdf以外的文档
|
|
|
|
|
isDoc(ext) { |
|
|
|
|
if ("xls,xlsx,doc,docx,pdf,ppt,pptx".includes(ext)) return true; |
|
|
|
|
return false; |
|
|
|
|
}, |
|
|
|
|
// 传入文件名获取文件后缀
|
|
|
|
|
getFileExt(fileName) { |
|
|
|
|
return fileName.substring(fileName.lastIndexOf('.') + 1) |
|
|
|
|
}, |
|
|
|
|
// 下载文件
|
|
|
|
|
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() |
|
|
|
|
}, |
|
|
|
|
// 下载图片
|
|
|
|
|
downloadPic(fileName, src) { |
|
|
|
|
const canvas = document.createElement('canvas') |
|
|
|
|
const img = document.createElement('img') |
|
|
|
|
// 解决跨域 Canvas 污染问题
|
|
|
|
|
img.setAttribute('crossOrigin', 'anonymous') |
|
|
|
|
//将资源链接赋值过去,才能触发img.onload事件
|
|
|
|
|
img.src = src |
|
|
|
|
img.onload = function (e) { |
|
|
|
|
canvas.width = img.width |
|
|
|
|
canvas.height = img.height |
|
|
|
|
const context = canvas.getContext('2d') |
|
|
|
|
//绘制图片
|
|
|
|
|
context.drawImage(img, 0, 0, img.width, img.height) |
|
|
|
|
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height) |
|
|
|
|
//将canvas转base64码,然后创建一个a连接自动下载图片
|
|
|
|
|
canvas.toBlob((blob) => { |
|
|
|
|
const link = document.createElement('a') |
|
|
|
|
link.href = window.URL.createObjectURL(blob) |
|
|
|
|
link.download = fileName |
|
|
|
|
link.click() |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
// 传入文件名和数据,下载文件
|
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
// 小于0前面加上0
|
|
|
|
|
handleZero (num) { |
|
|
|
|
return num < 10 ? '0' + num : num |
|
|
|
|
// 返回格式化时间,传参例如:"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 ("mp4,3gp,mov,m4v,avi,dat,mkv,flv,vob,rmvb,rm,qlv".includes(ext)) return true; |
|
|
|
|
return false; |
|
|
|
|
}, |
|
|
|
|
// 传入文件后缀判断是否是图片
|
|
|
|
|
isImg (ext) { |
|
|
|
|
if ("jpg,jpeg,png,gif,svg,psd".includes(ext)) return true; |
|
|
|
|
return false; |
|
|
|
|
}, |
|
|
|
|
// 传入文件后缀判断是否是pdf以外的文档
|
|
|
|
|
isDoc (ext) { |
|
|
|
|
if ("xls,xlsx,doc,docx,pdf,ppt,pptx".includes(ext)) return true; |
|
|
|
|
return false; |
|
|
|
|
}, |
|
|
|
|
// 传入文件名获取文件后缀
|
|
|
|
|
getFileExt (fileName) { |
|
|
|
|
return fileName.substring(fileName.lastIndexOf('.') + 1) |
|
|
|
|
}, |
|
|
|
|
// 下载文件
|
|
|
|
|
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() |
|
|
|
|
}, |
|
|
|
|
// 下载图片
|
|
|
|
|
downloadPic (fileName, src) { |
|
|
|
|
const canvas = document.createElement('canvas') |
|
|
|
|
const img = document.createElement('img') |
|
|
|
|
// 解决跨域 Canvas 污染问题
|
|
|
|
|
img.setAttribute('crossOrigin', 'anonymous') |
|
|
|
|
//将资源链接赋值过去,才能触发img.onload事件
|
|
|
|
|
img.src = src |
|
|
|
|
img.onload = function (e) { |
|
|
|
|
canvas.width = img.width |
|
|
|
|
canvas.height = img.height |
|
|
|
|
const context = canvas.getContext('2d') |
|
|
|
|
//绘制图片
|
|
|
|
|
context.drawImage(img, 0, 0, img.width, img.height) |
|
|
|
|
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height) |
|
|
|
|
//将canvas转base64码,然后创建一个a连接自动下载图片
|
|
|
|
|
canvas.toBlob((blob) => { |
|
|
|
|
const link = document.createElement('a') |
|
|
|
|
link.href = window.URL.createObjectURL(blob) |
|
|
|
|
link.download = fileName |
|
|
|
|
link.click() |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
// 传入文件名和数据,下载文件
|
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
// 小于0前面加上0
|
|
|
|
|
handleZero (num) { |
|
|
|
|
return num < 10 ? '0' + num : num |
|
|
|
|
}, |
|
|
|
|
// 退出实验
|
|
|
|
|
exit () { |
|
|
|
|
let href = Config.isDev ? |
|
|
|
|
`http://${location.hostname}:8082/#/` : |
|
|
|
|
`${location.origin}${Cookie.get('admin-fromManager') ? |
|
|
|
|
'/admin' : |
|
|
|
|
(!Config.isTest) ? |
|
|
|
|
'' : |
|
|
|
|
'/student'}/#/` |
|
|
|
|
// 考核
|
|
|
|
|
if (Cookie.get('admin-assessmentId')) { |
|
|
|
|
href += `ass/list` |
|
|
|
|
} else if (Cookie.get('admin-competitionId')) { |
|
|
|
|
// 竞赛
|
|
|
|
|
href += `match/list` |
|
|
|
|
} else { |
|
|
|
|
// 练习
|
|
|
|
|
href += `station/preview?courseId=${Cookie.get('admin-courseId')}&curriculumName=${Cookie.get('admin-curriculumName') ? decodeURIComponent(Cookie.get('admin-curriculumName')) : 'python'}&mallId=${Cookie.get('admin-mallId')}` |
|
|
|
|
} |
|
|
|
|
location.href = href |
|
|
|
|
} |
|
|
|
|
} |