// 设置购物车数量 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}`; } } /** * 格式化用户名称为两个字的姓名 * @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'; } } export { setGoodsNum, isObject, comparisonObject, isElementHidden ,formattedTimeYYYYmmdd,formattedTime,getDeviceType,formattedDate};