index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { clockState, clockJournalState } from './config';
  2. import dayjs from "dayjs";
  3. import Taro from "@tarojs/taro";
  4. export const getClockState = (id, journal = false) => {
  5. let arr = journal ? clockJournalState.filter(v => v.id === id) : clockState.filter(v => v.id === id);
  6. if (arr.length > 0) {
  7. return arr[0];
  8. } else {
  9. return {};
  10. }
  11. }
  12. //获取用户工作时间区间
  13. export const getUserWordTimes = () => {
  14. let userInfor = Taro.getStorageSync('userInfor');
  15. return userInfor.workTimeInfor;
  16. }
  17. //获取当天公出小时数
  18. export const getHours = (startTime, endTime) => {
  19. const { restStart, restEnd } = getUserWordTimes();
  20. let a1 = dayjs(startTime);
  21. let b1 = dayjs(endTime);
  22. let c1 = b1.diff(a1, 'minutes');
  23. let hourse = parseInt(c1 / 60);
  24. let minutes = c1 % 60
  25. let minutes1 = parseInt(minutes / 30) * 0.5;
  26. //开始时间在12:00之前 包括12:00
  27. if (!a1.isAfter(dayjs(a1.format('YYYY-MM-DD') + restStart))) {
  28. if (b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restEnd))) {
  29. return hourse + minutes1 - 1.5
  30. } else if (!b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restStart))) {
  31. return hourse + minutes1
  32. } else {
  33. let a2 = dayjs(startTime);
  34. let b2 = dayjs(b1.format('YYYY-MM-DD') + restStart);
  35. let c2 = b2.diff(a2, 'minutes');
  36. let hourse2 = parseInt(c2 / 60);
  37. let minutes2 = c2 % 60
  38. let minutes3 = parseInt(minutes2 / 30) * 0.5;
  39. return hourse2 + minutes3
  40. }
  41. // 开始时间在12:00之后13:30之前
  42. } else if (a1.isBefore(dayjs(a1.format('YYYY-MM-DD') + restEnd)) && a1.isAfter(dayjs(a1.format('YYYY-MM-DD') + restStart))) {
  43. if (b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restEnd))) {
  44. let a2 = dayjs(a1.format('YYYY-MM-DD') + restEnd);
  45. let b2 = dayjs(endTime);
  46. let c2 = b2.diff(a2, 'minutes');
  47. let hourse2 = parseInt(c2 / 60);
  48. let minutes2 = c2 % 60
  49. let minutes3 = parseInt(minutes2 / 30) * 0.5;
  50. return hourse2 + minutes3
  51. } else {
  52. return 0;
  53. }
  54. //开始时间在13:30之后 包括13:30
  55. } else if (!a1.isBefore(dayjs(a1.format('YYYY-MM-DD') + restEnd))) {
  56. return hourse + minutes1
  57. }
  58. }
  59. //获取总时长
  60. export const getNumHourse = (startTimeValue, endTimeValue, days) => {
  61. const { start, end } = getUserWordTimes();
  62. //开始时间 向下取整 结束时间向上取整
  63. let startTimeArr = [parseInt(dayjs(startTimeValue).format('HH')), parseInt(dayjs(startTimeValue).format('mm'))];
  64. let endTimeArr = [parseInt(dayjs(endTimeValue).format('HH')), parseInt(dayjs(endTimeValue).format('mm'))];
  65. if (startTimeArr[1] > 0 && startTimeArr[1] < 30) {
  66. startTimeArr[1] = 30;
  67. } else if (startTimeArr[1] > 30) {
  68. startTimeArr[0] = startTimeArr[0] + 1;
  69. startTimeArr[1] = 0;
  70. }
  71. if (endTimeArr[1] > 0 && endTimeArr[1] < 30) {
  72. endTimeArr[1] = 0;
  73. } else if (startTimeArr[1] > 30) {
  74. endTimeArr[1] = 30;
  75. }
  76. let startTime = dayjs(startTimeValue).format('YYYY-MM-DD') + ' ' + startTimeArr.join(':')
  77. let endTime = dayjs(endTimeValue).format('YYYY-MM-DD') + ' ' + endTimeArr.join(':')
  78. if (dayjs(dayjs(startTime).format('YYYY-MM-DD')).isSame(dayjs(dayjs(endTime).format('YYYY-MM-DD')))) {
  79. let num = getHours(startTime, endTime)
  80. if (num < 0) {
  81. return 0
  82. } else {
  83. return getHours(startTime, endTime)
  84. }
  85. } else {
  86. let a = getHours(startTime, dayjs(dayjs(startTime).format('YYYY-MM-DD') + end));
  87. let b = getHours(dayjs(dayjs(endTime).format('YYYY-MM-DD') + start), endTime);
  88. let totalDuration = getHours(dayjs(startTime).format('YYYY-MM-DD') + start, dayjs(dayjs(startTime).format('YYYY-MM-DD') + end)); //一天工作时长
  89. let c = (days - 2) * totalDuration;
  90. return a + b + c;
  91. }
  92. }
  93. function addZero(num) {
  94. return Number(num) < 10 ? `0${num}` : num;
  95. }
  96. export const formatDate = (year, month, day, hour, minute) => {
  97. const newmonth = addZero(month);
  98. const newday = addZero(day);
  99. const newhour = addZero(hour);
  100. const newminute = addZero(minute);
  101. return year + '-' + newmonth + '-' + newday + ' ' + newhour + ":" + newminute;
  102. };
  103. export const deepClone1 = (obj) => {
  104. //判断拷贝的要进行深拷贝的是数组还是对象,是数组的话进行数组拷贝,对象的话进行对象拷贝
  105. var objClone = Array.isArray(obj) ? [] : {};
  106. //进行深拷贝的不能为空,并且是对象或者是
  107. if (obj && typeof obj === "object") {
  108. for (key in obj) {
  109. if (obj.hasOwnProperty(key)) {
  110. if (obj[key] && typeof obj[key] === "object") {
  111. objClone[key] = deepClone1(obj[key]);
  112. } else {
  113. objClone[key] = obj[key];
  114. }
  115. }
  116. }
  117. }
  118. return objClone;
  119. }
  120. // 是否是登录人的id
  121. export const isOneself = (id) => {
  122. let userInfor = Taro.getStorageSync('userInfor');
  123. if (userInfor.aid === id) {
  124. return true
  125. } else {
  126. return false
  127. }
  128. }