城阳pc工作端前端代码
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.

207 lines
4.8 KiB

3 years ago
import store from "@/js/store";
import JSEncrypt from "jsencrypt"; //引入加密
4 years ago
/**
* 获取字典数据列表
* @param dictType 字典类型
*/
export function getDictDataList(dictType) {
3 years ago
const type = window.SITE_CONFIG["dictList"].find(
4 years ago
(element) => element.dictType === dictType
3 years ago
);
4 years ago
if (type) {
3 years ago
return type.dataList;
4 years ago
} else {
3 years ago
return [];
4 years ago
}
}
/**
* 获取字典名称
* @param dictType 字典类型
* @param dictValue 字典值
*/
export function getDictLabel(dictType, dictValue) {
3 years ago
const type = window.SITE_CONFIG["dictList"].find(
4 years ago
(element) => element.dictType === dictType
3 years ago
);
4 years ago
if (type) {
const val = type.dataList.find(
(element) => element.dictValue === dictValue.toString()
3 years ago
);
4 years ago
if (val) {
3 years ago
return val.dictLabel;
4 years ago
} else {
3 years ago
return dictValue;
4 years ago
}
} else {
3 years ago
return dictValue;
4 years ago
}
}
/**
* 清除登录信息
*/
export function clearLoginInfo() {
3 years ago
store.commit("resetStore");
localStorage.removeItem("token");
window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] = false;
4 years ago
}
/**
* 获取uuid
*/
export function getUUID() {
3 years ago
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
return (c === "x" ? (Math.random() * 16) | 0 : "r&0x3" | "0x8").toString(
16
);
});
4 years ago
}
/**
* 获取svg图标(id)列表
*/
export function getIconList() {
3 years ago
var res = [];
document.querySelectorAll("svg symbol").forEach((item) => {
res.push(item.id);
});
return res;
4 years ago
}
/**
* 树形数据转换
* @param {*} data
* @param {*} id
* @param {*} pid
*/
3 years ago
export function treeDataTranslate(data, id = "id", pid = "pid") {
var res = [];
var temp = {};
4 years ago
for (var i = 0; i < data.length; i++) {
3 years ago
temp[data[i][id]] = data[i];
4 years ago
}
for (var k = 0; k < data.length; k++) {
if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
3 years ago
res.push(data[k]);
continue;
4 years ago
}
3 years ago
if (!temp[data[k][pid]]["children"]) {
temp[data[k][pid]]["children"] = [];
4 years ago
}
3 years ago
temp[data[k][pid]]["children"].push(data[k]);
data[k]["_level"] = (temp[data[k][pid]]._level || 0) + 1;
4 years ago
}
3 years ago
return res;
4 years ago
}
4 years ago
// 时间格式化
export function dateFormats(fmt, date) {
3 years ago
let ret;
const _date = new Date(date);
4 years ago
const opt = {
3 years ago
"Y+": _date.getFullYear().toString(), // 年
"m+": (_date.getMonth() + 1).toString(), // 月
"d+": _date.getDate().toString(), // 日
"H+": _date.getHours().toString(), // 时
"M+": _date.getMinutes().toString(), // 分
"S+": _date.getSeconds().toString(), // 秒
4 years ago
// 有其他格式化字符需求可以继续添加,必须转化成字符串
3 years ago
};
4 years ago
for (const k in opt) {
3 years ago
ret = new RegExp("(" + k + ")").exec(fmt);
4 years ago
if (ret) {
3 years ago
fmt = fmt.replace(
ret[1],
ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
);
4 years ago
}
}
3 years ago
return fmt;
4 years ago
}
4 years ago
// 根据身份证计算出生日期,性别,年龄
export function computedCard(idCard) {
let sex = null;
let birth = null;
let myDate = new Date();
let month = myDate.getMonth() + 1;
let day = myDate.getDate();
let age = 0;
3 years ago
if (idCard.length === 18) {
4 years ago
age = myDate.getFullYear() - idCard.substring(6, 10) - 1;
3 years ago
sex = idCard.substring(16, 17);
birth =
idCard.substring(6, 10) +
"-" +
idCard.substring(10, 12) +
"-" +
idCard.substring(12, 14);
if (
idCard.substring(10, 12) < month ||
(idCard.substring(10, 12) === month && idCard.substring(12, 14) <= day)
)
age++;
4 years ago
}
3 years ago
if (idCard.length === 15) {
4 years ago
age = myDate.getFullYear() - idCard.substring(6, 8) - 1901;
3 years ago
sex = idCard.substring(13, 14);
birth =
"19" +
idCard.substring(6, 8) +
"-" +
idCard.substring(8, 10) +
"-" +
idCard.substring(10, 12);
if (
idCard.substring(8, 10) < month ||
(idCard.substring(8, 10) === month && idCard.substring(10, 12) <= day)
)
age++;
4 years ago
}
3 years ago
if (sex % 2 === 0)
sex = "0"; // 性别代码 1代表男,0代表女,暂时不涉及其他类型性别
else sex = "1";
return { age, sex, birth };
4 years ago
}
3 years ago
// 将数组分成小块数组的集合
export function spliceIntoChunks(arr, chunkSize) {
const res = [];
while (arr.length > 0) {
3 years ago
const chunk = arr.splice(0, chunkSize);
res.push(chunk);
3 years ago
}
return res;
}
3 years ago
// 获取当前时间 如果有指定time则按指定的来
3 years ago
export function getCurrentDate(hour, min, sec) {
console.log(hour, min, sec);
let date = new Date();
3 years ago
if (hour) {
date.setHours(hour);
3 years ago
console.log(hour);
}
if (min !== undefined) {
date.setMinutes(min);
3 years ago
console.log(min);
}
if (sec !== undefined) {
date.setSeconds(sec);
3 years ago
console.log(sec);
}
return date;
}
4 years ago
3 years ago
// 加密
export function encryptedData(key, data) {
// 新建JSEncrypt对象
let encryptor = new JSEncrypt();
// 设置公钥
encryptor.setPublicKey(key);
// 加密数据
return encryptor.encrypt(data);
}