gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
<!--suppress HttpUrlsUsage -->
<script lang="ts" setup>
import { computed, onMounted, ref, watch } from 'vue';
 
import { isEmpty } from '..\..\..\..\..\..\packages\utils\src';
 
import { useVModel } from '@vueuse/core';
import { Form, Input, Select } from 'ant-design-vue';
 
import { IotDataSinkTypeEnum } from '#/api/iot/rule/data/sink';
 
import KeyValueEditor from './components/key-value-editor.vue';
 
const props = defineProps<{ modelValue: any }>();
const emit = defineEmits(['update:modelValue']);
const config = useVModel(props, 'modelValue', emit);
 
const urlPrefix = ref<'http://' | 'https://'>('http://');
const urlPath = ref('');
const fullUrl = computed(() =>
  urlPath.value ? urlPrefix.value + urlPath.value : '',
);
 
function syncUrlFields(url?: string) {
  if (url?.startsWith('https://')) {
    urlPrefix.value = 'https://';
    urlPath.value = url.slice(8);
  } else if (url?.startsWith('http://')) {
    urlPrefix.value = 'http://';
    urlPath.value = url.slice(7);
  } else {
    urlPath.value = url ?? '';
  }
}
 
watch([urlPrefix, urlPath], () => {
  config.value.url = fullUrl.value;
});
 
watch(
  () => config.value?.url,
  (url) => syncUrlFields(url),
  { immediate: true },
);
 
onMounted(() => {
  if (!isEmpty(config.value)) {
    syncUrlFields(config.value.url);
    return;
  }
  config.value = {
    type: `${IotDataSinkTypeEnum.HTTP}`,
    url: '',
    method: 'POST',
    headers: {},
    query: {},
    body: '',
  };
});
</script>
 
<template>
  <Form.Item
    :name="['config', 'url']"
    :rules="[{ required: true, message: '请求地址不能为空', trigger: 'blur' }]"
    label="请求地址"
  >
    <Input v-model:value="urlPath" placeholder="请输入请求地址">
      <template #addonBefore>
        <Select v-model:value="urlPrefix" class="w-[100px]">
          <Select.Option value="http://">http://</Select.Option>
          <Select.Option value="https://">https://</Select.Option>
        </Select>
      </template>
    </Input>
  </Form.Item>
  <Form.Item
    :name="['config', 'method']"
    :rules="[
      { required: true, message: '请求方法不能为空', trigger: 'change' },
    ]"
    label="请求方法"
  >
    <Select v-model:value="config.method" placeholder="请选择请求方法">
      <Select.Option value="GET">GET</Select.Option>
      <Select.Option value="POST">POST</Select.Option>
      <Select.Option value="PUT">PUT</Select.Option>
      <Select.Option value="DELETE">DELETE</Select.Option>
    </Select>
  </Form.Item>
  <Form.Item label="请求头">
    <KeyValueEditor v-model="config.headers" add-button-text="添加请求头" />
  </Form.Item>
  <Form.Item label="请求参数">
    <KeyValueEditor v-model="config.query" add-button-text="添加参数" />
  </Form.Item>
  <Form.Item label="请求体">
    <Input.TextArea
      v-model:value="config.body"
      placeholder="请输入内容"
      :rows="4"
    />
  </Form.Item>
</template>