common.js 6.1 KB

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