forked from integrated_whb/integrated_whb_vue
358 lines
9.3 KiB
JavaScript
358 lines
9.3 KiB
JavaScript
import { ElMessage } from "element-plus";
|
||
|
||
/**
|
||
* @description 计算序号
|
||
* @param {Object} pagination 分页数据对象
|
||
* @param {number | string} pagination.currentPage 当前页
|
||
* @param {number | string} pagination.pageSize 每页条数
|
||
* @param {number} index 当页数据的索引值
|
||
* @return {number} 序号
|
||
**/
|
||
export function serialNumber(pagination, index) {
|
||
return (pagination.currentPage - 1) * pagination.pageSize + (index + 1);
|
||
}
|
||
|
||
/**
|
||
* @description 字符串数组转数组
|
||
* @param {string} value 转换的字符串数组
|
||
* @return {Array} 转换后的数组
|
||
**/
|
||
export function toArrayString(value) {
|
||
// eslint-disable-next-line no-eval
|
||
return value ? eval(value).map(String) : [];
|
||
}
|
||
|
||
/**
|
||
* @description 判断文件后缀名是否符合
|
||
* @param {string} name 文件名字
|
||
* @param {string} suffix 文件后缀
|
||
* @return {boolean} 是否符合
|
||
**/
|
||
export function interceptTheSuffix(name, suffix) {
|
||
return (
|
||
name.substring(name.lastIndexOf("."), name.length).toLowerCase() ===
|
||
suffix.toLowerCase()
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @description 图片转base64
|
||
* @param {string} imgUrl 图片地址
|
||
* @return {Promise} Promise实例,then包含base64编码
|
||
**/
|
||
export function image2Base64(imgUrl) {
|
||
return new Promise((resolve) => {
|
||
const img = new Image();
|
||
img.src = imgUrl;
|
||
img.crossOrigin = "Anonymous";
|
||
img.onload = function () {
|
||
const canvas = document.createElement("canvas");
|
||
canvas.width = img.width;
|
||
canvas.height = img.height;
|
||
const ctx = canvas.getContext("2d");
|
||
ctx.drawImage(img, 0, 0, img.width, img.height);
|
||
const ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
|
||
resolve(canvas.toDataURL("image/" + ext));
|
||
};
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @description 判断图片是否可访问成功
|
||
* @param {string} imgUrl 图片地址
|
||
* @return {Promise} Promise实例
|
||
**/
|
||
export function checkImgExists(imgUrl) {
|
||
return new Promise((resolve, reject) => {
|
||
const ImgObj = new Image();
|
||
ImgObj.src = imgUrl;
|
||
ImgObj.onload = function (res) {
|
||
resolve(res);
|
||
};
|
||
ImgObj.onerror = function (err) {
|
||
reject(err);
|
||
};
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @description 获取数据类型
|
||
* @param {any} data 数据
|
||
* @return {string} 数据类型
|
||
**/
|
||
export function getDataType(data) {
|
||
return Object.prototype.toString.call(data).slice(8, -1);
|
||
}
|
||
|
||
/**
|
||
* @description 数组去重
|
||
* @param {Array<number,string>} arr 去重的数组
|
||
* @return {Array} 去重后的数组
|
||
**/
|
||
export function ArrayDeduplication(arr) {
|
||
return [...new Set(arr)];
|
||
}
|
||
|
||
/**
|
||
* @description 数组对象去重
|
||
* @param {Array} arr 去重的数组
|
||
* @param {string} name 去重的key
|
||
* @return {Array} 去重后的数组
|
||
**/
|
||
export function arrayObjectDeduplication(arr, name) {
|
||
const obj = {};
|
||
arr = arr.reduce(function (previousValue, currentValue) {
|
||
if (!obj[currentValue[name]]) {
|
||
obj[currentValue[name]] = true;
|
||
previousValue.push(currentValue);
|
||
}
|
||
return previousValue;
|
||
}, []);
|
||
return arr;
|
||
}
|
||
|
||
/**
|
||
* @description 查找字符串中指定的值第几次出现的位置
|
||
* @param {Array} str 查找的字符串数组
|
||
* @param {string} char 查找的值
|
||
* @param {number} num 第几次出现
|
||
* @return {number} 出现的位置
|
||
**/
|
||
export function findCharIndex(str, char, num) {
|
||
let index = str.indexOf(char);
|
||
for (let i = 0; i < num - 1; i++) {
|
||
index = str.indexOf(char, index + 1);
|
||
}
|
||
return index;
|
||
}
|
||
|
||
/**
|
||
* @description 生成指定两个值之间的随机数
|
||
* @param {number} min 最小值
|
||
* @param {number} max 最大值
|
||
* @return {number} 随机数
|
||
**/
|
||
export function randoms(min, max) {
|
||
return Math.random() * (max - min + 1) + min;
|
||
}
|
||
|
||
/**
|
||
* @description 千位分隔符
|
||
* @param {number | string} num 转换的值
|
||
* @return {string} 转换后的值
|
||
**/
|
||
export function numFormat(num) {
|
||
if (num) {
|
||
const numArr = num.toString().split(".");
|
||
const arr = numArr[0].split("").reverse();
|
||
let res = [];
|
||
for (let i = 0; i < arr.length; i++) {
|
||
if (i % 3 === 0 && i !== 0) {
|
||
res.push(",");
|
||
}
|
||
res.push(arr[i]);
|
||
}
|
||
res.reverse();
|
||
if (numArr[1]) {
|
||
res = res.join("").concat("." + numArr[1]);
|
||
} else {
|
||
res = res.join("");
|
||
}
|
||
return res;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description 验证是否为空
|
||
* @param {any} value 验证的值
|
||
* @return {boolean} 是否为空
|
||
**/
|
||
export function isEmpty(value) {
|
||
return (
|
||
value === undefined ||
|
||
value === null ||
|
||
(typeof value === "object" && Object.keys(value).length === 0) ||
|
||
(typeof value === "string" && value.trim().length === 0)
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @description 获取url参数
|
||
* @param {string} name 获取的key
|
||
* @return {string} 获取的值
|
||
**/
|
||
export function getUrlParam(name) {
|
||
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
|
||
const r = window.location.search.substr(1).match(reg);
|
||
if (r != null) return decodeURI(r[2]);
|
||
return "";
|
||
}
|
||
|
||
/**
|
||
* @description 数据分页
|
||
* @param {Array} list 分页的数组
|
||
* @param {number | string} currentPage 当前页
|
||
* @param {number | string} pageSize 每页条数
|
||
* @return {Array} 分页后的数组
|
||
**/
|
||
export function paging(list, currentPage, pageSize) {
|
||
return list.filter((item, index) => {
|
||
return (
|
||
index < +currentPage * +pageSize &&
|
||
index >= (+currentPage - 1) * +pageSize
|
||
);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @description 获取文件后缀
|
||
* @param {string} name 文件名
|
||
* @return {string} 文件后缀
|
||
**/
|
||
export function getFileSuffix(name) {
|
||
return name.substring(name.lastIndexOf(".") + 1);
|
||
}
|
||
|
||
/**
|
||
* @description 获取文件名称
|
||
* @param {string} name 文件地址
|
||
* @return {string} 文件名称
|
||
**/
|
||
export function getFileName(name) {
|
||
if (!name) return "";
|
||
return name.substring(name.lastIndexOf("/") + 1);
|
||
}
|
||
|
||
/**
|
||
* @description 读取txt文档
|
||
* @param {string} filePah 文档路径
|
||
* @return {resolve,string} 读取后的内容
|
||
**/
|
||
export function readTxtDocument(filePah) {
|
||
return new Promise((resolve) => {
|
||
const FILE_URL = import.meta.env.VITE_FILE_URL;
|
||
const file_url = FILE_URL + filePah;
|
||
const xhr = new XMLHttpRequest();
|
||
xhr.open("get", file_url, true);
|
||
xhr.responseType = "blob";
|
||
xhr.onload = function (event) {
|
||
const reader = new FileReader();
|
||
reader.readAsText(event.target.response, "GB2312");
|
||
reader.onload = function () {
|
||
resolve(reader.result);
|
||
};
|
||
};
|
||
xhr.send();
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @description 将秒转换成时分秒
|
||
* @param {string,number} second 需要转换的秒数
|
||
* @return {string} 转换后的时间
|
||
**/
|
||
export function secondConversion(second) {
|
||
if (!second) return 0;
|
||
const h = parseInt((second / 60 / 60) % 24, 10);
|
||
const m = parseInt((second / 60) % 60, 10);
|
||
const s = parseInt(second % 60, 10);
|
||
if (h) {
|
||
return h + "小时" + m + "分钟" + s + "秒";
|
||
} else {
|
||
if (m) {
|
||
return m + "分钟" + s + "秒";
|
||
} else {
|
||
return s + "秒";
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description 附件添加前缀
|
||
* @param {Array} list 附件数组
|
||
* @return {Array} 添加完的数组
|
||
**/
|
||
export function addingPrefixToFile(list) {
|
||
const FILE_URL = import.meta.env.VITE_FILE_URL;
|
||
for (let i = 0; i < list.length; i++) {
|
||
list[i].url = FILE_URL + list[i].FILEPATH;
|
||
list[i].name = getFileName(list[i].FILEPATH);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
/**
|
||
* @description 验证重复选择
|
||
* @param {Array} list 验证的数组
|
||
* @param {number} index 选择的索引
|
||
* @param {string} key 验证的字段
|
||
* @param {string} id 验证的值
|
||
**/
|
||
export function verifyDuplicateSelection(list, index, key, id) {
|
||
if (list.some((item) => item[key] === id)) {
|
||
ElMessage.warning("不能重复选择");
|
||
} else {
|
||
list[index][key] = id;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description 翻译状态
|
||
* @param {number | string} status 状态
|
||
* @param {Array} list 翻译的数组
|
||
* @return {string} 翻译后的状态
|
||
**/
|
||
export function translationStatus(status, list) {
|
||
for (let i = 0; i < list.length; i++) {
|
||
if (status === list[i].ID) {
|
||
return list[i].NAME;
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description 计算文件大小
|
||
* @param {number | string} size 文件kb
|
||
* @return {string} 计算后的文件大小
|
||
**/
|
||
export function calculateFileSize(size) {
|
||
return size > 1024
|
||
? (size / 1024 + "").substring(0, (size / 1024 + "").lastIndexOf(".") + 3) +
|
||
"MB"
|
||
: size + "KB";
|
||
}
|
||
|
||
/**
|
||
* @description 根据身份证号获取出生日期和性别
|
||
* @param {String} idCard 身份证号
|
||
* @return {Object} 出生日期和性别 date sex
|
||
**/
|
||
export function idCardGetDateAndGender(idCard) {
|
||
const reg =
|
||
/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
|
||
let sex = "";
|
||
let date = "";
|
||
if (reg.test(idCard)) {
|
||
const org_birthday = idCard.substring(6, 14);
|
||
const org_gender = idCard.substring(16, 17);
|
||
const birthday =
|
||
org_birthday.substring(0, 4) +
|
||
"-" +
|
||
org_birthday.substring(4, 6) +
|
||
"-" +
|
||
org_birthday.substring(6, 8);
|
||
const birthdays = new Date(birthday.replace(/-/g, "/"));
|
||
const Month = birthdays.getMonth() + 1;
|
||
let MonthDate;
|
||
const DayDate = birthdays.getDate();
|
||
let Day;
|
||
if (Month < 10) MonthDate = "0" + Month;
|
||
else MonthDate = Month;
|
||
if (DayDate < 10) Day = "0" + DayDate;
|
||
else Day = DayDate;
|
||
sex = org_gender % 2 === 1 ? "1" : "0";
|
||
date = birthdays.getFullYear() + "-" + MonthDate + "-" + Day;
|
||
}
|
||
return { sex, date };
|
||
}
|