No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

261 líneas
7.5 KiB

  1. /**
  2. * 唯一的随机字符串,用来区分每条数据
  3. * @returns {string}
  4. */
  5. export function getUid() {
  6. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
  7. const r = (Math.random() * 16) | 0;
  8. const v = c === 'x' ? r : (r & 0x3) | 0x8;
  9. return v.toString(16);
  10. });
  11. }
  12. /**
  13. * 计算时间差
  14. * @param beginTime:2022-01-13
  15. * @param endTime:2022-01-13
  16. * @returns {{hours: number, seconds: number, minutes: number, day: number}}
  17. */
  18. export function dealTime(beginTime, endTime) {
  19. var dateBegin = new Date(beginTime);
  20. var dateEnd = new Date(endTime);
  21. var dateDiff = dateEnd.getTime() - dateBegin.getTime(); //时间差的毫秒数
  22. var day = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
  23. var leave1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  24. var hours = Math.floor(leave1 / (3600 * 1000)); //计算出小时数
  25. //计算相差分钟数
  26. var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  27. var minutes = Math.floor(leave2 / (60 * 1000)); //计算相差分钟数
  28. //计算相差秒数
  29. var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  30. var seconds = Math.round(leave3 / 1000);
  31. return {
  32. day,
  33. hours,
  34. minutes,
  35. seconds
  36. }
  37. }
  38. /**
  39. * 获取当天时间
  40. * @returns {string}
  41. */
  42. export function getCurDay() {
  43. var datetime = new Date();
  44. var year = datetime.getFullYear();
  45. var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
  46. var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
  47. return `${year}-${month}-${date}`
  48. }
  49. // 日期格式处理
  50. export function formatDate(date) {
  51. var year = date.getFullYear();
  52. var months = date.getMonth() + 1;
  53. var month = (months < 10 ? '0' + months : months).toString();
  54. var day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()).toString();
  55. return {
  56. year: year.toString(),
  57. month,
  58. day
  59. }
  60. }
  61. /**
  62. * 数组中,某个属性相同的数据放在一块,如把某个日期相同的相连一起
  63. * @param list 传入的数组
  64. * @param prop 那个属性相同的数据
  65. * @returns {*[]}
  66. */
  67. export function margePropData(list = [], prop) {
  68. let arr = [], tempArr = {};
  69. list.forEach(item => {
  70. if (!tempArr[item[prop]]) {
  71. tempArr[item[prop]] = [item]
  72. } else {
  73. tempArr[item[prop]].push(item)
  74. }
  75. })
  76. for (const tempArrKey in tempArr) {
  77. arr = [...arr, ...tempArr[tempArrKey]]
  78. }
  79. return arr
  80. }
  81. /**
  82. * 合并行
  83. * @param list
  84. * @param prop
  85. */
  86. export function mergeRows(list = [], prop) {
  87. list.forEach(ele => {
  88. ele.rowspan = 1
  89. })
  90. const len = list.length
  91. for (let i = 0; i < len; i++) {
  92. for (let j = i + 1; j < len; j++) {
  93. if (list[i][prop] === list[j][prop]) {
  94. list[i].rowspan++
  95. list[j].rowspan--
  96. }
  97. }
  98. // 这里跳过已经重复的数据
  99. i = i + list[i].rowspan - 1
  100. }
  101. return list
  102. }
  103. /**
  104. * 根据当前数据的位置,在数组中插入数据
  105. * 如数组【1,2,4,5】想要在2后面插入3,
  106. *1:首先获取到2的下标,
  107. *2:然后获取要插入之前的数据,获取要插入之后的数据,中间就是插入的位置
  108. *3:最后把这三个按顺序合并就得到在想要的位置插入数据
  109. * @param list
  110. * @param index
  111. * @param target
  112. */
  113. export function insertArrPositionOfIndex(list = [], index = 0, target = {}) {
  114. //根据index 找出小于index的数据放在左边
  115. const leftList = list.filter((t, i) => i < index);
  116. //根据index 找出大于index的数据放在右边
  117. const rightList = list.filter((t, i) => i >= index);
  118. // 最终合并数据
  119. return [...leftList, target, ...rightList]
  120. }
  121. /**
  122. * 校验规则
  123. */
  124. export function verifyRules(list = [], require = []) {
  125. let message = null
  126. for (const key of require) {
  127. const isEmpty = list.every(it => !it[key.prop])
  128. if (isEmpty) {
  129. message = key.message
  130. break;
  131. }
  132. }
  133. return message
  134. }
  135. /**
  136. * 获取元素下标
  137. * @param dir 为 1:得到正序遍历方法;为 -1: 得到逆序遍历方法。
  138. * @returns {(function(*, *, *=): (number|number|number))|*}
  139. */
  140. export function findArrIndex(dir = 1) {
  141. return function (array, cb, context) {
  142. let length = array.length;
  143. // 控制初始 index,0 或者 length-1
  144. let index = dir >= 0 ? 0 : length - 1;
  145. // 条件: 在数组范围内;
  146. // 递增或递减:递加 1 或者 -1; 妙啊~
  147. for (; index >= 0 && index <= length - 1; index += dir) {
  148. if (cb.call(context, array[index], index)) return index
  149. }
  150. return -1
  151. }
  152. }
  153. /**
  154. * map转换成数组
  155. * @param target
  156. * @returns {*[]}
  157. * @constructor
  158. */
  159. export function MapConvertArr(target = {}) {
  160. let list = [];
  161. for (let key in target) {
  162. list.push(target[key]);
  163. }
  164. return list;
  165. }
  166. /**
  167. * 对象数组去重
  168. * @param arr 数组
  169. * @param prop 根据什么字段去重
  170. * @returns {any[]}
  171. */
  172. export function arrayDeduplication(arr, prop) {
  173. let map = new Map();
  174. return arr.filter(item => !map.has(item[prop]) && map.set(item[prop], 1));
  175. }
  176. /**
  177. * 获取当前天时间
  178. * @param param 【Y:年;M:月;D:日;h:小时;m:分钟;s:秒;】 默认精确到秒
  179. * @returns {*}
  180. */
  181. export function getCurrentDate(param = 's') {
  182. var now = new Date();
  183. var year = now.getFullYear(); //得到年份
  184. var month = now.getMonth();//得到月份
  185. var date = now.getDate();//得到日期
  186. var day = now.getDay();//得到周几
  187. var hour = now.getHours();//得到小时
  188. var minu = now.getMinutes();//得到分钟
  189. var sec = now.getSeconds();//得到秒
  190. month = month + 1;
  191. if (month < 10) month = "0" + month;
  192. if (date < 10) date = "0" + date;
  193. if (hour < 10) hour = "0" + hour;
  194. if (minu < 10) minu = "0" + minu;
  195. if (sec < 10) sec = "0" + sec;
  196. const arr = {
  197. 'Y': year,
  198. 'M': year + "-" + month,
  199. 'D': year + "-" + month + "-" + date,
  200. 'h': year + "-" + month + "-" + date + " " + hour,
  201. 'm': year + "-" + month + "-" + date + " " + hour + ":" + minu,
  202. 's': year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec
  203. }
  204. return arr[param];
  205. }
  206. /**
  207. * 获取当天时间前后七天时间
  208. * @param day day>0 当天时间的后几天 day<0 当天时间前几天
  209. * @returns {string}
  210. */
  211. export function getRecentDate(day) {
  212. var date1 = new Date(),
  213. time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();//time1表示当前时间
  214. var date2 = new Date(date1);
  215. date2.setDate(date1.getDate() + day);
  216. const y = date2.getFullYear();
  217. const m = (date2.getMonth() + 1) > 9 ? (date2.getMonth() + 1) : '0' + (date2.getMonth() + 1)
  218. const d = date2.getDate() > 9 ? date2.getDate() : '0' + date2.getDate()
  219. return y + "-" + m + "-" + d;
  220. }
  221. export function MyDebounce(fn, duration = 100) {
  222. let timer = null
  223. return (...args) => {
  224. clearTimeout(timer)
  225. timer = setTimeout(() => {
  226. fn(...args);
  227. }, duration)
  228. }
  229. }
  230. export function MyThrottle(fn, duration = 100) {
  231. let target = true;
  232. return (...arg) => {
  233. if (!target) {
  234. return false;
  235. }
  236. target = false;
  237. setTimeout(() => {
  238. fn(...arg);
  239. target = true
  240. }, duration)
  241. }
  242. }