export const tools = { //根据key值去对象中取对应的值 dataForKey: function (data: any, key: any) { let arr = key.split(".") if (arr && arr[0]) { for (let i = 0; i < arr.length; i++) { if (arr[i] && data[arr[i]]) { data = data[arr[i]] } else { return '' } } return data } }, //根据key值去设置对象的值 setDataForKey: function (data: any, key: any, value: any) { let arr = key.split(".") if (arr && arr[0]) { for (let i = 0; i < arr.length; i++) { if (!(arr[i] && data[arr[i]])) { data[arr[i]] = {} } if (value !== undefined) { if (i == arr.length - 1) { data[arr[i]] = value } } data = data[arr[i]] } } }, deepCopy: function (obj: any): any { const out = [], len = obj.length; for (let i = 0; i < len; i++) { if (obj[i] instanceof Array) { out[i] = tools.deepCopy(obj[i]); } else out[i] = obj[i]; } return out; }, formatDate: (now: any) => { let year = now.getFullYear(); let month = now.getMonth() + 1; let date = now.getDate(); let hour = now.getHours(); let minute = now.getMinutes(); let second = now.getSeconds(); return year + "-" + month + "-" + (date > 10 ? date : '0' + date) + " " + hour + ":" + (minute > 10 ? minute : '0' + minute) + ":" + (second > 10 ? second : '0' + second); }, /** * @param arr [a,b,c] * * val value */ returnNewObj: (data: any, arr: any, val: any) => { if (arr.length > 1) { let k = arr.shift() if (!data[k]) { data[k] = {} } tools.returnNewObj(data[k], arr, val) } else { let curk = arr[0] data[curk] = val; } }, /** * @param data Object * @returns 将对象中 key 为a.b.c 转换为a:{b:{c:''}} */ formatData: (data: any) => { let newData = JSON.parse(JSON.stringify(data)) for (let key in newData) { if (key.indexOf('.') > -1) { let arr = key.split('.') tools.returnNewObj(newData, arr, newData[key]) delete newData[key] } } return newData }, /** * 解析模型文件名称信息 * @param name 模型文件名称 * @returns 模型文件名包含的楼层信息 */ getMoldeFileInfo: (name: string) => { const regList = name.match(/(-[FB]\d+M?[.-])|(-RF[.-])/g); const floorInfo: any = {} if (regList && regList.length === 1) { const floorName = regList[0].slice(1, -1); floorInfo.floorName = floorName; if (floorName === 'RF') { //顶层 floorInfo.floorSequenceId = 9999; return floorInfo; } else { //非顶层 const strList = floorName.split(""); if (strList[0] && strList[0] === 'F') { //地上 if (strList[strList.length - 1] === 'M') { //有夹层 floorInfo.floorSequenceId = Number(floorName.slice(1, -1)) * 10 + 5; } else { //无夹层 floorInfo.floorSequenceId = Number(floorName.slice(1)) * 10; } return floorInfo; } else if (strList[0] && strList[0] === 'B') { //地下 if (strList[strList.length - 1] === 'M') { //有夹层 floorInfo.floorSequenceId = -(Number(floorName.slice(1, -1)) * 10 + 5); } else { //无夹层 floorInfo.floorSequenceId = -(Number(floorName.slice(1)) * 10); } return floorInfo; } else { //其他 return false; } } } else { return false; } } } export default tools