tools.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { arrIndex, getArrValue } from 'js-fast-way'
  2. import { getDictionary, getDictionaryBiz } from '~api/other'
  3. //延迟
  4. export const setLatency = (time = 1000) => {
  5. return new Promise((resolve) => {
  6. setTimeout(() => {
  7. resolve(true)
  8. }, time)
  9. })
  10. }
  11. //获取字典数据
  12. export const getDictionaryData = async (code, biz = false) => {
  13. let res = []
  14. if (biz) {
  15. const { data } = await getDictionaryBiz({ code: code })
  16. res = getArrValue(data)
  17. } else {
  18. const { data } = await getDictionary({ code: code })
  19. res = getArrValue(data)
  20. }
  21. //处理数据
  22. let newArr = []
  23. for (let i = 0; i < res.length; i++) {
  24. newArr.push({
  25. label: res[i]['dictValue'],
  26. value: Number(res[i]['dictKey']),
  27. })
  28. }
  29. return newArr
  30. }
  31. //取数组中的值
  32. export const getRowsValue = (arr, key, key2, value) => {
  33. if (value) {
  34. const index = arrIndex(arr, key, value)
  35. return arr[index][key2]
  36. } else {
  37. return ''
  38. }
  39. }
  40. //日期格式化
  41. export const dateFormat = (date, format) => {
  42. format = format || 'yyyy-MM-dd hh:mm:ss'
  43. if (date !== 'Invalid Date') {
  44. let o = {
  45. 'M+': date.getMonth() + 1, //month
  46. 'd+': date.getDate(), //day
  47. 'h+': date.getHours(), //hour
  48. 'm+': date.getMinutes(), //minute
  49. 's+': date.getSeconds(), //second
  50. 'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
  51. 'S': date.getMilliseconds(), //millisecond
  52. }
  53. if (/(y+)/.test(format)) {
  54. format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  55. }
  56. for (let k in o) {
  57. if (new RegExp('(' + k + ')').test(format)) {
  58. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  59. }
  60. }
  61. return format
  62. }
  63. return ''
  64. }
  65. //获取当前域名
  66. export const getTopUrl = () => {
  67. return window.location.href.split('/#/')[0]
  68. }
  69. //设置系统名称
  70. export const setAppName = (name) => {
  71. const title = window.document.title
  72. window.document.title = `${title}${name ? ' - ' + name : ''}`
  73. }
  74. //判断是否为网址
  75. export const isPathUrl = (path) => {
  76. return /^(https?:|mailto:|tel:)/.test(path)
  77. }
  78. //判断起止时间是否为1个月
  79. export const isExactlyOneMonthApart = (dateStr1, dateStr2)=> {
  80. // 将日期字符串转换为 Date 对象
  81. const date1 = new Date(dateStr1)
  82. const date2 = new Date(dateStr2)
  83. // 获取两个日期的年、月、日
  84. const year1 = date1.getFullYear()
  85. const month1 = date1.getMonth()
  86. const day1 = date1.getDate()
  87. const year2 = date2.getFullYear()
  88. const month2 = date2.getMonth()
  89. const day2 = date2.getDate()
  90. // 计算日期差值(以毫秒为单位)
  91. const diff = Math.abs(date2 - date1)
  92. // 计算日期相差的天数
  93. const daysDiff = Math.ceil(diff / (1000 * 60 * 60 * 24))
  94. // 判断日期相差的天数是否等于一个月的天数
  95. const daysInMonth1 = new Date(year1, month1 + 1, 0).getDate() // 当月的天数
  96. const daysInMonth2 = new Date(year2, month2 + 1, 0).getDate() // 下个月的天数
  97. return daysDiff === daysInMonth1 || daysDiff === daysInMonth2
  98. }