351 lines
9.0 KiB
Vue
351 lines
9.0 KiB
Vue
<!--
|
||
ApiAppFormDialog — API 应用创建/编辑弹窗
|
||
对接接口:
|
||
- apiAppCreate:创建 API 应用(创建成功展示 AppKey/AppSecret)
|
||
- apiAppUpdate:编辑 API 应用
|
||
-->
|
||
<template>
|
||
<el-dialog
|
||
v-model="dialogVisible"
|
||
:title="isEdit ? '编辑 API 应用' : '创建 API 应用'"
|
||
width="600px"
|
||
:close-on-click-modal="false"
|
||
destroy-on-close
|
||
align-center
|
||
append-to-body
|
||
@closed="handleClosed"
|
||
>
|
||
<el-form
|
||
ref="formRef"
|
||
:model="formData"
|
||
:rules="formRules"
|
||
label-width="120px"
|
||
label-position="right"
|
||
>
|
||
<el-form-item label="应用名称" prop="app_name">
|
||
<el-input
|
||
v-model="formData.app_name"
|
||
placeholder="请输入应用名称"
|
||
:maxlength="200"
|
||
show-word-limit
|
||
clearable
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="速率限制">
|
||
<el-input-number
|
||
v-model="formData.rate_limit"
|
||
:min="1"
|
||
:max="100000"
|
||
:precision="0"
|
||
controls-position="right"
|
||
style="width: 100%"
|
||
/>
|
||
<span class="unit-text">次/秒</span>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="回调 URL">
|
||
<el-input
|
||
v-model="formData.callback_url"
|
||
placeholder="接收推送通知的 URL,如 https://example.com/webhook"
|
||
clearable
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="IP 白名单">
|
||
<div class="ip-whitelist">
|
||
<el-tag
|
||
v-for="(ip, idx) in formData.self_ip_whitelist"
|
||
:key="idx"
|
||
closable
|
||
class="ip-tag"
|
||
@close="handleRemoveFormIp(idx)"
|
||
>
|
||
{{ ip }}
|
||
</el-tag>
|
||
<el-input
|
||
v-if="formIpInputVisible"
|
||
ref="formIpInputRef"
|
||
v-model="formIpInputValue"
|
||
size="small"
|
||
style="width: 160px"
|
||
placeholder="IP 或 CIDR"
|
||
@blur="handleAddFormIp"
|
||
@keyup.enter="handleAddFormIp"
|
||
/>
|
||
<el-button v-else size="small" @click="showFormIpInput">+ 添加 IP</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
|
||
<el-form-item v-if="isEdit" label="启用状态">
|
||
<el-switch
|
||
v-model="formData.is_enabled"
|
||
:active-value="1"
|
||
:inactive-value="0"
|
||
active-text="启用"
|
||
inactive-text="停用"
|
||
/>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<!-- 创建成功后显示密钥信息 -->
|
||
<template v-if="createdSecret">
|
||
<el-divider />
|
||
<el-alert
|
||
type="warning"
|
||
:closable="false"
|
||
show-icon
|
||
title="请立即保存以下密钥信息,关闭后 AppSecret 将不再完整显示!"
|
||
/>
|
||
<div class="secret-display">
|
||
<div class="secret-row">
|
||
<span class="secret-label">AppKey:</span>
|
||
<code class="secret-value">{{ createdSecret.self_app_key }}</code>
|
||
<el-button link type="primary" size="small" @click="handleCopy(createdSecret.self_app_key, 'AppKey')">
|
||
复制
|
||
</el-button>
|
||
</div>
|
||
<div class="secret-row">
|
||
<span class="secret-label">AppSecret:</span>
|
||
<code class="secret-value secret-highlight">{{ createdSecret.self_app_secret }}</code>
|
||
<el-button link type="primary" size="small" @click="handleCopy(createdSecret.self_app_secret, 'AppSecret')">
|
||
复制
|
||
</el-button>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<template #footer>
|
||
<el-button @click="handleClosed">取消</el-button>
|
||
<el-button type="primary" :loading="submitting" @click="handleSubmit">
|
||
{{ isEdit ? '保存' : '创建' }}
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, nextTick, reactive, ref, watch } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import { apiAppCreate, apiAppUpdate } from '@/api/apiapp/apiapp'
|
||
|
||
defineOptions({ name: 'ApiAppFormDialog' })
|
||
|
||
const props = defineProps<{
|
||
visible: boolean
|
||
/** 编辑时的行数据注入 */
|
||
id?: number | string
|
||
app_name?: string
|
||
rate_limit?: number
|
||
callback_url?: string
|
||
self_ip_whitelist?: string[]
|
||
is_enabled?: number
|
||
}>()
|
||
|
||
const emit = defineEmits<{
|
||
(e: 'update:visible', v: boolean): void
|
||
(e: 'success'): void
|
||
}>()
|
||
|
||
const SUCCESS_CODES = [200, 20000, 1, 0]
|
||
|
||
const isEdit = computed(() => props.id !== undefined && props.id !== null && props.id !== '')
|
||
|
||
const dialogVisible = computed({
|
||
get: () => props.visible,
|
||
set: (v) => emit('update:visible', v)
|
||
})
|
||
|
||
const formRef = ref<any>()
|
||
const submitting = ref(false)
|
||
const createdSecret = ref<any>(null)
|
||
|
||
const formData = reactive({
|
||
app_name: '',
|
||
rate_limit: 1000,
|
||
callback_url: '',
|
||
self_ip_whitelist: [] as string[],
|
||
is_enabled: 1
|
||
})
|
||
|
||
const formRules = {
|
||
app_name: [
|
||
{ required: true, message: '请输入应用名称', trigger: 'blur' },
|
||
{ max: 200, message: '应用名称不能超过 200 个字符', trigger: 'blur' }
|
||
]
|
||
}
|
||
|
||
watch(() => props.visible, (v) => {
|
||
if (v) {
|
||
createdSecret.value = null
|
||
formData.app_name = props.app_name ?? ''
|
||
formData.rate_limit = props.rate_limit ?? 1000
|
||
formData.callback_url = props.callback_url ?? ''
|
||
formData.self_ip_whitelist = props.self_ip_whitelist ? [...props.self_ip_whitelist] : []
|
||
formData.is_enabled = props.is_enabled ?? 1
|
||
formRef.value?.clearValidate?.()
|
||
}
|
||
}, { immediate: true })
|
||
|
||
// 创建成功后用户关闭弹窗时通知父组件刷新列表
|
||
watch(() => props.visible, (visible, prev) => {
|
||
if (!visible && prev && createdSecret.value) {
|
||
emit('success')
|
||
}
|
||
})
|
||
|
||
function handleClosed() {
|
||
emit('update:visible', false)
|
||
}
|
||
|
||
// ===== IP 白名单(表单内) =====
|
||
const formIpInputVisible = ref(false)
|
||
const formIpInputValue = ref('')
|
||
const formIpInputRef = ref()
|
||
|
||
function showFormIpInput() {
|
||
formIpInputVisible.value = true
|
||
nextTick(() => {
|
||
formIpInputRef.value?.focus?.()
|
||
})
|
||
}
|
||
|
||
function handleAddFormIp() {
|
||
const val = formIpInputValue.value.trim()
|
||
if (!val) {
|
||
formIpInputVisible.value = false
|
||
return
|
||
}
|
||
if (formData.self_ip_whitelist.includes(val)) {
|
||
ElMessage.warning('该 IP 已存在')
|
||
formIpInputValue.value = ''
|
||
formIpInputVisible.value = false
|
||
return
|
||
}
|
||
formData.self_ip_whitelist.push(val)
|
||
formIpInputValue.value = ''
|
||
formIpInputVisible.value = false
|
||
}
|
||
|
||
function handleRemoveFormIp(idx: number) {
|
||
formData.self_ip_whitelist.splice(idx, 1)
|
||
}
|
||
|
||
async function handleSubmit() {
|
||
const valid = await formRef.value?.validate().catch(() => false)
|
||
if (!valid) return
|
||
|
||
submitting.value = true
|
||
try {
|
||
const payload = {
|
||
app_name: formData.app_name,
|
||
rate_limit: formData.rate_limit,
|
||
callback_url: formData.callback_url || undefined,
|
||
self_ip_whitelist: formData.self_ip_whitelist.length > 0 ? formData.self_ip_whitelist : undefined
|
||
}
|
||
if (isEdit.value) {
|
||
const res = await apiAppUpdate({
|
||
id: Number(props.id),
|
||
...payload,
|
||
is_enabled: formData.is_enabled
|
||
})
|
||
if (!SUCCESS_CODES.includes(res.code)) {
|
||
ElMessage.error(res.message || '更新失败')
|
||
return
|
||
}
|
||
ElMessage.success('更新成功')
|
||
emit('update:visible', false)
|
||
emit('success')
|
||
} else {
|
||
const res = await apiAppCreate(payload)
|
||
if (!SUCCESS_CODES.includes(res.code)) {
|
||
ElMessage.error(res.message || '创建失败')
|
||
return
|
||
}
|
||
createdSecret.value = res
|
||
ElMessage.success('创建成功!请保存 AppKey 和 AppSecret')
|
||
// 不关闭,等用户确认密钥后再关闭;关闭时通过 watch 触发刷新
|
||
}
|
||
} catch (e: any) {
|
||
ElMessage.error(e?.message || '操作失败')
|
||
} finally {
|
||
submitting.value = false
|
||
}
|
||
}
|
||
|
||
async function handleCopy(text: string, label: string) {
|
||
try {
|
||
await navigator.clipboard.writeText(text)
|
||
ElMessage.success(`${label} 已复制到剪贴板`)
|
||
} catch {
|
||
const textarea = document.createElement('textarea')
|
||
textarea.value = text
|
||
textarea.style.position = 'fixed'
|
||
textarea.style.opacity = '0'
|
||
document.body.appendChild(textarea)
|
||
textarea.select()
|
||
document.execCommand('copy')
|
||
document.body.removeChild(textarea)
|
||
ElMessage.success(`${label} 已复制到剪贴板`)
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.unit-text {
|
||
margin-left: 8px;
|
||
font-size: 13px;
|
||
color: #606266;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.ip-whitelist {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
align-items: center;
|
||
gap: 8px;
|
||
width: 100%;
|
||
}
|
||
|
||
.ip-tag {
|
||
margin: 0;
|
||
}
|
||
|
||
.secret-display {
|
||
background: #f5f7fa;
|
||
border: 1px solid #e4e7ed;
|
||
border-radius: 6px;
|
||
padding: 16px;
|
||
margin-top: 8px;
|
||
}
|
||
|
||
.secret-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
margin-bottom: 8px;
|
||
|
||
&:last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
}
|
||
|
||
.secret-label {
|
||
font-weight: 600;
|
||
color: #303133;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.secret-value {
|
||
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
|
||
font-size: 13px;
|
||
color: #409eff;
|
||
word-break: break-all;
|
||
}
|
||
|
||
.secret-highlight {
|
||
color: #e6a23c;
|
||
font-weight: 600;
|
||
}
|
||
</style>
|