common.js 6.3 KB

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