gaoluyang
2026-08-31 8f8dcdae7e40ba4e94a363aa8eb91c7665810bc3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<script lang="ts" setup>
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
 
import dayjs from 'dayjs';
 
import { Button, message } from 'ant-design-vue';
 
import { startWaitCountdown } from '#/api/mes/pro/task';
 
defineOptions({ name: 'WaitCountdown' });
 
interface Props {
  taskId?: number;
  /** 等待时长(小时),>0 才展示倒计时 */
  waitDuration?: number;
  /** 倒计时开始时间(yyyy-MM-dd HH:mm:ss) */
  waitStartTime?: string;
  /** 只读模式(详情/审批)下不展示开始按钮 */
  disabled?: boolean;
}
 
const props = withDefaults(defineProps<Props>(), {
  taskId: undefined,
  waitDuration: undefined,
  waitStartTime: undefined,
  disabled: false,
});
 
const starting = ref(false);
const startTime = ref<string | undefined>(props.waitStartTime);
const now = ref(Date.now());
let timer: ReturnType<typeof setInterval> | undefined;
 
const showCountdown = computed(
  () => !!props.waitDuration && props.waitDuration > 0,
);
 
const endTime = computed(() => {
  if (!startTime.value || !showCountdown.value) {
    return undefined;
  }
  return dayjs(startTime.value, 'YYYY-MM-DD HH:mm:ss').valueOf() +
    props.waitDuration! * 3600 * 1000;
});
 
const remainingMs = computed(() => {
  if (!endTime.value) {
    return 0;
  }
  return Math.max(0, endTime.value - now.value);
});
 
const counting = computed(() => remainingMs.value > 0);
 
const remainingText = computed(() => {
  const totalSeconds = Math.ceil(remainingMs.value / 1000);
  const hours = Math.floor(totalSeconds / 3600);
  const minutes = Math.floor((totalSeconds % 3600) / 60);
  const seconds = totalSeconds % 60;
  const pad = (n: number) => String(n).padStart(2, '0');
  if (hours > 0) {
    return `剩余 ${hours} 小时 ${pad(minutes)} 分 ${pad(seconds)} 秒`;
  }
  return `剩余 ${pad(minutes)} 分 ${pad(seconds)} 秒`;
});
 
function startTimer() {
  if (timer) {
    clearInterval(timer);
  }
  timer = setInterval(() => {
    now.value = Date.now();
    if (remainingMs.value <= 0) {
      clearInterval(timer);
      timer = undefined;
    }
  }, 1000);
}
 
watch(
  () => props.waitStartTime,
  (value) => {
    startTime.value = value;
    if (value) {
      now.value = Date.now();
      startTimer();
    }
  },
);
 
onMounted(() => {
  if (startTime.value) {
    startTimer();
  }
});
 
onBeforeUnmount(() => {
  if (timer) {
    clearInterval(timer);
    timer = undefined;
  }
});
 
async function handleStart() {
  if (!props.taskId) {
    message.warning('请先选择任务');
    return;
  }
  starting.value = true;
  try {
    const time = await startWaitCountdown(props.taskId);
    startTime.value = time;
    now.value = Date.now();
    startTimer();
    message.success('倒计时已开始');
  } finally {
    starting.value = false;
  }
}
</script>
 
<template>
  <div v-if="showCountdown" class="flex items-center gap-2">
    <Button
      v-if="!startTime && !disabled"
      type="primary"
      :loading="starting"
      @click="handleStart"
    >
      开始倒计时
    </Button>
    <span v-if="startTime" class="text-sm">
      <span v-if="counting" class="text-orange-500">{{ remainingText }}</span>
      <span v-else class="text-green-600">倒计时已结束,可报工</span>
    </span>
  </div>
</template>