index.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // Parse the time to string
  2. import { store } from '@/store'
  3. import Keys from '@/constant/key'
  4. import any = jasmine.any
  5. import Cookies from 'js-cookie'
  6. export const parseTime = (
  7. time?: object | string | number | null,
  8. cFormat?: string
  9. ): string | null => {
  10. if (time === undefined || !time) {
  11. return null
  12. }
  13. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  14. let date: Date
  15. if (typeof time === 'object') {
  16. date = time as Date
  17. } else {
  18. if (typeof time === 'string') {
  19. if (/^[0-9]+$/.test(time)) {
  20. // support "1548221490638"
  21. time = parseInt(time)
  22. } else {
  23. // support safari
  24. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  25. time = time.replace(new RegExp(/-/gm), '/')
  26. }
  27. }
  28. if (typeof time === 'number' && time.toString().length === 10) {
  29. time = time * 1000
  30. }
  31. date = new Date(time)
  32. }
  33. const formatObj: { [key: string]: number } = {
  34. y: date.getFullYear(),
  35. m: date.getMonth() + 1,
  36. d: date.getDate(),
  37. h: date.getHours(),
  38. i: date.getMinutes(),
  39. s: date.getSeconds(),
  40. a: date.getDay()
  41. }
  42. const timeStr = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  43. const value = formatObj[key]
  44. // Note: getDay() returns 0 on Sunday
  45. if (key === 'a') {
  46. return ['日', '一', '二', '三', '四', '五', '六'][value]
  47. }
  48. return value.toString().padStart(2, '0')
  49. })
  50. return timeStr
  51. }
  52. // Format and filter json data using filterKeys array
  53. export const formatJson = (filterKeys: any, jsonData: any) =>
  54. jsonData.map((data: any) => filterKeys.map((key: string) => {
  55. if (key === 'timestamp') {
  56. return parseTime(data[key])
  57. } else {
  58. return data[key]
  59. }
  60. }))
  61. // Check if an element has a class
  62. export const hasClass = (ele: HTMLElement, className: string) => {
  63. return !!ele.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'))
  64. }
  65. // Add class to element
  66. export const addClass = (ele: HTMLElement, className: string) => {
  67. if (!hasClass(ele, className)) ele.className += ' ' + className
  68. }
  69. // Remove class from element
  70. export const removeClass = (ele: HTMLElement, className: string) => {
  71. if (hasClass(ele, className)) {
  72. const reg = new RegExp('(\\s|^)' + className + '(\\s|$)')
  73. ele.className = ele.className.replace(reg, ' ')
  74. }
  75. }
  76. // Toggle class for the selected element
  77. export const toggleClass = (ele: HTMLElement, className: string) => {
  78. if (!ele || !className) {
  79. return
  80. }
  81. let classString = ele.className
  82. const nameIndex = classString.indexOf(className)
  83. if (nameIndex === -1) {
  84. classString += '' + className
  85. } else {
  86. classString =
  87. classString.substr(0, nameIndex) +
  88. classString.substr(nameIndex + className.length)
  89. }
  90. ele.className = classString
  91. }
  92. export const setQueryConfig = function (queryConfig: any) {
  93. let _str = ''
  94. for (const o in queryConfig) {
  95. if (queryConfig[o] !== -1) {
  96. _str += o + '=' + queryConfig[o] + '&'
  97. }
  98. }
  99. _str = _str.substring(0, _str.length - 1) // 末尾是&
  100. return _str
  101. }
  102. // 获取用户信息
  103. export const getUserInfo = function () {
  104. // debugger
  105. let userInfo = store.state.user
  106. const paramsInfo: any = {
  107. openid: userInfo.openid,
  108. userId: userInfo.userId,
  109. mac: userInfo.mac,
  110. pubname: Keys.pubname,
  111. projectId: userInfo.projectId
  112. }
  113. if (userInfo.userName) {
  114. paramsInfo.userName = userInfo.userName
  115. }
  116. if (userInfo.userPhone) {
  117. paramsInfo.userPhone = userInfo.userPhone
  118. }
  119. return paramsInfo
  120. }
  121. // 地址通用参赛携带
  122. export const getComparams = function () {
  123. let userInfo = store.state.user
  124. let paramsInfo: any = {
  125. openid: Keys.openid,
  126. userId: userInfo.userId,
  127. pubname: Keys.pubname,
  128. mac: userInfo.mac,
  129. projectId: userInfo.projectId
  130. }
  131. if (userInfo.userName) {
  132. paramsInfo.userName = userInfo.userName
  133. }
  134. if (userInfo.userPhone) {
  135. paramsInfo.userPhone = userInfo.userPhone
  136. }
  137. return paramsInfo
  138. }
  139. export const formatDate = function (split: string = 'YYYYMMDD', date: any = new Date()) {
  140. //三目运算符
  141. const dates = date ? date : new Date()
  142. //年份
  143. const year: number = dates.getFullYear()
  144. //月份下标是0-11
  145. const month: any = (dates.getMonth() + 1) < 10 ? '0' + (dates.getMonth() + 1) : (dates.getMonth() + 1)
  146. //具体的天数
  147. const day: any = dates.getDate() < 10 ? '0' + dates.getDate() : dates.getDate()
  148. // //小时
  149. const Hours = dates.getHours() < 10 ? '0' + dates.getHours() : dates.getHours()
  150. // //分钟
  151. const Minutes = dates.getMinutes() < 10 ? '0' + dates.getMinutes() : dates.getMinutes()
  152. // //秒
  153. const Seconds = dates.getSeconds() < 10 ? '0' + dates.getSeconds() : dates.getSeconds()
  154. //返回数据格式
  155. if (split === 'YYYY年MM月DD日') {
  156. return year + '年' + month + '月' + day + '日'
  157. } else if (split === 'YYYY.MM') {
  158. return year + '.' + month
  159. } else if (split === 'YYYYMM') {
  160. return year + '' + month
  161. } else if (split === 'YYYY-MM-DD') {
  162. return year + '-' + month + '-' + day
  163. } else if (split === 'YYYY.MM.DD HH:mm') {
  164. return year + '.' + month + "." + day + " " + Hours + ":" + Minutes
  165. } else if (split === 'YYYY.MM.DD HH:mm:ss') {
  166. return year + '.' + month + "." + day + " " + Hours + ":" + Minutes + ":" + Seconds
  167. }
  168. else {
  169. return year + '' + month + '' + day
  170. }
  171. }
  172. // 把字符串 (yyyymmdd) 转换成日期格式(yyyy-mm-dd)
  173. export const formatDateStr = function (date: any) {
  174. if (date) {
  175. return date.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3')
  176. } else {
  177. return ''
  178. }
  179. }
  180. export const getTomorrow = function (split: any = '') {
  181. let dates = new Date()
  182. dates.setTime(dates.getTime() + 24 * 60 * 60 * 1000)
  183. const Year: number = dates.getFullYear()
  184. //月份下标是0-11
  185. const Months: any = (dates.getMonth() + 1) < 10 ? '0' + (dates.getMonth() + 1) : (dates.getMonth() + 1)
  186. //具体的天数
  187. const day: any = dates.getDate() < 10 ? '0' + dates.getDate() : dates.getDate()
  188. //返回数据格式
  189. return Year + split + Months + split + day
  190. }
  191. export function getHours() {
  192. return new Date().getHours()
  193. }
  194. export function parseImgUrl(base: string, img: string) {
  195. if (img) {
  196. return `${process.env.BASE_URL}images/${base}/${img}`
  197. } else {
  198. return ''
  199. }
  200. }
  201. // 判断舒服在当前公司
  202. export function isWithinLocation(companyConfig: any) {
  203. let targetLocationInfo, maxDistance
  204. // 是否有权限不限制距离 true-不限制,false-限制
  205. let remoteControl = store.state.user.remoteControl
  206. // let companyConfig = store.state.company.companyConfig
  207. if (companyConfig.sagaCare && companyConfig.sagaCareLimit) {
  208. maxDistance = companyConfig.sagaCareDistance
  209. let location = companyConfig.sagaCareCoords.split(',')
  210. targetLocationInfo = {
  211. longitude: location[0],
  212. latitude: location[1]
  213. }
  214. }
  215. maxDistance = maxDistance || 1
  216. let toastTip = '您好像不在公司'
  217. }
  218. /**
  219. * js 获取以前或者未来日期
  220. * @param year
  221. */
  222. export function getAddYear(year: number = 0) {
  223. let time = new Date()
  224. time.setFullYear(time.getFullYear() + year)
  225. let y = time.getFullYear()
  226. let m = time.getMonth() + 1
  227. return new Date(y + '/' + m)
  228. }
  229. /**
  230. *
  231. * 获取上一个月,日期格式yyyyMM
  232. */
  233. export function getPreMonth(formatType: any = '') {
  234. const nowdays = new Date()
  235. let year = nowdays.getFullYear()
  236. let month: any = nowdays.getMonth()
  237. if (month === 0) {
  238. month = 12
  239. year = year - 1
  240. }
  241. if (month < 10) {
  242. month = '0' + month
  243. }
  244. if (formatType === 'YYYY.MM') {
  245. return year + '.' + month
  246. } else if (formatType === 'YYYY年MM月') {
  247. return year + '年' + month + '月'
  248. } else {
  249. return year + '' + month
  250. }
  251. }
  252. /**
  253. * 格式化日期 3月21日
  254. */
  255. export const formatEnergyDate = function (time: any) {
  256. //三目运算符
  257. const dates = time ? new Date(time) : new Date()
  258. //月份下标是0-11
  259. const months: any = (dates.getMonth() + 1) < 10 ? '0' + (dates.getMonth() + 1) : (dates.getMonth() + 1)
  260. //具体的天数
  261. const day: any = dates.getDate() < 10 ? '0' + dates.getDate() : dates.getDate()
  262. const year: number = dates.getFullYear()
  263. const hours = dates.getHours() < 10 ? '0' + dates.getHours() : dates.getHours()
  264. // //分钟
  265. const minutes = dates.getMinutes() < 10 ? '0' + dates.getMinutes() : dates.getMinutes()
  266. //返回数据格式
  267. return [
  268. year + '年' + months + '月',
  269. months + '月' + day + '日',
  270. hours + ':' + minutes
  271. ]
  272. }
  273. export const setSession = function (key: any = '', obj: any = '') {
  274. if (obj) {
  275. let str = JSON.stringify(obj)
  276. sessionStorage.setItem(key, str)
  277. }
  278. }
  279. export const getSession = function (key: any = '') {
  280. if (key) {
  281. let obj: any = sessionStorage.getItem(key)
  282. if (obj) {
  283. return JSON.parse(obj)
  284. }
  285. }
  286. return ''
  287. }
  288. /**
  289. * 本地存储localStorage
  290. * @param key
  291. * @param obj
  292. */
  293. export const setLocalStorage = function (key: any = '', obj: any = '') {
  294. if (obj) {
  295. if (obj instanceof Object) {
  296. let str = JSON.stringify(obj)
  297. localStorage.setItem(key, str)
  298. } else {
  299. localStorage.setItem(key, obj)
  300. }
  301. }
  302. }
  303. /**
  304. * 获取本地存储
  305. * @param key
  306. */
  307. export const getLocalStorage = function (key: any) {
  308. if (key) {
  309. let obj: any = localStorage.getItem(key)
  310. if (obj) {
  311. return JSON.parse(obj)
  312. } else {
  313. return ''
  314. }
  315. }
  316. return ''
  317. }
  318. /**
  319. * 存储最新的空间信息
  320. * @param spaceInfo
  321. */
  322. export const setLocalNewSpaceInfo = function (spaceInfo: any) {
  323. setLocalStorage(Keys.storageSpaceInfoKey, spaceInfo)
  324. }
  325. /**
  326. * 获取最新的空间信息
  327. */
  328. export const getLocalNewSpaceInfo = function () {
  329. let spaceInfo: any = getLocalStorage(Keys.storageSpaceInfoKey)
  330. return spaceInfo
  331. }
  332. /**
  333. * 本地缓存建筑,楼层,空间
  334. */
  335. export const localStorageSpaceId = function (buildingId: any, floorId: any, spaceId: any) {
  336. let spaceMap: any = getLocalStorage(Keys.storageSpaceKey) ? getLocalStorage(Keys.storageSpaceKey) : {}
  337. let key: any = `${buildingId},${floorId}`
  338. spaceMap[key] = spaceId
  339. setLocalStorage(Keys.storageSpaceKey, spaceMap)
  340. }
  341. export const getStorageSpaceId = function () {
  342. let spaceMap: any = getLocalStorage(Keys.storageSpaceKey)
  343. return spaceMap
  344. }
  345. /**
  346. * 本地缓存建筑对应的楼层
  347. */
  348. export const localStorageFloor = function (buildingId: any, floorId: any) {
  349. let floorMap: any = getLocalStorage(Keys.storageFloorKey) ? getLocalStorage(Keys.storageFloorKey) : {}
  350. floorMap[buildingId] = floorId
  351. setLocalStorage(Keys.storageFloorKey, floorMap)
  352. }
  353. /**
  354. * 获取本地存储的建筑对应的关系
  355. * @param buildingId
  356. * @param floorId
  357. */
  358. export const getLocalStorageFloor = function () {
  359. let floorMap: any = getLocalStorage(Keys.storageFloorKey)
  360. return floorMap
  361. }
  362. /**
  363. * 缓存搜索页面最近查找的数据
  364. * @param item
  365. */
  366. export const setLocalSearchSpace = function (item: any) {
  367. let historySearch: any = getLocalStorage(Keys.historySearchSpaceKey)
  368. let flag = false
  369. historySearch = historySearch ? historySearch : []
  370. historySearch.map((historyItem: any) => {
  371. if (historyItem.id === item.id) {
  372. flag = true
  373. }
  374. })
  375. if (!flag) {
  376. historySearch.push(item)
  377. }
  378. setLocalStorage(Keys.historySearchSpaceKey, historySearch)
  379. }
  380. /**
  381. * 获取搜索页面最近查找的数据
  382. */
  383. export const getLocalSearchSpace = function () {
  384. return getLocalStorage(Keys.historySearchSpaceKey) ? getLocalStorage(Keys.historySearchSpaceKey) : []
  385. }
  386. /**
  387. *存储当前项目id
  388. */
  389. export const setLocalProjectId = function (projectId: any) {
  390. setLocalStorage(Keys.projectId, projectId)
  391. }
  392. /**
  393. * 获取当前项目id
  394. * @param projectId
  395. */
  396. export const getLocalProjectId = function () {
  397. return localStorage.getItem(Keys.projectId)
  398. }
  399. /**
  400. * 清楚当前所有的存储数据
  401. */
  402. export const clearAllLocalData = function () {
  403. localStorage.clear()
  404. Cookies.remove('userInfo')
  405. Cookies.remove('projectId')
  406. Cookies.remove('accessToken')
  407. }
  408. export const doHandleMonth = function (month: any) {
  409. let m: any = month
  410. if (month.toString().length == 1) {
  411. m = '0' + month
  412. }
  413. return m
  414. }
  415. export const getWeekDate = function (day: any) {
  416. let weeks: any = new Array(
  417. '周日',
  418. '周一',
  419. '周二',
  420. '周三',
  421. '周四',
  422. '周五',
  423. '周六'
  424. )
  425. let week: any = weeks[day]
  426. return week
  427. }
  428. export const getNowWeek = function () {
  429. let date: any = new Date()
  430. let day: any = date.getDay()
  431. let nowWeek: any = getWeekDate(day)
  432. return nowWeek
  433. }
  434. export const getDate = function (date: any) {
  435. return date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  436. }
  437. /**
  438. * 初始化24:00
  439. */
  440. export const getTimers = function () {
  441. let timers: any = new Array()
  442. for (let i = 0; i <= 24; i++) {
  443. let str: any = '00'
  444. if (i < 10) {
  445. str = '0' + i
  446. } else {
  447. str = i
  448. }
  449. timers.push(str + ':00')
  450. if (i < 24) {
  451. timers.push(str + ':30')
  452. }
  453. }
  454. console.log("timers====")
  455. console.log(timers)
  456. return timers
  457. }
  458. export const getNowTime = function () {
  459. let date: any = new Date()
  460. let hours: any = date.getHours()
  461. let minute: any = date.getMinutes()
  462. let index: any = date.getHours()
  463. if (minute < 30) {
  464. hours = hours + ':' + '00'
  465. index = index * 2
  466. } else {
  467. hours = hours + ':' + '30'
  468. index = index * 2 + 1
  469. }
  470. return [hours, index]
  471. }
  472. // 获取当前真实时间
  473. export const getRelNowTime = function () {
  474. let date: any = new Date()
  475. let hours: any = date.getHours()
  476. let minute: any = date.getMinutes()
  477. if (hours < 10) {
  478. hours = "0" + hours
  479. }
  480. if (minute < 10) {
  481. minute = "0" + minute
  482. }
  483. return hours + "" + minute + "00"
  484. }
  485. /**
  486. * 19000转成19:00
  487. */
  488. export const formatTimerStr = function (timer: any) {
  489. if (timer) {
  490. let str: any = (timer / 10000).toFixed(2)
  491. str = str.replace(".", ":")
  492. return str
  493. } else {
  494. return ''
  495. }
  496. }
  497. export const newNumber = function (start: any, end: any, masAddess: any) {
  498. return masAddess + "" + Math.round(Math.random() * (end - start) + start);//生成在[start,end]范围内的随机数值,只支持不小于0的合法范围
  499. }
  500. export const formateTimeContinuous: any = function (index: any = 1,
  501. startTime: any,
  502. endTime: any,
  503. type: any = 1, data: any = [], that: any) {
  504. let todayDate: any = new Date()
  505. let tomorrowData = new Date(todayDate.setTime(todayDate.getTime() + 24 * 60 * 60 * 1000))
  506. let nowDate: any = formatDate("YYYY-MM-DD");
  507. let tomorrowDate: any = formatDate("YYYY-MM-DD", tomorrowData);
  508. data.map((item: any) => {
  509. // debugger
  510. let date: any = formatDateStr(item.date);
  511. let week: any = getWeekDate(new Date(date).getDay());
  512. if (date == nowDate) {
  513. week = '今日'
  514. } else if (date == tomorrowDate) {
  515. week = '次日'
  516. }
  517. item.week = week;
  518. });
  519. // debugger
  520. let text: any = "";
  521. // 工作时间和第二天连续的问题
  522. // 工作时间连续的问题
  523. let cusStartTime: any = data[index].cusStartTime;
  524. let cusEndTime: any = data[index].cusEndTime;
  525. if (type === 1) {
  526. // debugger;
  527. if (endTime === "240000") {
  528. // 处理时间连续的问题
  529. let customSceneList: any = data[index]?.customSceneList ?? [];
  530. if (cusStartTime === "000000") {
  531. text = data[index].week;
  532. endTime = cusEndTime;
  533. }
  534. customSceneList.map((item: any) => {
  535. if (item.startTime === "000000") {
  536. text = data[index].week;
  537. endTime = item.endTime;
  538. } else if (endTime === item.startTime) {
  539. text = data[index].week;
  540. endTime = item.endTime;
  541. }
  542. if (endTime === cusStartTime) {
  543. text = data[index].week;
  544. endTime = cusEndTime;
  545. }
  546. });
  547. if (text) {
  548. let nowIndex: any = index + 1;
  549. that.text = text
  550. if (nowIndex < data.length - 1) {
  551. return formateTimeContinuous(nowIndex, startTime, endTime, 1, data, that);
  552. } else {
  553. return {
  554. text: that.text,
  555. startTime: startTime,
  556. endTime: endTime,
  557. };
  558. }
  559. } else {
  560. return {
  561. text: that.text,
  562. startTime: startTime,
  563. endTime: endTime,
  564. };
  565. }
  566. } else {
  567. return {
  568. text: that.text,
  569. startTime: startTime,
  570. endTime: endTime,
  571. };
  572. }
  573. } else {
  574. // 预约时候后找最近的一段预约时间
  575. let nowTime: any = (getNowTime()[0]).replace(":", "") + "00"
  576. let customSceneList: any = data[index]?.customSceneList ?? [];
  577. customSceneList.map((item: any) => {
  578. if (index === 0) {
  579. if (nowTime < item.startTime) {
  580. if (!startTime || !endTime) {
  581. startTime = item.startTime
  582. endTime = item.endTime
  583. text = data[index].week
  584. }
  585. }
  586. } else {
  587. if (!startTime || !endTime) {
  588. startTime = item.startTime
  589. endTime = item.endTime
  590. text = data[index].week
  591. } else {
  592. // debugger
  593. // debugger
  594. if (endTime == '240000') {
  595. if (item.startTime == "000000") {
  596. endTime = item.endTime
  597. text = data[index].week
  598. }
  599. } else {
  600. if (endTime === item.startTime) {
  601. endTime = item.endTime
  602. text = data[index].week
  603. }
  604. }
  605. }
  606. }
  607. if (that.text) {
  608. let startText: any = that.text.split("~")[0]
  609. that.text = startText
  610. if (text && text != startText) {
  611. that.text = startText + "~" + text
  612. }
  613. } else {
  614. that.text = text
  615. }
  616. })
  617. if (startTime && endTime) {
  618. if (endTime == '240000') {
  619. let nowIndex: any = index + 1
  620. if (nowIndex < data.length - 1) {
  621. return formateTimeContinuous(nowIndex, startTime, endTime, 2, data, that);
  622. } else {
  623. return {
  624. text: that.text,
  625. startTime: startTime,
  626. endTime: endTime,
  627. };
  628. }
  629. } else {
  630. return {
  631. text: that.text,
  632. startTime: startTime,
  633. endTime: endTime,
  634. };
  635. }
  636. } else {
  637. let nowIndex: any = index + 1
  638. if (nowIndex < data.length - 1) {
  639. return formateTimeContinuous(nowIndex, startTime, endTime, 2, data, that);
  640. } else {
  641. return {
  642. text: that.text,
  643. startTime: startTime,
  644. endTime: endTime,
  645. };
  646. }
  647. }
  648. }
  649. }