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.
 
 
 
 
 
 

217 lines
7.0 KiB

// 设置购物车数量
const setGoodsNum = (data) => {
$globalStore.useCommon.SET_GOODS_NUM(data);
};
// 判断是否还是一个对象
const isObject = (obj) => {
return Object.prototype.toString.call(obj) === "[object Object]";
};
// 扁平化对象
const getObjKeys = (obj) => {
let result = {}; // 用于保存扁平化后的对象
const helpLoop = (obj) => {
const objKeys = Object.keys(obj);
for (let i = 0; i < objKeys.length; i++) {
if (typeof obj[objKeys[i]] == "object") {
helpLoop(obj[objKeys[i]]);
} else {
result[objKeys[i]] = obj[objKeys[i]];
}
}
};
helpLoop(obj);
return result;
};
// 比较两个对象差异
const comparisonObject = (objPre, objNext) => {
if (Object.keys(objPre).length == 0 || !objPre || !objNext) return false;
const tempPre = getObjKeys(objPre);
const tempNext = getObjKeys(objNext);
// 数组去重
let keys = [];
keys.push(...Object.keys(tempPre));
keys.push(...Object.keys(tempNext));
keys = [...new Set(keys)];
for (let i = 0; i < keys.length; i++) {
if (tempPre[keys[i]] != tempNext[keys[i]]) {
return keys[i];
}
}
return false;
};
/*
ele:要插入的元素(父元素要设置相对定位)
cloneEl:要克隆的元素
cssStyles:克隆时去掉不克隆的样式
*/
const isElementHidden = (ele = "", cloneEl = "", cssStyles = []) => {
if (!ele || !cloneEl) {
return false;
}
const clonedNode = cloneEl.cloneNode(true);
const allStyle = cloneEl.computedStyleMap();
for (const [prop, val] of allStyle) {
if (!cssStyles.includes(prop)) {
clonedNode.style[prop] = val;
}
}
clonedNode.className = "";
clonedNode.style.visibility = "hidden";
clonedNode.style.position = "absolute";
clonedNode.setAttribute("id", "ellipt_hidden__node___id");
ele.appendChild(clonedNode);
const differ = clonedNode.offsetHeight > ele.offsetHeight;
ele.removeChild(clonedNode);
return differ;
};
const formattedTimeYYYYmmdd = (dateTime) =>{
const date = new Date(dateTime);
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const hour = ("0" + date.getHours()).slice(-2);
const minute = ("0" + date.getMinutes()).slice(-2);
const second = ("0" + date.getSeconds()).slice(-2);
const type = getDeviceType();
console.log("机型格式",type)
return `${year}-${month}-${day}`;
}
const formattedTime = (dateTime) =>{
const date = new Date(dateTime);
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const hour = ("0" + date.getHours()).slice(-2);
const minute = ("0" + date.getMinutes()).slice(-2);
const second = ("0" + date.getSeconds()).slice(-2);
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
const formattedDate = (dateTime) =>{
const date = new Date(dateTime);
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const hour = ("0" + date.getHours()).slice(-2);
const minute = ("0" + date.getMinutes()).slice(-2);
const second = ("0" + date.getSeconds()).slice(-2);
if(hour==="00" && minute==="00" && second==="00"){
return `${year}-${month}-${day}`;
}else{
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
}
/**
* iOS 兼容的时间格式化
* 将 YYYY-MM-DD 中的 - 替换为 / 避免 iOS Safari 解析为 NaN
* @param {*} dateStr 日期字符串
* @returns 格式化后的日期时间字符串 YYYY-MM-DD HH:mm:ss
*/
const formatTime = (dateStr) => {
if (!dateStr) return '-';
const d = new Date(String(dateStr).replace(/-/g, '/'));
if (isNaN(d.getTime())) return dateStr;
const y = d.getFullYear();
const m = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
const h = d.getHours();
const min = d.getMinutes();
const sec = d.getSeconds();
// 时分秒全为0时只展示日期
if (h === 0 && min === 0 && sec === 0) {
return `${y}-${m}-${day}`;
}
const hh = String(h).padStart(2, '0');
const mm = String(min).padStart(2, '0');
const ss = String(sec).padStart(2, '0');
return `${y}-${m}-${day} ${hh}:${mm}:${ss}`;
};
/**
* 格式化用户名称为两个字的姓名
* @param {*} name
* @returns
*/
const alignNames = (name) =>{
if(name.length == 2){
name = name.substring(0,1) + " " + name.substring(1,2);
}
return name;
}
const getDeviceType = () =>{
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
return 'iOS';
} else if (userAgent.match(/Android/i)) {
return 'Android';
} else {
return 'unknown';
}
}
/**
* 格式化执法人员/入企人员数据,支持逗号分隔的名字和ID对应显示
* 支持多种字段名:lawEnforcerTwo/lawEnforcerIdTwo 或 lawEnforcerOther/lawEnforcerIdOther
* @param {Object} item - 包含执法人员信息的对象
* @returns {Array} - 格式化后的执法人员数组,每个元素为 "名字 - ID" 的格式
*/
const formatEnforcer = (item) => {
const enforcers = [];
if (item.lawEnforcer) {
const names = item.lawEnforcer.split(',').map(n => n.trim()).filter(n => n);
const ids = item.lawEnforcerTel ? item.lawEnforcerTel.split(',').map(id => id.trim()).filter(id => id) : [];
names.forEach((name, index) => {
const id = ids[index] || '';
enforcers.push({ name, id });
});
}
const secondName = item.lawEnforcerTwo || item.lawEnforcerOther;
const secondId = item.lawEnforcerTelTwo || item.lawEnforcerIdOther;
if (secondName) {
const names = secondName.split(',').map(n => n.trim()).filter(n => n);
const ids = secondId ? secondId.split(',').map(id => id.trim()).filter(id => id) : [];
names.forEach((name, index) => {
const id = ids[index] || '';
enforcers.push({ name, id });
});
}
return enforcers;
};
/**
* 电话号码脱敏处理
* 保留前3位和后4位,中间用 4 个 * 替换
* @param {string|number} phone 原始电话号码
* @returns {string} 脱敏后的电话号码,如 138****5678
*/
const maskPhone = (phone) => {
if (!phone) return '';
const str = String(phone);
if (str.length < 7) return str;
return str.substring(0, 3) + '****' + str.substring(str.length - 4);
};
/**
* 解析执法人员字符串,拆分名字和电话/证件号
* @param {string} str - 如 "陈碧祺 178****7511"
* @returns {Object} { name: '陈碧祺', phone: '178****7511' }
*/
const parseEnforcer = (str) => {
if (!str) return { name: '', phone: '' };
const match = str.trim().match(/^(.+?)\s+(\S+)$/);
if (match) {
return { name: match[1], phone: match[2] };
}
return { name: str.trim(), phone: '' };
};
export { setGoodsNum, isObject, comparisonObject, isElementHidden, formattedTimeYYYYmmdd, formattedTime, getDeviceType, formattedDate, formatEnforcer, formatTime, maskPhone, parseEnforcer };