index.js 18 KB

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