utils.js 6.2 KB

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