Files
sgxt_web/src/utils/dict.js
2026-04-15 16:04:50 +08:00

220 lines
5.7 KiB
JavaScript

import { ref, toRefs, isRef } from "vue";
import { getSysDictByCode, fzdict } from "@/api/sysDict"; //引入封装数字字典接口
import { getLocalDic } from "@/utils/localDic/index.js";
/**
* 获取字典数据
*/
let list = [];
/** 是否取本地字典 (需要本地加载就加这里,不需要就删除) */
export function isLocalDict(dictCode) {
let localDicObj = {
D_GS_ZDR_YJDJ: true, // "岗哨系统重点人员预警等级"
D_BZ_XB: true, // 性别
D_GS_BQ_DJ: true, // "岗哨系统标签管理标签等级"
D_GS_SSYJ: true, // "岗哨系统四色预警"
D_BZ_SF: true, // "是否"
BD_BK_CLYJBQ: true // "车辆预警标签"
};
return localDicObj[dictCode];
}
export function getDict(...args) {
const res = ref({});
return (() => {
args.forEach((d, index) => {
res.value[d] = [];
// 本地字典拦截,如果本地字典存在,则使用本地字典,否则使用远程字典
if (isLocalDict(d) && getLocalDic(d)) {
res.value[d] = getLocalDic(d);
} else {
getSysDictByCode({
dictCode: d
}).then((result) => {
result = result || {};
result.itemList = Array.isArray(result.itemList)
? result.itemList
: [];
result.itemList.forEach((p) => {
p.label = p.zdmc;
p.value = p.dm;
p.id = p.dm;
p.elTagType = p.dictType;
if (p?.itemList && p.itemList?.length > 0) {
getChildren(p);
}
p.children = p.itemList;
});
res.value[d] = result.itemList;
//
});
}
});
return toRefs(res.value);
})();
}
export function getFzDict(...args) {
const res = ref({});
return (() => {
args.forEach((d, index) => {
res.value[d] = [];
// 本地字典拦截,如果本地字典存在,则使用本地字典,否则使用远程字典
if (isLocalDict(d) && getLocalDic(d)) {
res.value[d] = getLocalDic(d);
} else {
fzdict({
dictLabel: d
}).then((result) => {
result = result || {};
// result.itemList = Array.isArray(result.itemList) ? result.itemList : [];
// result.itemList.forEach(p => {
// p.label = p.itemName;
// p.value = p.itemValue;
// p.id = p.itemValue;
// p.elTagType = p.dictType;
// if (p?.itemList && p.itemList?.length > 0) {
// getChildren(p);
// }
// p.children = p.itemList;
// });
// console.log(res.value);
res.value[d] = result;
});
}
});
// 使用toRefs确保返回的是响应式对象
return toRefs(res.value);
})();
}
export function getChildren(item) {
item.label = item.zdmc;
item.value = item.dm;
item.id = item.dm;
if (item.itemList && item.itemList.length > 0) {
item.itemList.forEach((v) => {
getChildren(v);
});
}
item.children = item.itemList;
}
/**
* 设置级联选择器回显
* @param {*} id 选中ID
* @param {*} array 级联数据树
* @param {*} childDeptList 子集变量
*/
export function setCascader(id, array, childDeptList = "childDeptList", fun) {
if (array) {
array.forEach((item) => {
if (item.childDeptList && item.id != id) {
setCascader(id, item.childDeptList, childDeptList, fun);
} else if (item.childDeptList && item.id == id) {
fun(item);
} else if (!item.childDeptList && item.id == id) {
fun(item);
}
});
}
}
/**
* 当type=1时获取出生日期,type=2时获取性别,type=3时获取年龄 all
* @param {*} IdCard
* @param {*} type
* @returns
*/
export function IdCard(IdCard, type) {
let user = {
birthday: "",
sex: "",
age: ""
};
if (type === 1 || type == "all") {
//获取出生日期
let birthday =
IdCard.substring(6, 10) +
"-" +
IdCard.substring(10, 12) +
"-" +
IdCard.substring(12, 14);
if (type == "all") {
user.birthday = birthday;
} else {
return birthday;
}
}
if (type === 2 || type == "all") {
//获取性别
if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
if (type == "all") {
user.sex = "男";
} else {
return "男";
}
} else {
if (type == "all") {
user.sex = "女";
} else {
return "女";
}
}
}
if (type === 3 || type == "all") {
//获取年龄
var ageDate = new Date();
var month = ageDate.getMonth() + 1;
var day = ageDate.getDate();
var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1;
if (
IdCard.substring(10, 12) < month ||
(IdCard.substring(10, 12) === month && IdCard.substring(12, 14) <= day)
) {
age++;
}
if (age <= 0) {
age = 1;
}
if (type == "all") {
user.age = age;
} else {
return age;
}
}
return user;
}
/**
*翻译字典数据
* @export
* @param {*} dm
* @param {*} array
*/
export function getDictValue(dm, array) {
let item = array.value.find((item) => {
if (item.value) {
return item.value == dm;
} else if (item.dm) {
return item.dm == dm;
}
});
return item ? item.label : "";
}
/** 获取多个字典值(一个值也可以) 字典内容 value-label
* @param {String} 要查的值
* @param {Array} dict 字典内容
*/
export function getMultiDictVal(values, dict) {
if (typeof values === "string" && values?.length) values = values.split(",");
if (!Array.isArray(values)) return "";
if (isRef(dict)) dict = dict.value;
if (!Array.isArray(dict)) return "";
return values
.map((v) => {
const item = dict.find((item) => item.value === v);
return item ? item.label : v;
})
.join(",");
}