Files
learning-system-portal/src/utils/tools.js
2022-10-25 19:47:57 +08:00

437 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 对象复制
*/
export const deepCopy = (obj) => {
return JSON.parse(JSON.stringify(obj));
};
/**截取名称部分,以/划分 */
export const cutFullName = (fullName,num) => {
let newName=fullName;
if(newName){
let names=newName.split('/');
if(names.length>1){
newName=names[1];
if(num==2){
if(names.length>2){
newName+="/"+names[2];
}
}
}
}
return newName;
};
/**截取机构名称路径namePath最后两段不包含第一个 */
export const cutOrgNamePath = (namePath) => {
let newName=namePath;
if(newName){
let names=newName.split('/');
let len=names.length;
//使用最后两们
if(len>1){
//newName=names[len-2]+'/'+names[len-1];
newName=names[1];
}
// if(names.length>1){
// newName=names[1];
// if(num==2){
// if(names.length>2){
// newName+="/"+names[2];
// }
// }
// }
}
return newName;
};
/**
* 获取url协议
* @param {Object} type
*/
export function getUrlPre(type) {
const isHttps='https:'==document.location.prototype ? true:false;
let isUrl='https';
if(isHttps){
isUrl='https';
}else{
isUrl='http';
}
return isUrl;
}
// 类型过滤
const contentTypeMaps = {
10: '视频',
20: '音频',
30: '图片',
40: '文档',
41: '图文',
50: 'scrom包',
52: '外链',
60: '作业',
61: '考试',
62: '评估',
90: '其它',
};
export function getType(type) {
let name = contentTypeMaps[type];
return name;
}
/**
* 根据文件类型后续不带点的。比如 mp4 转化为对应的资源数字
*/
export function toContentType(fileType) {
let docTypes=["pdf","doc","xls", "ppt","docx", "xlsx", "pptx"];
let type = null;
// ["doc", "xls", "ppt","docx", "xlsx", "pptx","png","txt", "pdf","jpg","gif","bmp","mp4","mp3"]
if (fileType === "mp4") {
type = 10;
} else if (fileType === "mp3") {
type = 20;
} else if (fileType === "jpg" ||fileType === "png" ||fileType === "gif") {
type = 30;
} else if (docTypes.indexOf(fileType)>-1){
type = 40;
} else {
type = 90;
}
return type;
}
// 10微课21在线课(直播)20:在线课( 录播)30:面授课40:混合式,
// export function courseType(type) {
// const maps = {
// 10: '微课',
// 21: '在线课(直播)',
// 20: '在线课(录播)',
// 30: '面授课',
// 40: '混合式',
// };
// return maps[type];
// }
export function courseType(type) {
const maps = {
10: '录播课',
21: '在线课(直播)',
20: '录播课',
30: '线下课',
40: '学习项目',
};
return maps[type];
}
/**
* 把日期格式化为显示时间yyyy-MM-dd HH:mm:ss
* @param {Object} date
*/
export function formatDate(date){
var year = date.getFullYear(),
month = date.getMonth() + 1,//月份是从0开始的
day = date.getDate(),
hour = date.getHours(),
min = date.getMinutes(),
sec = date.getSeconds();
if(month<10){month='0'+month}
if(day<10){day='0'+day}
if(hour<10){hour='0'+hour}
if(min<10){min='0'+min}
if(sec<10){sec='0'+sec}
var newTime = year + '-' +
month + '-' +
day + ' ' +
hour + ':' +
min + ':' +
sec;
return newTime;
}
/**
* 把日期格式化为显示时间yyyy-MM-dd
* @param {Object} date
*/
export function formatDateShort(date){
var year = date.getFullYear(),
month = date.getMonth() + 1,//月份是从0开始的
day = date.getDate(),
hour = date.getHours(),
min = date.getMinutes(),
sec = date.getSeconds();
if(month<10){month='0'+month}
if(day<10){day='0'+day}
if(hour<10){hour='0'+hour}
if(min<10){min='0'+min}
if(sec<10){sec='0'+sec}
var newTime = year + '-' + month + '-' + day;
return newTime;
}
export function numberToLetter(x){ //此方法和移到utils中去
let s = "";
while (x > 0) {
let m = x % 26;
m = m === 0 ? (m = 26) : m;
s = String.fromCharCode(96 + parseInt(m)) + s;
x = (x - m) / 26;
}
return s.toUpperCase();
}
export function testType(type) { //此方法移到tools中
let name = '';
switch (type) {
case 101:
name = '单选';
break;
case 102:
name = '多选';
break;
case 103:
name = '判断';
break;
}
return name;
}
export function examType(type) { //此方法移到tools中
let name = '';
switch (type) {
case 1:
name = '单选';
break;
case 2:
name = '多选';
break;
case 3:
name = '判断';
break;
}
return name;
}
export function correctJudgment(ditem) {
let judgment = false;
if(ditem.type == 101 || ditem.type == 103) {
ditem.options.forEach(item=>{
if(item.answer){
if(item.id == ditem.userAnswer) {
judgment = true;
} else{
judgment = false;
}
}
})
} else {
ditem.options.forEach(item=>{
if(item.answer == item.isCheck){
judgment = true;
}else{
judgment = false;
}
})
}
return judgment;
}
export function toScore(score) {
if (!score) {
return '0';
}
if((''+score).indexOf('.')>-1){
return score.toFixed(1);
}else{
return score+'.0';
}
}
export function toScoreTow(score) {// 返回两位小数
if (!score) {
return '0';
}
if((''+score).indexOf('.')>-1){
return score.toFixed(2);
}else{
return score+'.00';
}
}
/**
* 数组互换位置
* @param {array} arr 数组
* @param {number} index1 添加项目的位置
* @param {number} index2 删除项目的位置
* index1和index2分别是两个数组的索引值即是两个要交换元素位置的索引值如15就是数组中下标为1和5的两个元素交换位置
*/
export function swapArray(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
/**
* 头像文字
* @param {*} text
* @returns
*/
export function userAvatarText(text){
if(text){
let len=text.length;
if(len>2){
text=text.substring(len-2);
}
}
return text;
}
// u币key
const uTypeMaps = {
'PublishArticle': '发表文章',
'ViewArticle': '阅读文章',
'StudyCourse': '学习课程',
'PublishCourse': '发布课程',
'PulishQuestion': '发布问题',
'ReadQuestion': '阅读问题',
'AnswerQuestion': '回答问题',
'PublishNote': '发布笔记',
'PublishComment': '发表评论',
'ReplyComment': '回复评论',
'Praise': '点赞',
'ScoreCourse':'课程评分',
'Feedback':'意见反馈',
'VideoStudyDayStat':'试听学习时长',
'ViewCase':'阅读案例',
'ViewArticle':'阅读文章',
'DeleteQuestion':'删除问题',
'other':'其他'
};
export function getUType(type) {
let name = uTypeMaps[type];
return name;
}
/**此方法的意义,需要一起讨论*/
export function experienceValue(total) {
let data = {
start:'',
end:'',
endValue:null,
percentage:0,
total,
}
if(total < 300) {
data.start = 'LV1';
data.end = 'LV2';
data.endValue = 600;
data.percentage = parseFloat((total / 600) * 100);
return data;
}
if(total >= 300 && total < 600){
data.start = 'LV2';
data.end = 'LV3';
data.endValue = 1000;
data.percentage = parseFloat((total / 1000) * 1000);
return data;
}
if(total >= 600 && total < 1000){
data.start = 'LV3';
data.end = 'LV4';
data.endValue = 2000;
data.percentage = parseFloat((total / 2000) * 100);
return data;
}
if(total >= 1000 && total < 2000){
data.start = 'LV4';
data.end = 'LV5';
data.endValue = 4000;
data.percentage = parseFloat((total / 4000) * 4000);
return data;
}
if(total >= 2000 && total < 4000){
data.start = 'LV5';
data.end = 'LV6';
data.endValue = 8000;
data.percentage = parseFloat((total / 8000) * 100);
return data;
}
if(total >= 4000 && total < 8000){
data.start = 'LV6';
data.end = 'LV7';
data.endValue = 15000;
data.percentage = parseFloat((total / 15000) * 100);
return data;
}
if(total >= 8000 && total < 15000){
data.start = 'LV7';
data.end = 'LV8';
data.endValue = 20000;
data.percentage = parseFloat((total / 20000) * 100);
return data;
}
if(total >= 15000 && total < 20000){
data.start = 'LV8';
data.end = 'LV9';
data.endValue = 30000;
data.percentage = parseFloat((total / 30000) * 100);
return data;
}
if(total >= 20000 && total < 30000){
data.start = 'LV9';
data.end = 'LV10';
data.endValue = 30000;
data.percentage = parseFloat((total / 30000) * 100);
return data;
}
if(total >= 30000){
data.start = 'LV10';
data.end = '';
data.endValue = 90000;
data.percentage = parseFloat((total / 90000) * 100);
return data;
}
return data;
}
export function translate(field) {
let name = '';
switch (field) {
case 'total':name = '累计'; break;
case 'weeks':name = '本周'; break;
case 'months':name = '本月'; break;
case 'years':name = '本年'; break;
default:
break;
}
return name;
}
/**格式化人数的文档 ,学习人数,关注人数等*/
export function formatUserNumber(num) {
let rsNum =0;
if(num<5){return 0;}
if(num>=5 && num<= 10){return 10 +"+";}
if(num<95){
rsNum=Math.round((num)/10)*10;
return rsNum+'+';
}
if(num>94){
rsNum=Math.round((num+50)/100)*100;
return rsNum+'+';
}
return 0;
}
alert(formatUserNumber(99));
/**
* 将秒转换成小时
* @author wn
* @date 2022.10.18
* @param {Object} second 秒
* @@return 小时
*/
export function formatSecondToHour(second) {
var n = 1; // 保留小数位
second = Number(second);
var h = second / 3600;
h = h.toFixed(n);
return h;
}