utils.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //格式化日期
  2. export function getDate(type) {
  3. const date = new Date();
  4. let year = date.getFullYear();
  5. let month = date.getMonth() + 1;
  6. let day = date.getDate();
  7. if (type === 'end') {
  8. day = day + 1;
  9. }
  10. month = month > 9 ? month : '0' + month;
  11. day = day > 9 ? day : '0' + day;
  12. return `${year}-${month}-${day}`;
  13. }
  14. export function getDateNew(type = 'day') {
  15. const date = new Date();
  16. let year = date.getFullYear();
  17. let month = date.getMonth() + 1;
  18. let day = date.getDate();
  19. if (type === 'end') {
  20. day = day + 1;
  21. }
  22. month = month > 9 ? month : '0' + month;
  23. day = day > 9 ? day : '0' + day;
  24. if (type === 'year') return `${year}`
  25. if (type === 'month') return `${year}-${month}`
  26. return `${year}-${month}-${day}`;
  27. }
  28. /**
  29. * @description: 获取某月的起始、结束日期
  30. * @param {*} val 年月 2023-01
  31. * @returns
  32. */
  33. export function getMonday (val) {
  34. let date = new Date(val)
  35. let new_year = date.getFullYear() // 取当前的年份
  36. let month = date.getMonth()
  37. let new_month = month + 1 // 取当前的月份
  38. let mon = ''
  39. let day = ''
  40. if (month > 12) {
  41. new_month -= 12 // 月份减
  42. new_year++ // 年份增
  43. }
  44. let firstDay = new Date(new_year, new_month, 1) // 取当年当月中的第一天
  45. let lastDay = new Date(firstDay.getTime() - 1000 * 60 * 60 * 24).getDate() // 获取当月最后一天日期
  46. if (firstDay.getMonth() < 10) {
  47. mon = '0' + firstDay.getMonth()
  48. } else {
  49. mon = firstDay.getMonth()
  50. }
  51. if (lastDay < 10) {
  52. day = '0' + lastDay
  53. } else {
  54. day = lastDay
  55. }
  56. let startDate =
  57. firstDay.getFullYear() + '-' + mon + '-' + '0' + firstDay.getDate()
  58. let endDate = firstDay.getFullYear() + '-' + mon + '-' + day
  59. let dateRange = [startDate, endDate]
  60. return dateRange
  61. }
  62. export function initDict(originalData) {
  63. let obj = {}
  64. let arr = originalData.map(item => {
  65. const key = Object.keys(item)[0]
  66. const value = item[key]
  67. obj[key] = value
  68. return {
  69. label: value,
  70. value: parseInt(key)
  71. }
  72. })
  73. return [arr, obj]
  74. }
  75. export const recordingMethodList = [
  76. { label: '按质检项检', value: 1 },
  77. { label: '按样品检', value: 2 }
  78. ];
  79. export function stopScroll() {
  80. var box = function(e) {
  81. passive: false;
  82. };
  83. document.body.style.overflow = 'hidden';
  84. document.addEventListener("touchmove", box, false);
  85. }
  86. export function startScroll() {
  87. var box = function(e) {
  88. passive: false
  89. };
  90. document.body.style.overflow = ''; //出现滚动条
  91. document.removeEventListener("touchmove", box, false);
  92. }
  93. // 生成随机数
  94. export const getRuleNo = (suffix = 'R') => {
  95. const randomNum = Math.floor(Math.random() * 1000000)
  96. return `${suffix}${parseTime(new Date(), '{y}{m}{d}')}${randomNum}`
  97. }
  98. export function parseTime(time, cFormat) {
  99. if (arguments.length === 0 || !time) {
  100. return null
  101. }
  102. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  103. let date
  104. if (typeof time === 'object') {
  105. date = time
  106. } else {
  107. if (typeof time === 'string') {
  108. if (/^[0-9]+$/.test(time)) {
  109. // support "1548221490638"
  110. time = parseInt(time)
  111. } else {
  112. // support safari
  113. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  114. time = time.replace(new RegExp(/-/gm), '/')
  115. }
  116. }
  117. if (typeof time === 'number' && time.toString().length === 10) {
  118. time = time * 1000
  119. }
  120. date = new Date(time)
  121. }
  122. const formatObj = {
  123. y: date.getFullYear(),
  124. m: date.getMonth() + 1,
  125. d: date.getDate(),
  126. h: date.getHours(),
  127. i: date.getMinutes(),
  128. s: date.getSeconds(),
  129. a: date.getDay()
  130. }
  131. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  132. const value = formatObj[key]
  133. // Note: getDay() returns 0 on Sunday
  134. if (key === 'a') {
  135. return ['日', '一', '二', '三', '四', '五', '六'][value]
  136. }
  137. return value.toString().padStart(2, '0')
  138. })
  139. return time_str
  140. }
  141. /**
  142. * 将毫秒,转换成时间字符串。例如说,xx 分钟
  143. *
  144. * @param ms 毫秒
  145. * @returns {string} 字符串
  146. */
  147. export function getDates(ms) {
  148. const day = Math.floor(ms / (24 * 60 * 60 * 1000));
  149. const hour = Math.floor((ms / (60 * 60 * 1000) - day * 24));
  150. const minute = Math.floor(((ms / (60 * 1000)) - day * 24 * 60 - hour * 60));
  151. const second = Math.floor((ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60));
  152. if (day > 0) {
  153. return day + "天" + hour + "小时" + minute + "分钟";
  154. }
  155. if (hour > 0) {
  156. return hour + "小时" + minute + "分钟";
  157. }
  158. if (minute > 0) {
  159. return minute + "分钟";
  160. }
  161. if (second > 0) {
  162. return second + "秒";
  163. } else {
  164. return 0 + "秒";
  165. }
  166. }
  167. //机构一维数组转树形结构
  168. export function toTreeData(option) {
  169. var _a, _b;
  170. if (Array.isArray(option)) {
  171. option = {
  172. data: arguments[0],
  173. idField: arguments[1],
  174. parentIdField: arguments[2],
  175. childrenField: arguments[3],
  176. parentId: arguments[4],
  177. addParentIds: arguments[5],
  178. parentIdsField: arguments[6],
  179. parentIds: arguments[7]
  180. };
  181. }
  182. const data = option.data;
  183. const idField = option.idField || option.idKey || "id";
  184. const parentIdField = option.parentIdField || option.pidKey || "parentId";
  185. const childrenField = option.childrenField || option.childKey || "children";
  186. const parentIdIsNull = typeof option.parentId === "undefined" && typeof option.pid === "undefined";
  187. const parentId = parentIdIsNull ? [] : (_a = option.parentId) != null ? _a : option.pid;
  188. const addParentIds = (_b = option.addParentIds) != null ? _b : option.addPIds;
  189. const parentIdsField = option.parentIdsField || option.parentsKey || "parentIds";
  190. const parentIds = option.parentIds;
  191. if (parentIdIsNull) {
  192. data.forEach((d) => {
  193. let flag = true;
  194. for (let i = 0; i < data.length; i++) {
  195. if (d[parentIdField] == data[i][idField]) {
  196. flag = false;
  197. break;
  198. }
  199. }
  200. if (flag) {
  201. parentId.push(d[parentIdField]);
  202. }
  203. });
  204. }
  205. const result = [];
  206. data.forEach((d) => {
  207. if (d[idField] == d[parentIdField]) {
  208. throw new Error(
  209. [
  210. "data error: {",
  211. idField + ": ",
  212. JSON.stringify(d[idField]),
  213. parentIdField + ": ",
  214. JSON.stringify(d[parentIdField]),
  215. "}"
  216. ].join("")
  217. );
  218. }
  219. const isArr = Array.isArray(parentId);
  220. const isParent = isArr ? parentId.includes(d[parentIdField]) : d[parentIdField] == parentId;
  221. if (isParent) {
  222. const r = {
  223. ...d
  224. };
  225. const children = toTreeData({
  226. data,
  227. idField,
  228. parentIdField,
  229. childrenField,
  230. parentId: d[idField],
  231. addParentIds,
  232. parentIdsField,
  233. parentIds: (parentIds != null ? parentIds : []).concat([d[idField]])
  234. });
  235. if (children.length > 0) {
  236. r[childrenField] = children;
  237. }
  238. if (addParentIds) {
  239. r[parentIdsField] = parentIds != null ? parentIds : [];
  240. }
  241. result.push(r);
  242. }
  243. });
  244. return result;
  245. }
  246. //判断搜索条件是否为空的函数
  247. export function isObjectEmpty(obj) {
  248. for (const key in obj) {
  249. if (obj.hasOwnProperty(key)) {
  250. const value = obj[key];
  251. if (typeof value === 'object' && value !== null) {
  252. if (Array.isArray(value)) {
  253. if (value.length > 0) {
  254. return false;
  255. }
  256. } else {
  257. if (!isObjectEmpty(value)) {
  258. return false;
  259. }
  260. }
  261. } else if (value !== null && value !== undefined && value !== '' && value !== 0 && value !== false) {
  262. return false;
  263. }
  264. }
  265. }
  266. return true;
  267. }
  268. //指定属性,对由对象组成的数组去重
  269. export function uniqueByProperty(arr, property) {
  270. const map = new Map();
  271. arr.forEach(item => {
  272. const key = item[property];
  273. if (!map.has(key)) {
  274. map.set(key, item);
  275. }
  276. });
  277. return Array.from(map.values());
  278. }