common.js 6.2 KB

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