utils.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 stopScroll(){
  15. var box=function(e)
  16. {passive: false ;};
  17. document.body.style.overflow='hidden';
  18. document.addEventListener("touchmove",box,false);
  19. }
  20. export function startScroll(){
  21. var box=function(e){passive: false };
  22. document.body.style.overflow='';//出现滚动条
  23. document.removeEventListener("touchmove",box,false);
  24. }
  25. // 生成随机数
  26. export const getRuleNo = (suffix = 'R') => {
  27. const randomNum = Math.floor(Math.random() * 1000000)
  28. return `${suffix}${parseTime(new Date(), '{y}{m}{d}')}${randomNum}`
  29. }
  30. export function parseTime (time, cFormat) {
  31. if (arguments.length === 0 || !time) {
  32. return null
  33. }
  34. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  35. let date
  36. if (typeof time === 'object') {
  37. date = time
  38. } else {
  39. if (typeof time === 'string') {
  40. if (/^[0-9]+$/.test(time)) {
  41. // support "1548221490638"
  42. time = parseInt(time)
  43. } else {
  44. // support safari
  45. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  46. time = time.replace(new RegExp(/-/gm), '/')
  47. }
  48. }
  49. if (typeof time === 'number' && time.toString().length === 10) {
  50. time = time * 1000
  51. }
  52. date = new Date(time)
  53. }
  54. const formatObj = {
  55. y: date.getFullYear(),
  56. m: date.getMonth() + 1,
  57. d: date.getDate(),
  58. h: date.getHours(),
  59. i: date.getMinutes(),
  60. s: date.getSeconds(),
  61. a: date.getDay()
  62. }
  63. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  64. const value = formatObj[key]
  65. // Note: getDay() returns 0 on Sunday
  66. if (key === 'a') {
  67. return ['日', '一', '二', '三', '四', '五', '六'][value]
  68. }
  69. return value.toString().padStart(2, '0')
  70. })
  71. return time_str
  72. }
  73. /**
  74. * 将毫秒,转换成时间字符串。例如说,xx 分钟
  75. *
  76. * @param ms 毫秒
  77. * @returns {string} 字符串
  78. */
  79. export function getDates(ms) {
  80. const day = Math.floor(ms / (24 * 60 * 60 * 1000));
  81. const hour = Math.floor((ms / (60 * 60 * 1000) - day * 24));
  82. const minute = Math.floor(((ms / (60 * 1000)) - day * 24 * 60 - hour * 60));
  83. const second = Math.floor((ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60));
  84. if (day > 0) {
  85. return day + "天" + hour + "小时" + minute + "分钟";
  86. }
  87. if (hour > 0) {
  88. return hour + "小时" + minute + "分钟";
  89. }
  90. if (minute > 0) {
  91. return minute + "分钟";
  92. }
  93. if (second > 0) {
  94. return second + "秒";
  95. } else {
  96. return 0 + "秒";
  97. }
  98. }
  99. //机构一维数组转树形结构
  100. export function toTreeData(option) {
  101. var _a, _b;
  102. if (Array.isArray(option)) {
  103. option = {
  104. data: arguments[0],
  105. idField: arguments[1],
  106. parentIdField: arguments[2],
  107. childrenField: arguments[3],
  108. parentId: arguments[4],
  109. addParentIds: arguments[5],
  110. parentIdsField: arguments[6],
  111. parentIds: arguments[7]
  112. };
  113. }
  114. const data = option.data;
  115. const idField = option.idField || option.idKey || "id";
  116. const parentIdField = option.parentIdField || option.pidKey || "parentId";
  117. const childrenField = option.childrenField || option.childKey || "children";
  118. const parentIdIsNull = typeof option.parentId === "undefined" && typeof option.pid === "undefined";
  119. const parentId = parentIdIsNull ? [] : (_a = option.parentId) != null ? _a : option.pid;
  120. const addParentIds = (_b = option.addParentIds) != null ? _b : option.addPIds;
  121. const parentIdsField = option.parentIdsField || option.parentsKey || "parentIds";
  122. const parentIds = option.parentIds;
  123. if (parentIdIsNull) {
  124. data.forEach((d) => {
  125. let flag = true;
  126. for (let i = 0; i < data.length; i++) {
  127. if (d[parentIdField] == data[i][idField]) {
  128. flag = false;
  129. break;
  130. }
  131. }
  132. if (flag) {
  133. parentId.push(d[parentIdField]);
  134. }
  135. });
  136. }
  137. const result = [];
  138. data.forEach((d) => {
  139. if (d[idField] == d[parentIdField]) {
  140. throw new Error(
  141. [
  142. "data error: {",
  143. idField + ": ",
  144. JSON.stringify(d[idField]),
  145. parentIdField + ": ",
  146. JSON.stringify(d[parentIdField]),
  147. "}"
  148. ].join("")
  149. );
  150. }
  151. const isArr = Array.isArray(parentId);
  152. const isParent = isArr ? parentId.includes(d[parentIdField]) : d[parentIdField] == parentId;
  153. if (isParent) {
  154. const r = { ...d };
  155. const children = toTreeData({
  156. data,
  157. idField,
  158. parentIdField,
  159. childrenField,
  160. parentId: d[idField],
  161. addParentIds,
  162. parentIdsField,
  163. parentIds: (parentIds != null ? parentIds : []).concat([d[idField]])
  164. });
  165. if (children.length > 0) {
  166. r[childrenField] = children;
  167. }
  168. if (addParentIds) {
  169. r[parentIdsField] = parentIds != null ? parentIds : [];
  170. }
  171. result.push(r);
  172. }
  173. });
  174. return result;
  175. }