123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689 |
- // 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,
- };
- }
- }
- }
- }
|