tool.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /* eslint-disable no-mixed-operators */
  2. /* eslint-disable no-unused-expressions */
  3. /* eslint-disable max-statements-per-line */
  4. /* eslint-disable prefer-regex-literals */
  5. /**
  6. * Created by PanJiaChen on 16/11/18.
  7. */
  8. /**
  9. * Parse the time to string
  10. * @param {(Object|string|number)} time
  11. * @param {string} cFormat
  12. * @returns {string | null}
  13. */
  14. export function parseTime(time: string | number | Date, cFormat: string) {
  15. if (arguments.length === 0 || !time) {
  16. return null
  17. }
  18. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  19. let date
  20. if (typeof time === 'object') {
  21. date = time
  22. }
  23. else {
  24. if ((typeof time === 'string')) {
  25. if ((/^[0-9]+$/.test(time))) {
  26. // support "1548221490638"
  27. time = parseInt(time)
  28. }
  29. else {
  30. // support safari
  31. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  32. time = time.replace(new RegExp(/-/gm), '/')
  33. }
  34. }
  35. if ((typeof time === 'number') && (time.toString().length === 10)) {
  36. time = time * 1000
  37. }
  38. date = new Date(time)
  39. }
  40. const formatObj = {
  41. y: date.getFullYear(),
  42. m: date.getMonth() + 1,
  43. d: date.getDate(),
  44. h: date.getHours(),
  45. i: date.getMinutes(),
  46. s: date.getSeconds(),
  47. a: date.getDay(),
  48. }
  49. const time_str = format.replace(/{([ymdhisa])+}/g, (result: any, key: string) => {
  50. const value = formatObj[key as unknown as keyof typeof formatObj]
  51. // Note: getDay() returns 0 on Sunday
  52. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  53. return value.toString().padStart(2, '0')
  54. })
  55. return time_str
  56. }
  57. /**
  58. * @param {number} time
  59. * @param {string} option
  60. * @returns {string}
  61. */
  62. export function formatTime(time: string | number | Date, option: any) {
  63. if ((`${time}`).length === 10) {
  64. time = parseInt(time.toString()) * 1000
  65. }
  66. else {
  67. time = +time
  68. }
  69. const d = new Date(time)
  70. const now = Date.now()
  71. const diff = (now - d.getTime()) / 1000
  72. if (diff < 30) {
  73. return '刚刚'
  74. }
  75. else if (diff < 3600) {
  76. // less 1 hour
  77. return `${Math.ceil(diff / 60)}分钟前`
  78. }
  79. else if (diff < 3600 * 24) {
  80. return `${Math.ceil(diff / 3600)}小时前`
  81. }
  82. else if (diff < 3600 * 24 * 2) {
  83. return '1天前'
  84. }
  85. if (option) {
  86. return parseTime(time, option)
  87. }
  88. else {
  89. return (
  90. `${d.getMonth()
  91. + 1
  92. }月${
  93. d.getDate()
  94. }日${
  95. d.getHours()
  96. }时${
  97. d.getMinutes()
  98. }分`
  99. )
  100. }
  101. }
  102. /**
  103. * @param {string} input value
  104. * @returns {number} output value
  105. */
  106. export function byteLength(str: string) {
  107. // returns the byte length of an utf8 string
  108. let s = str.length
  109. for (let i = str.length - 1; i >= 0; i--) {
  110. const code = str.charCodeAt(i)
  111. if (code > 0x7F && code <= 0x7FF) { s++ }
  112. else if (code > 0x7FF && code <= 0xFFFF) { s += 2 }
  113. if (code >= 0xDC00 && code <= 0xDFFF) { i-- }
  114. }
  115. return s
  116. }
  117. /**
  118. * @param {Array} actual
  119. * @returns {Array}
  120. */
  121. export function cleanArray(actual: string | any[]) {
  122. const newArray = []
  123. for (let i = 0; i < actual.length; i++) {
  124. if (actual[i]) {
  125. newArray.push(actual[i])
  126. }
  127. }
  128. return newArray
  129. }
  130. /**
  131. * @param {Object} json
  132. * @returns {Array}
  133. */
  134. export function param(json: { [x: string]: string | number | boolean }) {
  135. if (!json) { return '' }
  136. return cleanArray(
  137. Object.keys(json).map((key) => {
  138. if (json[key] === undefined) { return '' }
  139. return `${encodeURIComponent(key)}=${encodeURIComponent(json[key])}`
  140. }),
  141. ).join('&')
  142. }
  143. /**
  144. * @param {string} url
  145. * @returns {Object}
  146. */
  147. export function param2Obj(url: string) {
  148. interface Obj {
  149. name: String
  150. }
  151. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  152. if (!search) {
  153. return {}
  154. }
  155. const obj = <Obj>({})
  156. const searchArr = search.split('&')
  157. searchArr.forEach((v) => {
  158. const index = v.indexOf('=')
  159. if (index !== -1) {
  160. const name = v.substring(0, index)
  161. const val = v.substring(index + 1, v.length)
  162. obj[name as unknown as keyof typeof obj] = val
  163. }
  164. })
  165. return obj
  166. }
  167. /**
  168. * @param {string} val
  169. * @returns {string}
  170. */
  171. export function html2Text(val: string) {
  172. const div = document.createElement('div')
  173. div.innerHTML = val
  174. return div.textContent || div.innerText
  175. }
  176. /**
  177. * Merges two objects, giving the last one precedence
  178. * @param {Object} target
  179. * @param {(Object|Array)} source
  180. * @returns {Object}
  181. */
  182. export function objectMerge(target: { [x: string]: any }, source: string | any[]) {
  183. if (typeof target !== 'object') {
  184. target = {}
  185. }
  186. if (Array.isArray(source)) {
  187. return source.slice()
  188. }
  189. Object.keys(source).forEach((property) => {
  190. const sourceProperty = source[property as unknown as keyof typeof source]
  191. if (typeof sourceProperty === 'object') {
  192. target[property] = objectMerge(target[property], sourceProperty)
  193. }
  194. else {
  195. target[property] = sourceProperty
  196. }
  197. })
  198. return target
  199. }
  200. /**
  201. * @param {HTMLElement} element
  202. * @param {string} className
  203. */
  204. export function toggleClass(element: { className: any }, className: string | any[]) {
  205. if (!element || !className) {
  206. return
  207. }
  208. let classString = element.className
  209. const nameIndex = classString.indexOf(className)
  210. if (nameIndex === -1) {
  211. classString += `${className}`
  212. }
  213. else {
  214. classString
  215. = classString.substr(0, nameIndex)
  216. + classString.substr(nameIndex + className.length)
  217. }
  218. element.className = classString
  219. }
  220. /**
  221. * @param {string} type
  222. * @returns {Date}
  223. */
  224. export function getTime(type: string) {
  225. if (type === 'start') {
  226. return new Date().getTime() - 3600 * 1000 * 24 * 90
  227. }
  228. else {
  229. return new Date(new Date().toDateString())
  230. }
  231. }
  232. /**
  233. * @param {Function} func
  234. * @param {number} wait
  235. * @param {boolean} immediate
  236. * @return {*}
  237. */
  238. /**
  239. * @param {Array} arr
  240. * @returns {Array}
  241. */
  242. export function uniqueArr(arr: Iterable<unknown> | null | undefined) {
  243. return Array.from(new Set(arr))
  244. }
  245. /**
  246. * @returns {string}
  247. */
  248. export function createUniqueString() {
  249. const timestamp = `${+new Date()}`
  250. const randomNum = parseInt(((1 + Math.random()) * 6553).toString())
  251. return (+(randomNum + timestamp)).toString(32)
  252. }
  253. /**
  254. * Check if an element has a class
  255. * @param {HTMLElement} elm
  256. * @param {string} cls
  257. * @returns {boolean}
  258. */
  259. export function hasClass(ele: { className: string }, cls: any) {
  260. return !!ele.className.match(new RegExp(`(\\s|^)${cls}(\\s|$)`))
  261. }
  262. /**
  263. * Add class to element
  264. * @param {HTMLElement} elm
  265. * @param {string} cls
  266. */
  267. export function addClass(ele: { className: string }, cls: any) {
  268. if (!hasClass(ele, cls)) { ele.className += ` ${cls}` }
  269. }
  270. /**
  271. * Remove class from element
  272. * @param {HTMLElement} elm
  273. * @param {string} cls
  274. */
  275. export function removeClass(ele: { className: string }, cls: any) {
  276. if (hasClass(ele, cls)) {
  277. const reg = new RegExp(`(\\s|^)${cls}(\\s|$)`)
  278. ele.className = ele.className.replace(reg, ' ')
  279. }
  280. }
  281. export function strArr2NumArr(actual: string | any[]) {
  282. const newArray = []
  283. for (let i = 0; i < actual.length; i++) {
  284. if (actual[i]) {
  285. newArray.push(parseInt(actual[i]))
  286. }
  287. }
  288. return newArray
  289. }
  290. // 格式化日期:yyyy-MM-dd
  291. export function formatDate(date: Date) {
  292. const myyear = date.getFullYear()
  293. const mymonth = date.getMonth() + 1
  294. const myweekday = date.getDate()
  295. let _mymonth
  296. let _myweekday
  297. if (mymonth < 10) {
  298. _mymonth = `0${mymonth}`
  299. }
  300. if (myweekday < 10) {
  301. _myweekday = `0${myweekday}`
  302. }
  303. return (`${myyear}-${_mymonth}-${_myweekday}`)
  304. }
  305. // 获得某月的天数
  306. export function getMonthDays(myMonth: number) {
  307. const now = new Date() // 当前日期
  308. const nowYear = now.getFullYear() // 当前年
  309. const monthStartDate = new Date(nowYear, myMonth, 1).getTime()
  310. const monthEndDate = new Date(nowYear, myMonth + 1, 1).getTime()
  311. const days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
  312. return days
  313. }
  314. // 获得本季度的开始月份
  315. export function getQuarterStartMonth() {
  316. const now = new Date() // 当前日期
  317. const nowMonth = now.getMonth() // 当前月
  318. let quarterStartMonth = 0
  319. if (nowMonth < 3) {
  320. quarterStartMonth = 0
  321. }
  322. if (nowMonth > 2 && nowMonth < 6) {
  323. quarterStartMonth = 3
  324. }
  325. if (nowMonth > 5 && nowMonth < 9) {
  326. quarterStartMonth = 6
  327. }
  328. if (nowMonth > 8) {
  329. quarterStartMonth = 9
  330. }
  331. return quarterStartMonth
  332. }
  333. // 获得本周的开始日期
  334. export function getWeekStartDate() {
  335. const now = new Date() // 当前日期
  336. const nowDay = now.getDate() // 当前日
  337. const nowDayOfWeek = now.getDay() - 1 // 今天本周的第几天
  338. const nowMonth = now.getMonth()
  339. const nowYear = now.getFullYear() // 当前年
  340. const weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
  341. return formatDate(weekStartDate)
  342. }
  343. // 获得本周的结束日期
  344. export function getWeekEndDate() {
  345. const now = new Date() // 当前日期
  346. const nowDay = now.getDate() // 当前日
  347. const nowMonth = now.getMonth() // 当前月
  348. const nowYear = now.getFullYear() // 当前年
  349. const nowDayOfWeek = now.getDay() - 1 // 今天本周的第几天
  350. const weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
  351. return formatDate(weekEndDate)
  352. }
  353. // 获得上周的开始日期
  354. export function getLastWeekStartDate() {
  355. const now = new Date() // 当前日期
  356. const nowDay = now.getDate() // 当前日
  357. const nowMonth = now.getMonth() // 当前月
  358. const nowYear = now.getFullYear() // 当前年
  359. const nowDayOfWeek = now.getDay() - 1 // 今天本周的第几天
  360. const weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7)
  361. return formatDate(weekStartDate)
  362. }
  363. // 获得上周的结束日期
  364. export function getLastWeekEndDate() {
  365. const now = new Date() // 当前日期
  366. const nowDay = now.getDate() // 当前日
  367. const nowMonth = now.getMonth() // 当前月
  368. const nowYear = now.getFullYear() // 当前年
  369. const nowDayOfWeek = now.getDay() - 1 // 今天本周的第几天
  370. const weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1)
  371. return formatDate(weekEndDate)
  372. }
  373. // 获得本月的开始日期
  374. export function getMonthStartDate() {
  375. const now = new Date() // 当前日期
  376. const nowMonth = now.getMonth() // 当前月
  377. const nowYear = now.getFullYear() // 当前年
  378. const monthStartDate = new Date(nowYear, nowMonth, 1)
  379. return formatDate(monthStartDate)
  380. }
  381. // 获得本月的结束日期
  382. export function getMonthEndDate() {
  383. const now = new Date() // 当前日期
  384. const nowMonth = now.getMonth() // 当前月
  385. const nowYear = now.getFullYear() // 当前年
  386. const monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth))
  387. return formatDate(monthEndDate)
  388. }
  389. // 获得本年的开始日期
  390. export function getYearStartDate() {
  391. const now = new Date() // 当前日期
  392. const nowYear = now.getFullYear() // 当前年
  393. const monthStartDate = new Date(nowYear, 1, 1)
  394. return formatDate(monthStartDate)
  395. }
  396. // 获得本年的结束日期
  397. export function getYearEndDate() {
  398. const now = new Date() // 当前日期
  399. const nowYear = now.getFullYear() // 当前年
  400. const yarEndDate = new Date(nowYear, 11, 31)
  401. return formatDate(yarEndDate)
  402. }
  403. // 获得上月开始时间
  404. export function getLastMonthStartDate() {
  405. const now = new Date() // 当前日期
  406. const nowYear = now.getFullYear() // 当前年
  407. const nowMonth = now.getMonth() // 当前月
  408. let lastMonthStartDate
  409. if (nowMonth === 0) {
  410. lastMonthStartDate = new Date(nowYear - 1, 12, 1)
  411. }
  412. else {
  413. lastMonthStartDate = new Date(nowYear, nowMonth - 1, 1)
  414. }
  415. return formatDate(lastMonthStartDate)
  416. }
  417. // 获得上月结束时间
  418. export function getLastMonthEndDate() {
  419. const now = new Date() // 当前日期
  420. const nowMonth = now.getMonth() // 当前月
  421. const nowYear = now.getFullYear() // 当前年
  422. let lastMonthEndDate
  423. if (nowMonth === 0) {
  424. lastMonthEndDate = new Date(nowYear - 1, 12, 31)
  425. }
  426. else {
  427. lastMonthEndDate = new Date(nowYear, nowMonth - 1, getMonthDays(nowMonth - 1))
  428. }
  429. return formatDate(lastMonthEndDate)
  430. }
  431. // 获得本季度的开始日期
  432. export function getQuarterStartDate() {
  433. const now = new Date() // 当前日期
  434. const nowYear = now.getFullYear() // 当前年
  435. const quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1)
  436. return formatDate(quarterStartDate)
  437. }
  438. // 或的本季度的结束日期
  439. export function getQuarterEndDate() {
  440. const now = new Date() // 当前日期
  441. const nowYear = now.getFullYear() // 当前年
  442. const quarterEndMonth = getQuarterStartMonth() + 2
  443. const quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth))
  444. return formatDate(quarterStartDate)
  445. }
  446. // 平级转树结构
  447. /* treeArr 基础数据
  448. * id 唯一id
  449. * parentId 父级id
  450. * childrenList 子级数组名 */
  451. export function packageTreeData(params: { data: any; id?: any; parentId?: any; children?: any }) {
  452. const treeArr = params && params.data || []
  453. const id = params && params.id || 'id'
  454. const parentId = params && params.parentId || 'parentId'
  455. const childrenList = params && params.children || 'children'
  456. const cloneData = JSON.parse(JSON.stringify(treeArr))
  457. return cloneData.filter((fatherItem: { [x: string]: any }) => {
  458. const warpArr = cloneData.filter((sonItem: { [x: string]: any }) => fatherItem[id] === sonItem[parentId])
  459. warpArr.length ? fatherItem[childrenList] = warpArr : null
  460. return !fatherItem[parentId]
  461. })
  462. }
  463. // 根据根id处理树结构
  464. export function getNestedChildren(arr: any = [], parent_id = 'parent_id', id = 'id', parent: any = 0) {
  465. const res: any[] = []
  466. for (const item of arr) {
  467. if (item[parent_id] === parent) {
  468. const children = getNestedChildren(arr, item[id])
  469. if (children.length) {
  470. item.children = children
  471. }
  472. res.push(item)
  473. }
  474. }
  475. return res
  476. }
  477. // 风险类型
  478. export function riskType(val: string | number) {
  479. const strs = [
  480. '默认',
  481. '人',
  482. '物',
  483. '管',
  484. ]
  485. return strs[val as unknown as keyof typeof strs]
  486. }
  487. // 风险等级
  488. export function riskLevel(val: string | number) {
  489. const strs = [
  490. '默认',
  491. '重大',
  492. '较大',
  493. '一般',
  494. '较小',
  495. ]
  496. return strs[val as unknown as keyof typeof strs]
  497. }
  498. // 风险等级
  499. export function riskPointStatus(val: string | number) {
  500. const strs = [
  501. '未知',
  502. '安全受控',
  503. '失控',
  504. ]
  505. return strs[val as unknown as keyof typeof strs]
  506. }
  507. // 风险点等级
  508. export function riskPointLevel(val: string | number) {
  509. const strs = [
  510. '默认',
  511. '重大',
  512. '较大',
  513. '一般',
  514. '较小',
  515. ]
  516. return strs[val as unknown as keyof typeof strs]
  517. }
  518. export function taskPriority(i: string | number) {
  519. const strs = [
  520. '未知',
  521. '较低',
  522. '普通',
  523. '紧急',
  524. '非常紧急',
  525. ]
  526. return strs[i as unknown as keyof typeof strs]
  527. }
  528. export function taskType(i: string | number) {
  529. const strs = [
  530. '未知',
  531. '常规',
  532. '临时',
  533. '立即',
  534. ]
  535. return strs[i as unknown as keyof typeof strs]
  536. }
  537. export function taskStatus(i: number) {
  538. const strs = [
  539. '待处理',
  540. '已完成',
  541. ]
  542. if (i === -1) {
  543. return '已撤消'
  544. }
  545. else {
  546. return strs[i]
  547. }
  548. }
  549. export function checkResult(i: string | number) {
  550. const strs = [
  551. '没执行',
  552. '通过',
  553. '没通过',
  554. '有隐患',
  555. '已完成',
  556. ]
  557. return strs[i as unknown as keyof typeof strs]
  558. }
  559. export function dangerStatus(i: number) {
  560. if (i >= 0) {
  561. const strs = [
  562. '未开始',
  563. '处理中',
  564. '已完成',
  565. ]
  566. return strs[i]
  567. }
  568. else {
  569. return '已撤销'
  570. }
  571. }
  572. export function dangerLevel(i: string | number) {
  573. const strs = [
  574. '未知',
  575. '一般隐患',
  576. '重大隐患',
  577. ]
  578. return strs[i as unknown as keyof typeof strs]
  579. }
  580. export function dangerSource(i: string | number) {
  581. const strs = [
  582. '自查',
  583. '内部反馈',
  584. '上级抽查',
  585. '政府执法',
  586. ]
  587. return strs[i as unknown as keyof typeof strs]
  588. }
  589. export function rectifyCat(i: string | number) {
  590. const strs = [
  591. '自行整改',
  592. '外协整改',
  593. ]
  594. return strs[i as unknown as keyof typeof strs]
  595. }
  596. export function wfInsStatus(i: number) {
  597. if (i >= 0) {
  598. const strs = [
  599. '未开始',
  600. '执行中',
  601. '已完成',
  602. ]
  603. return strs[i]
  604. }
  605. else {
  606. return '已撤销'
  607. }
  608. }
  609. export function wfActivityInsStatus(i: string | number) {
  610. const strs = [
  611. '未开始',
  612. '已完成',
  613. ]
  614. return strs[i as unknown as keyof typeof strs]
  615. }
  616. export function dangerRectifyCat(i: string | number) {
  617. const strs = [
  618. '自行整改',
  619. '外协整改',
  620. ]
  621. return strs[i as unknown as keyof typeof strs]
  622. }
  623. export function alertLevel(i: string | number) {
  624. const strs = [
  625. '未知',
  626. '1级',
  627. '2级',
  628. '3级',
  629. '4级',
  630. '5级',
  631. '6级',
  632. ]
  633. return strs[i as unknown as keyof typeof strs]
  634. }
  635. export function alertStatus(i: number) {
  636. if (i >= 0) {
  637. const strs = [
  638. '未知',
  639. '待处理',
  640. '已处理',
  641. ]
  642. return strs[i]
  643. }
  644. else {
  645. return '已撤销'
  646. }
  647. }
  648. export function hosStoryTitle(i: number) {
  649. if (i >= 0) {
  650. const strs = [
  651. '未知',
  652. '平安医院',
  653. '质量医院',
  654. '满意医院',
  655. '智慧医院',
  656. '健康医院',
  657. '节约型医院',
  658. ]
  659. return strs[i]
  660. }
  661. else {
  662. return '未知'
  663. }
  664. }
  665. export function hosStory(code: string | number) {
  666. const enums = {
  667. pingAn: 1,
  668. zhiLiang: 2,
  669. manYi: 3,
  670. zhiHui: 4,
  671. jianKang: 5,
  672. jieYue: 6,
  673. }
  674. return enums[code as unknown as keyof typeof enums]
  675. }
  676. export function ocAccess(i: string | number) {
  677. const strs = [
  678. '未接入',
  679. '已接入',
  680. ]
  681. return strs[i as unknown as keyof typeof strs]
  682. }