common.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 pinyin from '@/utils/pinyin'
  8. import { snapshoot } from '@/api/platform/file/attachment'
  9. import { getToken } from '@/utils/auth'
  10. import {
  11. getNextIdByAlias,
  12. reagentConsumablesInventory
  13. } from '@/api/platform/system/identity'
  14. import { save as sendMsg } from '@/api/platform/message/innerMessage'
  15. import { save as saveNews, manage, internal } from '@/api/platform/system/news'
  16. import { bpmTaskSave, buildTask } from '@/api/platform/bpmn/bpmTask'
  17. import { onlyOfficeToPdf } from '@/api/platform/form/seal'
  18. import { downloadFile as download } from '@/business/platform/file/utils'
  19. import { removeFormData } from '@/api/platform/data/dataTemplate'
  20. import { queryPageList as queryRole } from '@/api/platform/org/employee'
  21. import exportExcel from '@/plugins/exportWithExcelJS'
  22. // 引入工具类
  23. import Utils from '@/utils/util'
  24. import ActionUtils from '@/utils/action'
  25. // base64解码
  26. const decode = (str) =>
  27. decodeURIComponent(
  28. window
  29. .atob(str)
  30. .split('')
  31. .map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
  32. .join('')
  33. )
  34. // 下载
  35. export const downloadByBlob = (o, name) => {
  36. if (!(o instanceof Blob)) {
  37. return
  38. }
  39. if (window.navigator.msSaveBlob) {
  40. window.navigator.msSaveBlob(o, name)
  41. } else {
  42. const URL = window.URL || window.webkitURL || window
  43. const e = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
  44. e.href = URL.createObjectURL(o)
  45. e.download = name
  46. if (document.all) {
  47. e.click()
  48. } else {
  49. const ev = document.createEvent('MouseEvents')
  50. ev.initMouseEvent(
  51. 'click',
  52. true,
  53. false,
  54. window,
  55. 0,
  56. 0,
  57. 0,
  58. 0,
  59. false,
  60. false,
  61. false,
  62. false,
  63. 0,
  64. null
  65. )
  66. e.dispatchEvent(ev)
  67. }
  68. }
  69. }
  70. // 获取当前时间
  71. export const getDateNow = (length = 10, formatType) => {
  72. if (formatType === 'string') {
  73. return new Date(new Date().getTime() + 28800000)
  74. .toJSON()
  75. .slice(0, length)
  76. .replace(/[-:T]/g, '')
  77. }
  78. return new Date(new Date().getTime() + 28800000)
  79. .toJSON()
  80. .slice(0, length)
  81. .replace('T', ' ')
  82. }
  83. // 获取指定值后的日期
  84. export const getDate = (type, value, date) => {
  85. const d = date || getDateNow(19)
  86. if (
  87. typeof type !== 'string' ||
  88. !Number.isInteger(value) ||
  89. typeof d !== 'string'
  90. ) {
  91. console.log('参数类型错误')
  92. return null
  93. }
  94. const now = new Date(d)
  95. const D = {
  96. day: value * 24 * 60 * 60 * 1000,
  97. hour: value * 60 * 60 * 1000,
  98. minute: value * 60 * 1000,
  99. second: value * 1000
  100. }
  101. const year = now.getFullYear()
  102. const month = now.getMonth()
  103. const day = now.getDate()
  104. const hour = now.getHours()
  105. const minute = now.getMinutes()
  106. const second = now.getSeconds()
  107. switch (type) {
  108. case 'year': {
  109. const isLeapYear = new Date(year + value, 1, 29).getDate() === 29
  110. return new Date(
  111. year + value,
  112. isLeapYear && month === 1 && day === 29 ? 1 : month,
  113. isLeapYear && month === 1 && day === 29 ? 29 : day,
  114. hour,
  115. minute,
  116. second
  117. )
  118. }
  119. case 'month': {
  120. let newYear = year
  121. let newMonth = month + value
  122. if (newMonth < 0) {
  123. newYear -= Math.ceil(Math.abs(newMonth) / 12)
  124. newMonth = 12 - (Math.abs(newMonth) % 12)
  125. } else if (newMonth > 11) {
  126. newYear += Math.floor(newMonth / 12)
  127. newMonth = newMonth % 12
  128. }
  129. const isLeapMonth = new Date(newYear, newMonth + 1, 0).getDate() === 29
  130. return new Date(
  131. newYear,
  132. isLeapMonth && month === 1 && day === 29 ? 1 : newMonth,
  133. isLeapMonth && month === 1 && day === 29 ? 29 : day,
  134. hour,
  135. minute,
  136. second
  137. )
  138. }
  139. case 'day':
  140. case 'hour':
  141. case 'minute':
  142. case 'second':
  143. return new Date(now.getTime() + D[type])
  144. default:
  145. console.log('无效的日期类型')
  146. return null
  147. }
  148. }
  149. // 时间格式化
  150. export const getFormatDate = (type, length, date = new Date()) => {
  151. const now = new Date(new Date(date).getTime())
  152. // eslint-disable-next-line
  153. if (now == 'Invalid Date') {
  154. console.log('非法日期,无法格式化')
  155. return date
  156. }
  157. const year = now.getFullYear()
  158. const month = now.getMonth() + 1
  159. const day = now.getDate()
  160. const hours = now.getHours()
  161. const minutes = now.getMinutes()
  162. const seconds = now.getSeconds()
  163. // 补零
  164. const padZero = (num) => {
  165. return num.toString().padStart(2, '0')
  166. }
  167. // 默认返回String类型
  168. let result = `${year}-${padZero(month)}-${padZero(day)} ${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`
  169. switch (type) {
  170. case 'string':
  171. result = `${year}-${padZero(month)}-${padZero(day)} ${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`
  172. break
  173. case 'cn':
  174. result = `${year}年${padZero(month)}月${padZero(day)}日 ${padZero(hours)}时${padZero(minutes)}分${padZero(seconds)}秒`
  175. break
  176. case 'number':
  177. result = `${year}${padZero(month)}${padZero(day)}${padZero(hours)}${padZero(minutes)}${padZero(seconds)}`
  178. break
  179. default:
  180. break
  181. }
  182. return result.slice(0, length || result.length)
  183. }
  184. /**
  185. * 替换对象中的null为空字符串
  186. * @param {Object} obj 目标对象
  187. */
  188. export const replaceNullWithEmpty = (obj) => {
  189. function replaceValue(value) {
  190. if (value === null || typeof value === 'undefined') {
  191. return ''
  192. } else if (typeof value === 'object') {
  193. if (Array.isArray(value)) {
  194. return value.map((item) => replaceValue(item))
  195. } else {
  196. return mapValues(value, (v) => replaceValue(v))
  197. }
  198. } else {
  199. return value
  200. }
  201. }
  202. return replaceValue(obj)
  203. }
  204. export const export2Excel = (options) => {
  205. // options 参数属性值参考:src\plugins\exportWithExcelJS\readme.md
  206. exportExcel(options).then(() => {
  207. ActionUtils.success(options.message || '导出成功')
  208. })
  209. }
  210. export default {
  211. preview,
  212. request,
  213. pinyin,
  214. pinyin4js,
  215. snapshoot,
  216. getNextIdByAlias,
  217. reagentConsumablesInventory,
  218. decode,
  219. encryptByAes,
  220. decryptByAes,
  221. downloadByBlob,
  222. sendMsg,
  223. saveNews,
  224. internal,
  225. manage,
  226. bpmTaskSave,
  227. buildTask,
  228. getDate,
  229. getDateNow,
  230. getFormatDate,
  231. onlyOfficeToPdf,
  232. generateUUID: Utils.guid,
  233. download,
  234. removeFormData,
  235. replaceNullWithEmpty,
  236. queryRole,
  237. export2Excel,
  238. Utils,
  239. getToken,
  240. ActionUtils
  241. }