ny
23 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import { computed, reactive } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
 
import { useClipboard } from '@vueuse/core';
 
import { getWebhookParams as workFlowGetWebhookParams, webhookStart as workFlowWebhookStart } from '#/api/workFlow/template';
import { useBaseStore } from '#/store';
 
interface State {
  url: string;
  randomStr: string;
  id: string;
  loading: boolean;
  isRetry: boolean;
  timer: any;
  type: string;
}
 
const emit = defineEmits(['register', 'confirm']);
 
const state = reactive<State>({
  url: '',
  randomStr: '',
  id: '',
  loading: false,
  isRetry: false,
  timer: null,
  type: '',
});
 
const { createMessage } = useMessage();
const baseStore = useBaseStore();
const [registerModal, { closeModal }] = useModalInner(init);
 
const getSysConfig = computed(() => baseStore.sysConfigInfo);
const { copy } = useClipboard({ legacy: true });
function init(data) {
  state.url = data.url;
  state.randomStr = data.randomStr;
  state.id = data.id;
  state.loading = false;
  state.isRetry = false;
  state.type = data.type;
}
function handleCopy(text) {
  copy(text);
  createMessage.success('复制成功');
}
function handleRequest() {
  const webhookStart = workFlowWebhookStart;
  webhookStart(state.id, state.randomStr).then((_) => {
    state.loading = true;
    state.isRetry = false;
    state.timer = setInterval(() => {
      handleWebhookParams();
    }, 1000);
    setTimeout(() => {
      clearTimer();
      state.isRetry = true;
    }, 180000);
  });
}
function clearTimer() {
  if (!state.timer) return;
  clearInterval(state.timer);
  state.timer = null;
}
function handleWebhookParams() {
  const getWebhookParams = workFlowGetWebhookParams;
  getWebhookParams(state.randomStr)
    .then((res) => {
      if (res?.data.length) {
        emit('confirm', res.data);
        clearTimer();
        closeModal();
      }
    })
    .catch(() => {
      clearTimer();
      state.isRetry = true;
    });
}
async function onClose() {
  clearTimer();
  return true;
}
</script>
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="请求接口" :close-func="onClose" :footer="null" :width="600" class="webhook-request-modal">
    <div class="main">
      <div class="mb-[10px]">向该接口发送请求,通过接口生成接口字段!</div>
      <a-input :value="getSysConfig.jnpfDomain + state.url" readonly>
        <template #addonAfter>
          <span class="cursor-pointer" @click="handleCopy(getSysConfig.jnpfDomain + state.url)">复制链接</span>
        </template>
      </a-input>
      <div class="mt-[20px]">
        <a-button type="primary" @click="handleRequest" v-if="!state.loading">接收请求</a-button>
        <template v-else>
          <a-alert message="请在3分钟内向URL发送一条GET或POST请求" type="warning" show-icon />
          <div class="tip" v-if="!state.isRetry">正在接收请求......</div>
          <template v-else>
            <div class="tip tip-error">当前URL没有接收到任何请求,是否重试?</div>
            <a-button type="primary" @click="handleRequest" class="mt-[10px]">重试</a-button>
          </template>
        </template>
      </div>
    </div>
  </BasicModal>
</template>
<style lang="scss">
.webhook-request-modal {
  .main {
    height: 300px;
 
    .tip {
      height: 40px;
      padding: 0 15px;
      margin-top: 10px;
      line-height: 40px;
      color: var(--primary-color);
      background-color: var(--app-main-background);
 
      &.tip-error {
        color: var(--error-color);
      }
    }
  }
}
</style>