@pit-front-end/utils
Version:
湖南创智艾泰克科技有限公司
252 lines (251 loc) • 8.27 kB
JavaScript
import { getGetRouter as c } from "./global.js";
import { cloneDeep as p } from "lodash";
import h from "dayjs";
let f = {
state: {}
};
const u = () => {
var e, t;
return (t = (e = f.state) == null ? void 0 : e.value) == null ? void 0 : t.user;
}, _ = (e) => {
f = e;
}, d = {
hasCode: {
btn_add: "btn_add",
btn_edit: "btn_edit",
btn_detail: "btn_detail",
btn_remove: "btn_remove",
btn_batchRemove: "btn_batchRemove",
btn_copy: "btn_copy",
btn_upload: "btn_upload",
btn_download: "btn_download",
btn_more: "btn_more",
btn_grading: "btn_grading",
btn_password: "btn_password",
btn_menu: "btn_menu",
btn_relation: "btn_relation",
// 关联按钮
btn_batchXZ: "btn_batchXZ",
// 批量下载
btn_class: "btn_class",
// 分类管理
btn_reduction: "btn_reduction",
// 还原
btn_del: "btn_del",
// 删除
btn_autoSC: "btn_autoSC",
// 自动生成
btn_batchFB: "btn_batchFB",
// 批量封版
btn_sum: "btn_sum",
//汇总
btn_unlock: "btn_unlock",
//解锁
btn_estimate: "btn_estimate"
//关联概算条目
},
hasP(e) {
var a;
const t = u().permissionList, n = ((a = c()) == null ? void 0 : a.currentRoute.value).meta.modelId || "";
if (!n) return !1;
const r = t.filter((l) => l.modelId === n);
if (!r.length) return !1;
const o = r[0] && r[0].column ? r[0].column : [];
return o.length ? !!o.some(
(l) => l.enCode === e
) : !1;
},
hasFormP(e) {
var a;
const t = u().permissionList, n = ((a = c()) == null ? void 0 : a.currentRoute.value).meta.modelId || "";
if (!n) return !1;
const r = t.filter((l) => l.modelId === n);
if (!r.length) return !1;
const o = r[0] && r[0].form ? r[0].form : [];
return o.length ? !!o.some((l) => l.enCode === e) : !1;
},
hasBtnP(e) {
var a;
const t = u().permissionList, n = ((a = c()) == null ? void 0 : a.currentRoute.value).meta.modelId || "";
if (!n) return !1;
const r = t.filter((l) => l.modelId === n);
if (!r.length) return !1;
const o = r[0] && r[0].button ? r[0].button : [];
return o.length ? !!o.some((l) => l.enCode === e) : !1;
},
hasFlow(e, t, s, n = "writeable") {
let r = !1;
const o = e.approvalStatus === "draft" || e.approvalStatus === "reject" || e.approvalStatus === "withdraw" || e.approvalStatus === "termination" || e.approvalStatus === null || e.approvalStatus === "";
return t ? e[n] !== void 0 ? o && e[n] ? r = !0 : r = !1 : r = o : r = !0, r;
},
toDate(e, t) {
if (t = t || "yyyy-MM-dd HH:mm", !e) return "";
let s = e;
typeof e == "string" ? e.indexOf("/Date(") > -1 ? s = new Date(parseInt(e.replace("/Date(", "").replace(")/", ""), 10)) : s = new Date(
Date.parse(e.replace(/-/g, "/").replace("T", " ").split(".")[0])
) : s = new Date(e);
const n = {
"M+": s.getMonth() + 1,
"d+": s.getDate(),
"h+": s.getHours(),
"H+": s.getHours(),
"m+": s.getMinutes(),
"s+": s.getSeconds(),
"q+": Math.floor((s.getMonth() + 3) / 3),
S: s.getMilliseconds()
};
/(y+)/.test(t) && (t = t.replace(
RegExp.$1,
(s.getFullYear() + "").substr(4 - RegExp.$1.length)
));
for (const r in n)
new RegExp("(" + r + ")").test(t) && (t = t.replace(
RegExp.$1,
RegExp.$1.length === 1 ? n[r] : ("00" + n[r]).substr(("" + n[r]).length)
));
return t;
},
/**
* 日期格式化
* @param time
* @param pattern
* @returns
*/
parseTime(e, t) {
if (arguments.length === 0 || !e)
return null;
const s = t || "{y}-{m}-{d} {h}:{i}:{s}";
let n;
typeof e == "object" ? n = e : (typeof e == "string" && /^[0-9]+$/.test(e) ? e = parseInt(e) : typeof e == "string" && (e = e.replace(new RegExp(/-/gm), "/").replace("T", " ").replace(new RegExp(/\.[\d]{3}/gm), "")), typeof e == "number" && e.toString().length === 10 && (e = e * 1e3), n = new Date(e));
const r = {
y: n.getFullYear(),
m: n.getMonth() + 1,
d: n.getDate(),
h: n.getHours(),
i: n.getMinutes(),
s: n.getSeconds(),
a: n.getDay()
};
return s.replace(/{(y|m|d|h|i|s|a)+}/g, (i, a) => {
let l = r[a];
return a === "a" ? ["日", "一", "二", "三", "四", "五", "六"][l] : (i.length > 0 && l < 10 && (l = "0" + l), l || 0);
});
},
/**
* 返回项目路径
* @param p
* @returns
*/
getNormalPath(e) {
if (e.length === 0 || !e || e == "undefined")
return e;
const t = e.replace("//", "/");
return t[t.length - 1] === "/" ? t.slice(0, t.length - 1) : t;
},
// 基于dayjs日期格式化,时间戳(毫秒)转日期
dateFormat(e, t) {
return t = t || "YYYY-MM-DD HH:mm", e ? h(e).format(t) : "";
},
formatFloat(e, t, s, n, r) {
let o = "" + e;
return o = o.replace(/[^\d.-]/g, "").replace(/^\./g, "").replace(/\.{2,}/g, ".").replace(".", "$#$").replace(/\./g, "").replace("$#$", "."), r && (o = o.slice(0, 1) + o.slice(1).replace(/-/g, "")), t === 2 ? o = o.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3") : t === 3 ? o = o.replace(/^(\-)*(\d+)\.(\d\d\d).*$/, "$1$2.$3") : t === 4 ? o = o.replace(/^(\-)*(\d+)\.(\d\d\d\d).*$/, "$1$2.$3") : o = o.replace(/[^\d]/g, ""), o.indexOf(".") < 0, !isNaN(parseFloat(o)) && o > s && (o = s), !isNaN(parseFloat(o)) && o < n && (o = n), o;
},
treeToArray(e, t = []) {
return p(e).forEach((n) => {
const { children: r } = n;
if (r && r.length)
return t.push(n), d.treeToArray(r, t);
t.push(n);
}), t.forEach((n) => {
n.children = null, n.hasChildren = !1;
}), t;
},
// 数组转树
arrayToTree(e) {
const t = {}, s = [];
e.forEach((r) => {
t[r.id] = { ...r, children: [] };
}), e.forEach((r) => {
const o = r.parentId, i = t[r.id];
if (o === "0")
s.push(i);
else {
const a = t[o];
a && a.children.push(i);
}
});
function n(r) {
r.forEach((o) => {
o.children && o.children.length === 0 ? delete o.children : o.children && n(o.children);
});
}
return n(s), s;
},
handleDateStr(e) {
return new Date(e).toLocaleString().replaceAll("/", "-");
},
amountThousandsConversion(e) {
return e && ((e + "").includes(".") ? (e + "").replace(/\d{1,3}(?=(\d{3})+(\.))/g, (t) => t + ",") : (
// 就是说1-3位后面一定要匹配3位
(e + "").replace(/\d{1,3}(?=(\d{3})+$)/g, (t) => t + ",")
));
},
adminAuthority() {
return localStorage.getItem("administrator") === "true";
},
isProjectManager() {
return localStorage.getItem("isPermission") === "true";
},
iterationArr(e, t, s = "children") {
e == null || e.map((n) => {
t(n), d.iterationArr(n[s], t);
});
},
/**
* @desc 在系统内部进行跳转
* @param url {string} 跳转地址,如 /gatewayHom 必须为相对地址
* @param target {string} _target或_self
* @param cb {function} 跳转前回调,参数url
*/
goToRouteWithThePlatform(e, t, s, n = 1e3) {
e[0] === "/" && (e = e.slice(1)), setTimeout(() => {
typeof s == "function" && s(e);
const r = /#/.test(location.href);
window.open(
r ? location.href.replace(/#.*/, `#/${e}`) : "/" + e,
t
);
}, n);
},
getBlobFileName(e) {
let t = decodeURIComponent(e.headers["content-disposition"]) || "";
return t ? (t = t == null ? void 0 : t.split("=")[1], t) : "";
},
deepClone(e) {
if (!e && typeof e != "object")
throw new Error("error arguments");
const t = e.constructor === Array ? [] : {};
return Object.keys(e).forEach((s) => {
e[s] && typeof e[s] == "object" ? t[s] = this.deepClone(e[s]) : t[s] = e[s];
}), t;
},
/**
* @desc 检测图片链接是否有效
* @param imgUrl {string} 图片地址
* @return 返回一个promise 根据promise状态知晓地址图片地址是否失效
*/
checkImgIsVaild(e) {
return new Promise(function(t, s) {
const n = new Image();
n.src = e, n.onload = function(r) {
t(r);
}, n.onerror = function(r) {
s(r);
};
});
}
};
export {
_ as initUserStore,
d as pit
};