utils.js 6.3 KB

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