tools.js 3.1 KB

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