// Parse the time to string import { store } from '@/store' import Keys from '@/constant/key' import any = jasmine.any import Cookies from 'js-cookie' export const parseTime = ( time?: object | string | number | null, cFormat?: string ): string | null => { if (time === undefined || !time) { return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date: Date if (typeof time === 'object') { date = time as Date } else { if (typeof time === 'string') { if (/^[0-9]+$/.test(time)) { // support "1548221490638" time = parseInt(time) } else { // support safari // https://stackoverflow.com/questions/4310953/invalid-date-in-safari time = time.replace(new RegExp(/-/gm), '/') } } if (typeof time === 'number' && time.toString().length === 10) { time = time * 1000 } date = new Date(time) } const formatObj: { [key: string]: number } = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const timeStr = format.replace(/{([ymdhisa])+}/g, (result, key) => { const value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } return value.toString().padStart(2, '0') }) return timeStr } // Format and filter json data using filterKeys array export const formatJson = (filterKeys: any, jsonData: any) => jsonData.map((data: any) => filterKeys.map((key: string) => { if (key === 'timestamp') { return parseTime(data[key]) } else { return data[key] } })) // Check if an element has a class export const hasClass = (ele: HTMLElement, className: string) => { return !!ele.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)')) } // Add class to element export const addClass = (ele: HTMLElement, className: string) => { if (!hasClass(ele, className)) ele.className += ' ' + className } // Remove class from element export const removeClass = (ele: HTMLElement, className: string) => { if (hasClass(ele, className)) { const reg = new RegExp('(\\s|^)' + className + '(\\s|$)') ele.className = ele.className.replace(reg, ' ') } } // Toggle class for the selected element export const toggleClass = (ele: HTMLElement, className: string) => { if (!ele || !className) { return } let classString = ele.className const nameIndex = classString.indexOf(className) if (nameIndex === -1) { classString += '' + className } else { classString = classString.substr(0, nameIndex) + classString.substr(nameIndex + className.length) } ele.className = classString } export const setQueryConfig = function (queryConfig: any) { let _str = '' for (const o in queryConfig) { if (queryConfig[o] !== -1) { _str += o + '=' + queryConfig[o] + '&' } } _str = _str.substring(0, _str.length - 1) // 末尾是& return _str } // 获取用户信息 export const getUserInfo = function () { // debugger let userInfo = store.state.user const paramsInfo: any = { openid: userInfo.openid, userId: userInfo.userId, mac: userInfo.mac, pubname: Keys.pubname, projectId: userInfo.projectId } if (userInfo.userName) { paramsInfo.userName = userInfo.userName } if (userInfo.userPhone) { paramsInfo.userPhone = userInfo.userPhone } return paramsInfo } // 地址通用参赛携带 export const getComparams = function () { let userInfo = store.state.user let paramsInfo: any = { openid: Keys.openid, userId: userInfo.userId, pubname: Keys.pubname, mac: userInfo.mac, projectId: userInfo.projectId } if (userInfo.userName) { paramsInfo.userName = userInfo.userName } if (userInfo.userPhone) { paramsInfo.userPhone = userInfo.userPhone } return paramsInfo } export const formatDate = function (split: string = 'YYYYMMDD', date: any = new Date()) { //三目运算符 const dates = date ? date : new Date() //年份 const year: number = dates.getFullYear() //月份下标是0-11 const month: any = (dates.getMonth() + 1) < 10 ? '0' + (dates.getMonth() + 1) : (dates.getMonth() + 1) //具体的天数 const day: any = dates.getDate() < 10 ? '0' + dates.getDate() : dates.getDate() // //小时 const Hours = dates.getHours() < 10 ? '0' + dates.getHours() : dates.getHours() // //分钟 const Minutes = dates.getMinutes() < 10 ? '0' + dates.getMinutes() : dates.getMinutes() // //秒 const Seconds = dates.getSeconds() < 10 ? '0' + dates.getSeconds() : dates.getSeconds() //返回数据格式 if (split === 'YYYY年MM月DD日') { return year + '年' + month + '月' + day + '日' } else if (split === 'YYYY.MM') { return year + '.' + month } else if (split === 'YYYYMM') { return year + '' + month } else if (split === 'YYYY-MM-DD') { return year + '-' + month + '-' + day } else if (split === 'YYYY.MM.DD HH:mm') { return year + '.' + month + "." + day + " " + Hours + ":" + Minutes } else if (split === 'YYYY.MM.DD HH:mm:ss') { return year + '.' + month + "." + day + " " + Hours + ":" + Minutes + ":" + Seconds } else { return year + '' + month + '' + day } } // 把字符串 (yyyymmdd) 转换成日期格式(yyyy-mm-dd) export const formatDateStr = function (date: any) { if (date) { return date.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3') } else { return '' } } export const getTomorrow = function (split: any = '') { let dates = new Date() dates.setTime(dates.getTime() + 24 * 60 * 60 * 1000) const Year: number = dates.getFullYear() //月份下标是0-11 const Months: any = (dates.getMonth() + 1) < 10 ? '0' + (dates.getMonth() + 1) : (dates.getMonth() + 1) //具体的天数 const day: any = dates.getDate() < 10 ? '0' + dates.getDate() : dates.getDate() //返回数据格式 return Year + split + Months + split + day } export function getHours() { return new Date().getHours() } export function parseImgUrl(base: string, img: string) { if (img) { return `${process.env.BASE_URL}images/${base}/${img}` } else { return '' } } // 判断舒服在当前公司 export function isWithinLocation(companyConfig: any) { let targetLocationInfo, maxDistance // 是否有权限不限制距离 true-不限制,false-限制 let remoteControl = store.state.user.remoteControl // let companyConfig = store.state.company.companyConfig if (companyConfig.sagaCare && companyConfig.sagaCareLimit) { maxDistance = companyConfig.sagaCareDistance let location = companyConfig.sagaCareCoords.split(',') targetLocationInfo = { longitude: location[0], latitude: location[1] } } maxDistance = maxDistance || 1 let toastTip = '您好像不在公司' } /** * js 获取以前或者未来日期 * @param year */ export function getAddYear(year: number = 0) { let time = new Date() time.setFullYear(time.getFullYear() + year) let y = time.getFullYear() let m = time.getMonth() + 1 return new Date(y + '/' + m) } /** * * 获取上一个月,日期格式yyyyMM */ export function getPreMonth(formatType: any = '') { const nowdays = new Date() let year = nowdays.getFullYear() let month: any = nowdays.getMonth() if (month === 0) { month = 12 year = year - 1 } if (month < 10) { month = '0' + month } if (formatType === 'YYYY.MM') { return year + '.' + month } else if (formatType === 'YYYY年MM月') { return year + '年' + month + '月' } else { return year + '' + month } } /** * 格式化日期 3月21日 */ export const formatEnergyDate = function (time: any) { //三目运算符 const dates = time ? new Date(time) : new Date() //月份下标是0-11 const months: any = (dates.getMonth() + 1) < 10 ? '0' + (dates.getMonth() + 1) : (dates.getMonth() + 1) //具体的天数 const day: any = dates.getDate() < 10 ? '0' + dates.getDate() : dates.getDate() const year: number = dates.getFullYear() const hours = dates.getHours() < 10 ? '0' + dates.getHours() : dates.getHours() // //分钟 const minutes = dates.getMinutes() < 10 ? '0' + dates.getMinutes() : dates.getMinutes() //返回数据格式 return [ year + '年' + months + '月', months + '月' + day + '日', hours + ':' + minutes ] } export const setSession = function (key: any = '', obj: any = '') { if (obj) { let str = JSON.stringify(obj) sessionStorage.setItem(key, str) } } export const getSession = function (key: any = '') { if (key) { let obj: any = sessionStorage.getItem(key) if (obj) { return JSON.parse(obj) } } return '' } /** * 本地存储localStorage * @param key * @param obj */ export const setLocalStorage = function (key: any = '', obj: any = '') { if (obj) { if (obj instanceof Object) { let str = JSON.stringify(obj) localStorage.setItem(key, str) } else { localStorage.setItem(key, obj) } } } /** * 获取本地存储 * @param key */ export const getLocalStorage = function (key: any) { if (key) { let obj: any = localStorage.getItem(key) if (obj) { return JSON.parse(obj) } else { return '' } } return '' } /** * 存储最新的空间信息 * @param spaceInfo */ export const setLocalNewSpaceInfo = function (spaceInfo: any) { setLocalStorage(Keys.storageSpaceInfoKey, spaceInfo) } /** * 获取最新的空间信息 */ export const getLocalNewSpaceInfo = function () { let spaceInfo: any = getLocalStorage(Keys.storageSpaceInfoKey) return spaceInfo } /** * 本地缓存建筑,楼层,空间 */ export const localStorageSpaceId = function (buildingId: any, floorId: any, spaceId: any) { let spaceMap: any = getLocalStorage(Keys.storageSpaceKey) ? getLocalStorage(Keys.storageSpaceKey) : {} let key: any = `${buildingId},${floorId}` spaceMap[key] = spaceId setLocalStorage(Keys.storageSpaceKey, spaceMap) } export const getStorageSpaceId = function () { let spaceMap: any = getLocalStorage(Keys.storageSpaceKey) return spaceMap } /** * 本地缓存建筑对应的楼层 */ export const localStorageFloor = function (buildingId: any, floorId: any) { let floorMap: any = getLocalStorage(Keys.storageFloorKey) ? getLocalStorage(Keys.storageFloorKey) : {} floorMap[buildingId] = floorId setLocalStorage(Keys.storageFloorKey, floorMap) } /** * 获取本地存储的建筑对应的关系 * @param buildingId * @param floorId */ export const getLocalStorageFloor = function () { let floorMap: any = getLocalStorage(Keys.storageFloorKey) return floorMap } /** * 缓存搜索页面最近查找的数据 * @param item */ export const setLocalSearchSpace = function (item: any) { let historySearch: any = getLocalStorage(Keys.historySearchSpaceKey) let flag = false historySearch = historySearch ? historySearch : [] historySearch.map((historyItem: any) => { if (historyItem.id === item.id) { flag = true } }) if (!flag) { historySearch.push(item) } setLocalStorage(Keys.historySearchSpaceKey, historySearch) } /** * 获取搜索页面最近查找的数据 */ export const getLocalSearchSpace = function () { return getLocalStorage(Keys.historySearchSpaceKey) ? getLocalStorage(Keys.historySearchSpaceKey) : [] } /** *存储当前项目id */ export const setLocalProjectId = function (projectId: any) { setLocalStorage(Keys.projectId, projectId) } /** * 获取当前项目id * @param projectId */ export const getLocalProjectId = function () { return localStorage.getItem(Keys.projectId) } /** * 清楚当前所有的存储数据 */ export const clearAllLocalData = function () { localStorage.clear() Cookies.remove('userInfo') Cookies.remove('projectId') Cookies.remove('accessToken') } export const doHandleMonth = function (month: any) { let m: any = month if (month.toString().length == 1) { m = '0' + month } return m } export const getWeekDate = function (day: any) { let weeks: any = new Array( '周日', '周一', '周二', '周三', '周四', '周五', '周六' ) let week: any = weeks[day] return week } export const getNowWeek = function () { let date: any = new Date() let day: any = date.getDay() let nowWeek: any = getWeekDate(day) return nowWeek } export const getDate = function (date: any) { return date.getDate() < 10 ? '0' + date.getDate() : date.getDate() } /** * 初始化24:00 */ export const getTimers = function () { let timers: any = new Array() for (let i = 0; i <= 24; i++) { let str: any = '00' if (i < 10) { str = '0' + i } else { str = i } timers.push(str + ':00') if (i < 24) { timers.push(str + ':30') } } console.log("timers====") console.log(timers) return timers } export const getNowTime = function () { let date: any = new Date() let hours: any = date.getHours() let minute: any = date.getMinutes() let index: any = date.getHours() if (minute < 30) { hours = hours + ':' + '00' index = index * 2 } else { hours = hours + ':' + '30' index = index * 2 + 1 } return [hours, index] } // 获取当前真实时间 export const getRelNowTime = function () { let date: any = new Date() let hours: any = date.getHours() let minute: any = date.getMinutes() if (hours < 10) { hours = "0" + hours } if (minute < 10) { minute = "0" + minute } return hours + "" + minute + "00" } /** * 19000转成19:00 */ export const formatTimerStr = function (timer: any) { if (timer) { let str: any = (timer / 10000).toFixed(2) str = str.replace(".", ":") return str } else { return '' } } export const newNumber = function (start: any, end: any, masAddess: any) { return masAddess + "" + Math.round(Math.random() * (end - start) + start);//生成在[start,end]范围内的随机数值,只支持不小于0的合法范围 } export const formateTimeContinuous: any = function (index: any = 1, startTime: any, endTime: any, type: any = 1, data: any = [], that: any) { let todayDate: any = new Date() let tomorrowData = new Date(todayDate.setTime(todayDate.getTime() + 24 * 60 * 60 * 1000)) let nowDate: any = formatDate("YYYY-MM-DD"); let tomorrowDate: any = formatDate("YYYY-MM-DD", tomorrowData); data.map((item: any) => { // debugger let date: any = formatDateStr(item.date); let week: any = getWeekDate(new Date(date).getDay()); if (date == nowDate) { week = '今日' } else if (date == tomorrowDate) { week = '次日' } item.week = week; }); // debugger let text: any = ""; // 工作时间和第二天连续的问题 // 工作时间连续的问题 let cusStartTime: any = data[index].cusStartTime; let cusEndTime: any = data[index].cusEndTime; if (type === 1) { // debugger; if (endTime === "240000") { // 处理时间连续的问题 let customSceneList: any = data[index]?.customSceneList ?? []; if (cusStartTime === "000000") { text = data[index].week; endTime = cusEndTime; } customSceneList.map((item: any) => { if (item.startTime === "000000") { text = data[index].week; endTime = item.endTime; } else if (endTime === item.startTime) { text = data[index].week; endTime = item.endTime; } if (endTime === cusStartTime) { text = data[index].week; endTime = cusEndTime; } }); if (text) { let nowIndex: any = index + 1; that.text = text if (nowIndex < data.length - 1) { return formateTimeContinuous(nowIndex, startTime, endTime, 1, data, that); } else { return { text: that.text, startTime: startTime, endTime: endTime, }; } } else { return { text: that.text, startTime: startTime, endTime: endTime, }; } } else { return { text: that.text, startTime: startTime, endTime: endTime, }; } } else { // 预约时候后找最近的一段预约时间 let nowTime: any = (getNowTime()[0]).replace(":", "") + "00" let customSceneList: any = data[index]?.customSceneList ?? []; customSceneList.map((item: any) => { if (index === 0) { if (nowTime < item.startTime) { if (!startTime || !endTime) { startTime = item.startTime endTime = item.endTime text = data[index].week } } } else { if (!startTime || !endTime) { startTime = item.startTime endTime = item.endTime text = data[index].week } else { // debugger // debugger if (endTime == '240000') { if (item.startTime == "000000") { endTime = item.endTime text = data[index].week } } else { if (endTime === item.startTime) { endTime = item.endTime text = data[index].week } } } } if (that.text) { let startText: any = that.text.split("~")[0] that.text = startText if (text && text != startText) { that.text = startText + "~" + text } } else { that.text = text } }) if (startTime && endTime) { if (endTime == '240000') { let nowIndex: any = index + 1 if (nowIndex < data.length - 1) { return formateTimeContinuous(nowIndex, startTime, endTime, 2, data, that); } else { return { text: that.text, startTime: startTime, endTime: endTime, }; } } else { return { text: that.text, startTime: startTime, endTime: endTime, }; } } else { let nowIndex: any = index + 1 if (nowIndex < data.length - 1) { return formateTimeContinuous(nowIndex, startTime, endTime, 2, data, that); } else { return { text: that.text, startTime: startTime, endTime: endTime, }; } } } }