|
| 1 | +<template> |
| 2 | + <el-dialog |
| 3 | + v-model="dialogVisible" |
| 4 | + title="编辑请求头" |
| 5 | + width="600px" |
| 6 | + :close-on-click-modal="false" |
| 7 | + @close="handleClose" |
| 8 | + > |
| 9 | + <div class="header-editor"> |
| 10 | + <div class="header-list"> |
| 11 | + <div v-for="(item, index) in headerList" :key="index" class="header-item"> |
| 12 | + <el-input v-model="item.key" placeholder="请输入参数名" class="header-key" clearable /> |
| 13 | + <span class="separator">:</span> |
| 14 | + <el-input |
| 15 | + v-model="item.value" |
| 16 | + placeholder="请输入参数值 (支持表达式 ${变量名})" |
| 17 | + class="header-value" |
| 18 | + clearable |
| 19 | + /> |
| 20 | + <el-button |
| 21 | + type="danger" |
| 22 | + :icon="Delete" |
| 23 | + circle |
| 24 | + size="small" |
| 25 | + @click="removeHeader(index)" |
| 26 | + /> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | + <el-button type="primary" :icon="Plus" class="add-btn" @click="addHeader"> |
| 30 | + 添加请求头 |
| 31 | + </el-button> |
| 32 | + </div> |
| 33 | + <template #footer> |
| 34 | + <span class="dialog-footer"> |
| 35 | + <el-button @click="handleClose">取消</el-button> |
| 36 | + <el-button type="primary" @click="handleSave">保存</el-button> |
| 37 | + </span> |
| 38 | + </template> |
| 39 | + </el-dialog> |
| 40 | +</template> |
| 41 | + |
| 42 | +<script lang="ts" setup> |
| 43 | +import { Delete, Plus } from '@element-plus/icons-vue' |
| 44 | +
|
| 45 | +defineOptions({ name: 'HttpHeaderEditor' }) |
| 46 | +
|
| 47 | +const props = defineProps({ |
| 48 | + modelValue: { |
| 49 | + type: Boolean, |
| 50 | + default: false |
| 51 | + }, |
| 52 | + headers: { |
| 53 | + type: String, |
| 54 | + default: '' |
| 55 | + } |
| 56 | +}) |
| 57 | +
|
| 58 | +const emit = defineEmits(['update:modelValue', 'save']) |
| 59 | +
|
| 60 | +interface HeaderItem { |
| 61 | + key: string |
| 62 | + value: string |
| 63 | +} |
| 64 | +
|
| 65 | +const dialogVisible = computed({ |
| 66 | + get: () => props.modelValue, |
| 67 | + set: (val) => emit('update:modelValue', val) |
| 68 | +}) |
| 69 | +
|
| 70 | +const headerList = ref<HeaderItem[]>([]) |
| 71 | +
|
| 72 | +// 解析请求头字符串为列表 |
| 73 | +const parseHeaders = (headersStr: string): HeaderItem[] => { |
| 74 | + if (!headersStr || !headersStr.trim()) { |
| 75 | + return [{ key: '', value: '' }] |
| 76 | + } |
| 77 | +
|
| 78 | + const lines = headersStr.split('\n').filter((line) => line.trim()) |
| 79 | + const parsed = lines.map((line) => { |
| 80 | + const colonIndex = line.indexOf(':') |
| 81 | + if (colonIndex > 0) { |
| 82 | + return { |
| 83 | + key: line.substring(0, colonIndex).trim(), |
| 84 | + value: line.substring(colonIndex + 1).trim() |
| 85 | + } |
| 86 | + } |
| 87 | + return { key: line.trim(), value: '' } |
| 88 | + }) |
| 89 | +
|
| 90 | + return parsed.length > 0 ? parsed : [{ key: '', value: '' }] |
| 91 | +} |
| 92 | +
|
| 93 | +// 将列表转换为请求头字符串 |
| 94 | +const stringifyHeaders = (headers: HeaderItem[]): string => { |
| 95 | + return headers |
| 96 | + .filter((item) => item.key.trim()) |
| 97 | + .map((item) => `${item.key}: ${item.value}`) |
| 98 | + .join('\n') |
| 99 | +} |
| 100 | +
|
| 101 | +// 添加请求头 |
| 102 | +const addHeader = () => { |
| 103 | + headerList.value.push({ key: '', value: '' }) |
| 104 | +} |
| 105 | +
|
| 106 | +// 移除请求头 |
| 107 | +const removeHeader = (index: number) => { |
| 108 | + if (headerList.value.length === 1) { |
| 109 | + // 至少保留一行 |
| 110 | + headerList.value = [{ key: '', value: '' }] |
| 111 | + } else { |
| 112 | + headerList.value.splice(index, 1) |
| 113 | + } |
| 114 | +} |
| 115 | +
|
| 116 | +// 保存 |
| 117 | +const handleSave = () => { |
| 118 | + const headersStr = stringifyHeaders(headerList.value) |
| 119 | + emit('save', headersStr) |
| 120 | + dialogVisible.value = false |
| 121 | +} |
| 122 | +
|
| 123 | +// 关闭 |
| 124 | +const handleClose = () => { |
| 125 | + dialogVisible.value = false |
| 126 | +} |
| 127 | +
|
| 128 | +// 监听对话框打开,初始化数据 |
| 129 | +watch( |
| 130 | + () => props.modelValue, |
| 131 | + (val) => { |
| 132 | + if (val) { |
| 133 | + headerList.value = parseHeaders(props.headers) |
| 134 | + } |
| 135 | + }, |
| 136 | + { immediate: true } |
| 137 | +) |
| 138 | +</script> |
| 139 | + |
| 140 | +<style lang="scss" scoped> |
| 141 | +.header-editor { |
| 142 | + .header-list { |
| 143 | + max-height: 400px; |
| 144 | + overflow-y: auto; |
| 145 | + margin-bottom: 16px; |
| 146 | + } |
| 147 | +
|
| 148 | + .header-item { |
| 149 | + display: flex; |
| 150 | + align-items: center; |
| 151 | + gap: 8px; |
| 152 | + margin-bottom: 12px; |
| 153 | +
|
| 154 | + .header-key { |
| 155 | + flex: 0 0 180px; |
| 156 | + } |
| 157 | +
|
| 158 | + .separator { |
| 159 | + color: #606266; |
| 160 | + font-weight: 500; |
| 161 | + } |
| 162 | +
|
| 163 | + .header-value { |
| 164 | + flex: 1; |
| 165 | + } |
| 166 | + } |
| 167 | +
|
| 168 | + .add-btn { |
| 169 | + width: 100%; |
| 170 | + } |
| 171 | +} |
| 172 | +
|
| 173 | +.dialog-footer { |
| 174 | + display: flex; |
| 175 | + justify-content: flex-end; |
| 176 | + gap: 10px; |
| 177 | +} |
| 178 | +</style> |
0 commit comments