common.js 6.1 KB

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