123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { clockState, clockJournalState } from './config';
- import dayjs from "dayjs";
- import Taro from "@tarojs/taro";
- export const getClockState = (id, journal = false) => {
- let arr = journal ? clockJournalState.filter(v => v.id === id) : clockState.filter(v => v.id === id);
- if (arr.length > 0) {
- return arr[0];
- } else {
- return {};
- }
- }
- //获取用户工作时间区间
- export const getUserWordTimes = () => {
- let userInfor = Taro.getStorageSync('userInfor');
- return userInfor.workTimeInfor;
- }
- //获取当天公出小时数
- export const getHours = (startTime, endTime) => {
- const { restStart, restEnd } = getUserWordTimes();
- let a1 = dayjs(startTime);
- let b1 = dayjs(endTime);
- let c1 = b1.diff(a1, 'minutes');
- let hourse = parseInt(c1 / 60);
- let minutes = c1 % 60
- let minutes1 = parseInt(minutes / 30) * 0.5;
- //开始时间在12:00之前 包括12:00
- if (!a1.isAfter(dayjs(a1.format('YYYY-MM-DD') + restStart))) {
- if (b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restEnd))) {
- return hourse + minutes1 - 1.5
- } else if (!b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restStart))) {
- return hourse + minutes1
- } else {
- let a2 = dayjs(startTime);
- let b2 = dayjs(b1.format('YYYY-MM-DD') + restStart);
- let c2 = b2.diff(a2, 'minutes');
- let hourse2 = parseInt(c2 / 60);
- let minutes2 = c2 % 60
- let minutes3 = parseInt(minutes2 / 30) * 0.5;
- return hourse2 + minutes3
- }
- // 开始时间在12:00之后13:30之前
- } else if (a1.isBefore(dayjs(a1.format('YYYY-MM-DD') + restEnd)) && a1.isAfter(dayjs(a1.format('YYYY-MM-DD') + restStart))) {
- if (b1.isAfter(dayjs(b1.format('YYYY-MM-DD') + restEnd))) {
- let a2 = dayjs(a1.format('YYYY-MM-DD') + restEnd);
- let b2 = dayjs(endTime);
- let c2 = b2.diff(a2, 'minutes');
- let hourse2 = parseInt(c2 / 60);
- let minutes2 = c2 % 60
- let minutes3 = parseInt(minutes2 / 30) * 0.5;
- return hourse2 + minutes3
- } else {
- return 0;
- }
- //开始时间在13:30之后 包括13:30
- } else if (!a1.isBefore(dayjs(a1.format('YYYY-MM-DD') + restEnd))) {
- return hourse + minutes1
- }
- }
- //获取总时长
- export const getNumHourse = (startTimeValue, endTimeValue, days) => {
- const { start, end } = getUserWordTimes();
- //开始时间 向下取整 结束时间向上取整
- let startTimeArr = [parseInt(dayjs(startTimeValue).format('HH')), parseInt(dayjs(startTimeValue).format('mm'))];
- let endTimeArr = [parseInt(dayjs(endTimeValue).format('HH')), parseInt(dayjs(endTimeValue).format('mm'))];
- if (startTimeArr[1] > 0 && startTimeArr[1] < 30) {
- startTimeArr[1] = 30;
- } else if (startTimeArr[1] > 30) {
- startTimeArr[0] = startTimeArr[0] + 1;
- startTimeArr[1] = 0;
- }
- if (endTimeArr[1] > 0 && endTimeArr[1] < 30) {
- endTimeArr[1] = 0;
- } else if (startTimeArr[1] > 30) {
- endTimeArr[1] = 30;
- }
- let startTime = dayjs(startTimeValue).format('YYYY-MM-DD') + ' ' + startTimeArr.join(':')
- let endTime = dayjs(endTimeValue).format('YYYY-MM-DD') + ' ' + endTimeArr.join(':')
- if (dayjs(dayjs(startTime).format('YYYY-MM-DD')).isSame(dayjs(dayjs(endTime).format('YYYY-MM-DD')))) {
- let num = getHours(startTime, endTime)
- if (num < 0) {
- return 0
- } else {
- return getHours(startTime, endTime)
- }
- } else {
- let a = getHours(startTime, dayjs(dayjs(startTime).format('YYYY-MM-DD') + end));
- let b = getHours(dayjs(dayjs(endTime).format('YYYY-MM-DD') + start), endTime);
- let totalDuration = getHours(dayjs(startTime).format('YYYY-MM-DD') + start, dayjs(dayjs(startTime).format('YYYY-MM-DD') + end)); //一天工作时长
- let c = (days - 2) * totalDuration;
- return a + b + c;
- }
- }
- function addZero(num) {
- return Number(num) < 10 ? `0${num}` : num;
- }
- export const formatDate = (year, month, day, hour, minute) => {
- const newmonth = addZero(month);
- const newday = addZero(day);
- const newhour = addZero(hour);
- const newminute = addZero(minute);
- return year + '-' + newmonth + '-' + newday + ' ' + newhour + ":" + newminute;
- };
- export const deepClone1 = (obj) => {
- //判断拷贝的要进行深拷贝的是数组还是对象,是数组的话进行数组拷贝,对象的话进行对象拷贝
- var objClone = Array.isArray(obj) ? [] : {};
- //进行深拷贝的不能为空,并且是对象或者是
- if (obj && typeof obj === "object") {
- for (key in obj) {
- if (obj.hasOwnProperty(key)) {
- if (obj[key] && typeof obj[key] === "object") {
- objClone[key] = deepClone1(obj[key]);
- } else {
- objClone[key] = obj[key];
- }
- }
- }
- }
- return objClone;
- }
- // 是否是登录人的id
- export const isOneself = (id) => {
- let userInfor = Taro.getStorageSync('userInfor');
- if (userInfor.aid === id) {
- return true
- } else {
- return false
- }
- }
|