common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // 通用工具类,定义通用函数及常用接口
  2. import { encryptByAes } from '@/utils/encrypt'
  3. import { preview } from '@/business/platform/form/utils/custom/preview'
  4. import request from '@/business/platform/form/utils/custom/joinCURD'
  5. import pinyin4js from 'pinyin4js'
  6. import { snapshoot } from '@/api/platform/file/attachment'
  7. import { getNextIdByAlias } from '@/api/platform/system/identity'
  8. import { save as sendMsg } from '@/api/platform/message/innerMessage'
  9. import { save as saveNews } from '@/api/platform/system/news'
  10. import { bpmTaskSave } from '@/api/platform/bpmn/bpmTask'
  11. import { onlyOfficeToPdf, generateUUID } from '@/api/platform/form/seal'
  12. import { downloadFile as download } from '@/business/platform/file/utils'
  13. import { removeFormData } from '@/api/platform/data/dataTemplate'
  14. // base64解码
  15. const decode = str => decodeURIComponent(window.atob(str).split('').map(c => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`).join(''))
  16. // 下载
  17. export const downloadByBlob = (o, name) => {
  18. if (!(o instanceof Blob)) {
  19. return
  20. }
  21. if (window.navigator.msSaveBlob) {
  22. window.navigator.msSaveBlob(o, name)
  23. } else {
  24. const URL = window.URL || window.webkitURL || window
  25. const e = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
  26. e.href = URL.createObjectURL(o)
  27. e.download = name
  28. if (document.all) {
  29. e.click()
  30. } else {
  31. const ev = document.createEvent('MouseEvents')
  32. ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, false, false, false, false, 0, null)
  33. e.dispatchEvent(ev)
  34. }
  35. }
  36. }
  37. // 获取当前时间
  38. export const getDateNow = (length = 10, formatType) => {
  39. if (formatType === 'string') {
  40. return new Date(new Date().getTime() + 28800000).toJSON().slice(0, length).replace(/[-:T]/g, '')
  41. }
  42. return new Date(new Date().getTime() + 28800000).toJSON().slice(0, length).replace('T', ' ')
  43. }
  44. // 获取指定值后的日期
  45. export const getDate = (type, value, date) => {
  46. const d = date || getDateNow(19)
  47. if (typeof type !== 'string' || !Number.isInteger(value) || typeof d !== 'string') {
  48. console.log('参数类型错误')
  49. return null
  50. }
  51. const now = new Date(d)
  52. const D = {
  53. day: value * 24 * 60 * 60 * 1000,
  54. hour: value * 60 * 60 * 1000,
  55. minute: value * 60 * 1000,
  56. second: value * 1000
  57. }
  58. const year = now.getFullYear()
  59. const month = now.getMonth()
  60. const day = now.getDate()
  61. const hour = now.getHours()
  62. const minute = now.getMinutes()
  63. const second = now.getSeconds()
  64. switch (type) {
  65. case 'year': {
  66. const isLeapYear = new Date(year + value, 1, 29).getDate() === 29
  67. return new Date(year + value, isLeapYear && month === 1 && day === 29 ? 1 : month, isLeapYear && month === 1 && day === 29 ? 29 : day, hour, minute, second)
  68. }
  69. case 'month': {
  70. let newYear = year
  71. let newMonth = month + value
  72. if (newMonth < 0) {
  73. newYear -= Math.ceil(Math.abs(newMonth) / 12)
  74. newMonth = 12 - Math.abs(newMonth) % 12
  75. } else if (newMonth > 11) {
  76. newYear += Math.floor(newMonth / 12)
  77. newMonth = newMonth % 12
  78. }
  79. const isLeapMonth = new Date(newYear, newMonth + 1, 0).getDate() === 29
  80. return new Date(newYear, isLeapMonth && month === 1 && day === 29 ? 1 : newMonth, isLeapMonth && month === 1 && day === 29 ? 29 : day, hour, minute, second)
  81. }
  82. case 'day': case 'hour': case 'minute': case 'second':
  83. return new Date(now.getTime() + D[type])
  84. default:
  85. console.log('无效的日期类型')
  86. return null
  87. }
  88. }
  89. // 时间格式化
  90. export const getFormatDate = (type, length, date = new Date()) => {
  91. const now = new Date(new Date(date).getTime())
  92. // eslint-disable-next-line
  93. if (now == 'Invalid Date') {
  94. console.log('非法日期,无法格式化')
  95. return date
  96. }
  97. const year = now.getFullYear()
  98. const month = now.getMonth() + 1
  99. const day = now.getDate()
  100. const hours = now.getHours()
  101. const minutes = now.getMinutes()
  102. const seconds = now.getSeconds()
  103. // 补零
  104. const padZero = (num) => {
  105. return num.toString().padStart(2, '0')
  106. }
  107. // 默认返回String类型
  108. let result = `${year}-${padZero(month)}-${padZero(day)} ${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`
  109. switch (type) {
  110. case 'string':
  111. result = `${year}-${padZero(month)}-${padZero(day)} ${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`
  112. break
  113. case 'cn':
  114. result = `${year}年${padZero(month)}月${padZero(day)}日 ${padZero(hours)}时${padZero(minutes)}分${padZero(seconds)}秒`
  115. break
  116. case 'number':
  117. result = `${year}${padZero(month)}${padZero(day)}${padZero(hours)}${padZero(minutes)}${padZero(seconds)}`
  118. break
  119. default:
  120. break
  121. }
  122. return result.slice(0, length || result.length)
  123. }
  124. export default {
  125. preview,
  126. request,
  127. pinyin4js,
  128. snapshoot,
  129. getNextIdByAlias,
  130. decode,
  131. encryptByAes,
  132. downloadByBlob,
  133. sendMsg,
  134. saveNews,
  135. bpmTaskSave,
  136. getDate,
  137. getDateNow,
  138. getFormatDate,
  139. onlyOfficeToPdf,
  140. generateUUID,
  141. download,
  142. removeFormData
  143. }