feat: 新增API开放接口管理模块
- 新增 src/api/system/apiApp.ts: 封装 apiApp.list/detail/create/update/delete/regenerateSecret 全部接口 - 新增 ApiOpenInterface.vue: API应用列表+CRUD+详情+密钥管理完整功能 - 修改 index.vue: 添加「API开放接口」tab页签
This commit is contained in:
parent
1827ea30de
commit
f7f50a3c99
201
src/api/system/apiApp.ts
Normal file
201
src/api/system/apiApp.ts
Normal file
@ -0,0 +1,201 @@
|
||||
import request from '@/utils/request'
|
||||
|
||||
const v = '1.0'
|
||||
|
||||
// ============================================================
|
||||
// 类型定义(匹配实际 API 响应字段)
|
||||
// ============================================================
|
||||
|
||||
/** API 应用列表项 */
|
||||
export interface ApiAppItem {
|
||||
id: number
|
||||
app_name: string
|
||||
warehouse_id: number
|
||||
self_app_key: string
|
||||
self_app_secret: string // 列表返回脱敏后的 Secret(如 "ce4f07a1****")
|
||||
self_ip_whitelist: string[] | null
|
||||
callback_url: string | null
|
||||
interface_type_id: number
|
||||
rate_limit: number
|
||||
sign_method: string
|
||||
is_enabled: number // 1-启用, 0-停用
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
/** API 应用详情(含完整 Secret — 仅创建/重新生成时返回) */
|
||||
export interface ApiAppDetail extends ApiAppItem {
|
||||
// 创建/重置密钥时 self_app_secret 为完整 64 位 hex(顶层字段)
|
||||
}
|
||||
|
||||
/** create / detail / update / regenerateSecret 的通用扁平响应 */
|
||||
export interface ApiAppFlatResponse {
|
||||
flag: string
|
||||
code: number
|
||||
message: string
|
||||
id: number
|
||||
app_name: string
|
||||
warehouse_id: number
|
||||
self_app_key: string
|
||||
self_app_secret: string // 创建/重置时是完整密钥,列表/详情时脱敏
|
||||
self_ip_whitelist: string[] | null
|
||||
callback_url: string | null
|
||||
interface_type_id: number
|
||||
rate_limit: number
|
||||
sign_method: string
|
||||
is_enabled: number
|
||||
created_at: string
|
||||
updated_at: string
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
/** 列表查询参数 */
|
||||
export interface ApiAppListParams {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
app_name?: string
|
||||
is_enabled?: number
|
||||
}
|
||||
|
||||
/** 列表响应(list 和 pagination 在顶层,非 data 内嵌套) */
|
||||
export interface ApiAppListResponse {
|
||||
flag: string
|
||||
code: number
|
||||
message: string
|
||||
list: ApiAppItem[]
|
||||
pagination: {
|
||||
page: number
|
||||
pageSize: number
|
||||
total: number
|
||||
totalPages: number
|
||||
hasNext: boolean
|
||||
hasPrevious: boolean
|
||||
}
|
||||
}
|
||||
|
||||
/** 创建参数 */
|
||||
export interface ApiAppCreateParams {
|
||||
app_name: string
|
||||
interface_type_id?: number
|
||||
self_ip_whitelist?: string[]
|
||||
callback_url?: string
|
||||
rate_limit?: number
|
||||
sign_method?: string
|
||||
}
|
||||
|
||||
/** 更新参数 */
|
||||
export interface ApiAppUpdateParams {
|
||||
id: number
|
||||
app_name?: string
|
||||
self_ip_whitelist?: string[]
|
||||
callback_url?: string
|
||||
rate_limit?: number
|
||||
sign_method?: string
|
||||
is_enabled?: number
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// API 方法(响应中 list/pagination 在顶层)
|
||||
// ============================================================
|
||||
|
||||
/** 查询 API 应用列表(分页) */
|
||||
export function apiAppList(
|
||||
params: ApiAppListParams = {}
|
||||
): Promise<ApiAppListResponse> {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'apiApp.list',
|
||||
v,
|
||||
timestamp: Date.now()
|
||||
},
|
||||
data: params,
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/** 查询单个 API 应用详情 */
|
||||
export function apiAppDetail(
|
||||
id: number
|
||||
): Promise<ApiAppFlatResponse> {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'apiApp.detail',
|
||||
v,
|
||||
timestamp: Date.now()
|
||||
},
|
||||
data: { id },
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/** 创建 API 应用 */
|
||||
export function apiAppCreate(
|
||||
data: ApiAppCreateParams
|
||||
): Promise<ApiAppFlatResponse> {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'apiApp.create',
|
||||
v,
|
||||
timestamp: Date.now()
|
||||
},
|
||||
data,
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/** 更新 API 应用 */
|
||||
export function apiAppUpdate(
|
||||
data: ApiAppUpdateParams
|
||||
): Promise<ApiAppFlatResponse> {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'apiApp.update',
|
||||
v,
|
||||
timestamp: Date.now()
|
||||
},
|
||||
data,
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/** 停用 API 应用(软删除) */
|
||||
export function apiAppDelete(
|
||||
id: number
|
||||
): Promise<{ flag: string; code: number; message: string }> {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'apiApp.delete',
|
||||
v,
|
||||
timestamp: Date.now()
|
||||
},
|
||||
data: { id },
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
|
||||
/** 重新生成 AppSecret(响应为扁平结构,self_app_secret 为完整密钥) */
|
||||
export function apiAppRegenerateSecret(
|
||||
id: number
|
||||
): Promise<ApiAppFlatResponse> {
|
||||
return request({
|
||||
url: '/system',
|
||||
method: 'post',
|
||||
params: {
|
||||
method: 'apiApp.regenerateSecret',
|
||||
v,
|
||||
timestamp: Date.now()
|
||||
},
|
||||
data: { id },
|
||||
isSystemApi: true
|
||||
} as any)
|
||||
}
|
||||
770
src/views/system/setting/basicManagement/ApiOpenInterface.vue
Normal file
770
src/views/system/setting/basicManagement/ApiOpenInterface.vue
Normal file
@ -0,0 +1,770 @@
|
||||
<!--
|
||||
ApiOpenInterface — API 开放接口管理
|
||||
管理 API 应用的创建、编辑、停用、密钥管理等。
|
||||
-->
|
||||
<template>
|
||||
<div class="api-open-interface">
|
||||
<!-- ===== 工具栏 ===== -->
|
||||
<div class="toolbar">
|
||||
<div class="toolbar-left">
|
||||
<el-input
|
||||
v-model="searchForm.app_name"
|
||||
placeholder="搜索应用名称"
|
||||
clearable
|
||||
style="width: 220px"
|
||||
size="default"
|
||||
@keyup.enter="handleSearch"
|
||||
@clear="handleSearch"
|
||||
>
|
||||
<template #prefix>
|
||||
<el-icon><Search /></el-icon>
|
||||
</template>
|
||||
</el-input>
|
||||
<el-select
|
||||
v-model="searchForm.is_enabled"
|
||||
placeholder="启用状态"
|
||||
clearable
|
||||
style="width: 140px"
|
||||
size="default"
|
||||
@change="handleSearch"
|
||||
>
|
||||
<el-option label="全部" :value="undefined" />
|
||||
<el-option label="已启用" :value="1" />
|
||||
<el-option label="已停用" :value="0" />
|
||||
</el-select>
|
||||
<el-button type="primary" size="default" @click="handleSearch">
|
||||
<el-icon><Search /></el-icon>
|
||||
查询
|
||||
</el-button>
|
||||
</div>
|
||||
<div class="toolbar-right">
|
||||
<el-button size="default" @click="loadData">
|
||||
<el-icon><Refresh /></el-icon>
|
||||
刷新
|
||||
</el-button>
|
||||
<el-button type="primary" size="default" @click="openCreateDialog">
|
||||
<el-icon><Plus /></el-icon>
|
||||
创建应用
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- ===== 数据表格 ===== -->
|
||||
<el-table
|
||||
v-loading="loading"
|
||||
:data="listData"
|
||||
border
|
||||
stripe
|
||||
size="default"
|
||||
style="width: 100%"
|
||||
empty-text="暂无 API 应用数据"
|
||||
>
|
||||
<el-table-column prop="id" label="ID" width="70" align="center" />
|
||||
<el-table-column prop="app_name" label="应用名称" min-width="160" show-overflow-tooltip />
|
||||
<el-table-column label="AppKey" min-width="200" show-overflow-tooltip>
|
||||
<template #default="{ row }">
|
||||
<span class="mono-text">{{ row.self_app_key }}</span>
|
||||
<el-button
|
||||
link
|
||||
type="primary"
|
||||
size="small"
|
||||
style="margin-left: 4px"
|
||||
@click="handleCopy(row.self_app_key, 'AppKey')"
|
||||
>
|
||||
复制
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="AppSecret" min-width="160" show-overflow-tooltip>
|
||||
<template #default="{ row }">
|
||||
<span class="mono-text">{{ row.self_app_secret || '****' }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="sign_method" label="签名方法" width="100" align="center">
|
||||
<template #default="{ row }">
|
||||
<el-tag size="small" type="info">{{ row.sign_method?.toUpperCase() || 'MD5' }}</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="rate_limit" label="速率限制" width="110" align="center">
|
||||
<template #default="{ row }">
|
||||
{{ row.rate_limit ?? 1000 }} 次/秒
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="is_enabled" label="状态" width="90" align="center">
|
||||
<template #default="{ row }">
|
||||
<el-tag size="small" :type="row.is_enabled === 1 ? 'success' : 'danger'">
|
||||
{{ row.is_enabled === 1 ? '启用' : '停用' }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="创建时间" width="170" align="center">
|
||||
<template #default="{ row }">
|
||||
{{ formatTime(row.created_at) }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="操作" width="260" align="center" fixed="right">
|
||||
<template #default="{ row }">
|
||||
<el-button link type="primary" size="small" @click="openDetailDialog(row)">
|
||||
详情
|
||||
</el-button>
|
||||
<el-button link type="primary" size="small" @click="openEditDialog(row)">
|
||||
编辑
|
||||
</el-button>
|
||||
<el-button link type="warning" size="small" @click="handleRegenerateSecret(row)">
|
||||
重置密钥
|
||||
</el-button>
|
||||
<el-button
|
||||
v-if="row.is_enabled === 1"
|
||||
link
|
||||
type="danger"
|
||||
size="small"
|
||||
@click="handleDelete(row)"
|
||||
>
|
||||
停用
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<!-- ===== 分页 ===== -->
|
||||
<div class="pagination-wrap">
|
||||
<el-pagination
|
||||
v-model:current-page="pagination.page"
|
||||
v-model:page-size="pagination.pageSize"
|
||||
:page-sizes="[10, 20, 50, 100]"
|
||||
:total="pagination.total"
|
||||
layout="total, sizes, prev, pager, next, jumper"
|
||||
@size-change="handleSizeChange"
|
||||
@current-change="handlePageChange"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- ===== 创建/编辑对话框 ===== -->
|
||||
<el-dialog
|
||||
v-model="dialogVisible"
|
||||
:title="dialogMode === 'create' ? '创建 API 应用' : '编辑 API 应用'"
|
||||
width="600px"
|
||||
:close-on-click-modal="false"
|
||||
destroy-on-close
|
||||
align-center
|
||||
>
|
||||
<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-select v-model="formData.sign_method" placeholder="请选择签名方法" style="width: 100%">
|
||||
<el-option label="MD5" value="md5" />
|
||||
<el-option label="HMAC" value="hmac" />
|
||||
<el-option label="SHA256" value="sha256" />
|
||||
</el-select>
|
||||
</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="dialogMode === 'edit'" 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="dialogVisible = false">取消</el-button>
|
||||
<el-button type="primary" :loading="submitting" @click="handleSubmit">
|
||||
{{ dialogMode === 'create' ? '创建' : '保存' }}
|
||||
</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
|
||||
<!-- ===== 详情对话框 ===== -->
|
||||
<el-dialog
|
||||
v-model="detailVisible"
|
||||
title="API 应用详情"
|
||||
width="600px"
|
||||
:close-on-click-modal="false"
|
||||
align-center
|
||||
>
|
||||
<el-descriptions v-if="detailData" :column="2" border size="default">
|
||||
<el-descriptions-item label="应用 ID">{{ detailData.id }}</el-descriptions-item>
|
||||
<el-descriptions-item label="应用名称">{{ detailData.app_name }}</el-descriptions-item>
|
||||
<el-descriptions-item label="AppKey" :span="2">
|
||||
<code>{{ detailData.self_app_key }}</code>
|
||||
<el-button link type="primary" size="small" style="margin-left: 8px" @click="handleCopy(detailData.self_app_key, 'AppKey')">
|
||||
复制
|
||||
</el-button>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="AppSecret" :span="2">
|
||||
<code>{{ detailData.self_app_secret || '****' }}</code>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="签名方法">
|
||||
<el-tag size="small" type="info">{{ detailData.sign_method?.toUpperCase() || 'MD5' }}</el-tag>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="速率限制">{{ detailData.rate_limit ?? 1000 }} 次/秒</el-descriptions-item>
|
||||
<el-descriptions-item label="启用状态">
|
||||
<el-tag size="small" :type="detailData.is_enabled === 1 ? 'success' : 'danger'">
|
||||
{{ detailData.is_enabled === 1 ? '已启用' : '已停用' }}
|
||||
</el-tag>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="接口类型">{{ detailData.interface_type_id }}</el-descriptions-item>
|
||||
<el-descriptions-item label="仓库 ID">{{ detailData.warehouse_id }}</el-descriptions-item>
|
||||
<el-descriptions-item label="回调 URL" :span="2">
|
||||
{{ detailData.callback_url || '—' }}
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="IP 白名单" :span="2">
|
||||
<template v-if="detailData.self_ip_whitelist?.length">
|
||||
<el-tag v-for="(ip, i) in detailData.self_ip_whitelist" :key="i" size="small" style="margin: 2px 4px 2px 0">
|
||||
{{ ip }}
|
||||
</el-tag>
|
||||
</template>
|
||||
<span v-else>—</span>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="创建时间">{{ formatTime(detailData.created_at) }}</el-descriptions-item>
|
||||
<el-descriptions-item label="更新时间">{{ formatTime(detailData.updated_at) }}</el-descriptions-item>
|
||||
</el-descriptions>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, nextTick, watch } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { Search, Refresh, Plus } from '@element-plus/icons-vue'
|
||||
import {
|
||||
apiAppList,
|
||||
apiAppCreate,
|
||||
apiAppUpdate,
|
||||
apiAppDelete,
|
||||
apiAppDetail,
|
||||
apiAppRegenerateSecret,
|
||||
type ApiAppItem,
|
||||
type ApiAppFlatResponse
|
||||
} from '@/api/system/apiApp'
|
||||
|
||||
defineOptions({ name: 'ApiOpenInterface' })
|
||||
|
||||
// ============================================================
|
||||
// 表格数据
|
||||
// ============================================================
|
||||
const loading = ref(false)
|
||||
const listData = ref<ApiAppItem[]>([])
|
||||
const pagination = reactive({
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
total: 0
|
||||
})
|
||||
|
||||
const searchForm = reactive({
|
||||
app_name: '',
|
||||
is_enabled: undefined as number | undefined
|
||||
})
|
||||
|
||||
// ============================================================
|
||||
// 数据加载
|
||||
// ============================================================
|
||||
const SUCCESS_CODES = [200, 20000, 1, 0]
|
||||
|
||||
async function loadData() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await apiAppList({
|
||||
page: pagination.page,
|
||||
pageSize: pagination.pageSize,
|
||||
app_name: searchForm.app_name || undefined,
|
||||
is_enabled: searchForm.is_enabled
|
||||
})
|
||||
if (!SUCCESS_CODES.includes(res.code)) {
|
||||
ElMessage.error(res.message || '查询失败')
|
||||
listData.value = []
|
||||
pagination.total = 0
|
||||
return
|
||||
}
|
||||
listData.value = res.list ?? []
|
||||
pagination.total = res.pagination?.total ?? 0
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.message || '查询异常')
|
||||
listData.value = []
|
||||
pagination.total = 0
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function handleSearch() {
|
||||
pagination.page = 1
|
||||
loadData()
|
||||
}
|
||||
|
||||
function handlePageChange(page: number) {
|
||||
pagination.page = page
|
||||
loadData()
|
||||
}
|
||||
|
||||
function handleSizeChange(size: number) {
|
||||
pagination.pageSize = size
|
||||
pagination.page = 1
|
||||
loadData()
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 创建 / 编辑对话框
|
||||
// ============================================================
|
||||
const dialogVisible = ref(false)
|
||||
const dialogMode = ref<'create' | 'edit'>('create')
|
||||
const submitting = ref(false)
|
||||
const editingId = ref<number | null>(null)
|
||||
const formRef = ref()
|
||||
const createdSecret = ref<ApiAppFlatResponse | null>(null)
|
||||
|
||||
interface FormData {
|
||||
app_name: string
|
||||
sign_method: string
|
||||
rate_limit: number
|
||||
callback_url: string
|
||||
self_ip_whitelist: string[]
|
||||
is_enabled: number
|
||||
}
|
||||
|
||||
const formData = reactive<FormData>({
|
||||
app_name: '',
|
||||
sign_method: 'md5',
|
||||
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' }
|
||||
]
|
||||
}
|
||||
|
||||
function resetForm() {
|
||||
formData.app_name = ''
|
||||
formData.sign_method = 'md5'
|
||||
formData.rate_limit = 1000
|
||||
formData.callback_url = ''
|
||||
formData.self_ip_whitelist = []
|
||||
formData.is_enabled = 1
|
||||
createdSecret.value = null
|
||||
editingId.value = null
|
||||
}
|
||||
|
||||
function openCreateDialog() {
|
||||
dialogMode.value = 'create'
|
||||
resetForm()
|
||||
dialogVisible.value = true
|
||||
}
|
||||
|
||||
function openEditDialog(row: ApiAppItem) {
|
||||
dialogMode.value = 'edit'
|
||||
resetForm()
|
||||
editingId.value = row.id
|
||||
formData.app_name = row.app_name
|
||||
formData.sign_method = row.sign_method || 'md5'
|
||||
formData.rate_limit = row.rate_limit ?? 1000
|
||||
formData.callback_url = row.callback_url || ''
|
||||
formData.self_ip_whitelist = row.self_ip_whitelist ? [...row.self_ip_whitelist] : []
|
||||
formData.is_enabled = row.is_enabled
|
||||
dialogVisible.value = true
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
const valid = await formRef.value?.validate().catch(() => false)
|
||||
if (!valid) return
|
||||
|
||||
submitting.value = true
|
||||
try {
|
||||
if (dialogMode.value === 'create') {
|
||||
const res = await apiAppCreate({
|
||||
app_name: formData.app_name,
|
||||
sign_method: formData.sign_method,
|
||||
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 (!SUCCESS_CODES.includes(res.code)) {
|
||||
ElMessage.error(res.message || '创建失败')
|
||||
return
|
||||
}
|
||||
createdSecret.value = res
|
||||
ElMessage.success('创建成功!请保存 AppKey 和 AppSecret')
|
||||
// 不关闭对话框,等用户确认密钥后再关闭;关闭时通过 watch 自动刷新列表
|
||||
} else {
|
||||
const res = await apiAppUpdate({
|
||||
id: editingId.value!,
|
||||
app_name: formData.app_name,
|
||||
sign_method: formData.sign_method,
|
||||
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,
|
||||
is_enabled: formData.is_enabled
|
||||
})
|
||||
if (!SUCCESS_CODES.includes(res.code)) {
|
||||
ElMessage.error(res.message || '更新失败')
|
||||
return
|
||||
}
|
||||
ElMessage.success('更新成功')
|
||||
dialogVisible.value = false
|
||||
loadData()
|
||||
}
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.message || '操作失败')
|
||||
} finally {
|
||||
submitting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 创建成功后用户关闭对话框时,自动刷新列表
|
||||
watch(dialogVisible, (visible, prevVisible) => {
|
||||
if (!visible && prevVisible && dialogMode.value === 'create' && createdSecret.value) {
|
||||
loadData()
|
||||
}
|
||||
})
|
||||
|
||||
// ============================================================
|
||||
// 删除(停用)
|
||||
// ============================================================
|
||||
async function handleDelete(row: ApiAppItem) {
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
`确定要停用应用「${row.app_name}」吗?停用后该应用的 API 密钥将立即失效。`,
|
||||
'停用确认',
|
||||
{
|
||||
confirmButtonText: '确定停用',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}
|
||||
)
|
||||
const res = await apiAppDelete(row.id)
|
||||
if (!SUCCESS_CODES.includes(res.code)) {
|
||||
ElMessage.error(res.message || '停用失败')
|
||||
return
|
||||
}
|
||||
ElMessage.success('已停用')
|
||||
loadData()
|
||||
} catch {
|
||||
// 用户取消
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 重新生成密钥
|
||||
// ============================================================
|
||||
async function handleRegenerateSecret(row: ApiAppItem) {
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
`确定要重新生成「${row.app_name}」的 AppSecret 吗?旧密钥将立即失效,请确保已更新所有使用该密钥的系统。`,
|
||||
'重置密钥确认',
|
||||
{
|
||||
confirmButtonText: '确定重置',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}
|
||||
)
|
||||
const res = await apiAppRegenerateSecret(row.id)
|
||||
if (!SUCCESS_CODES.includes(res.code)) {
|
||||
ElMessage.error(res.message || '重置失败')
|
||||
return
|
||||
}
|
||||
const newSecret = res.self_app_secret || ''
|
||||
ElMessageBox.alert(
|
||||
`新 AppSecret:${newSecret}`,
|
||||
'密钥已重置 — 请妥善保存',
|
||||
{
|
||||
confirmButtonText: '我已保存',
|
||||
type: 'warning',
|
||||
dangerouslyUseHTMLString: false
|
||||
}
|
||||
)
|
||||
loadData()
|
||||
} catch {
|
||||
// 用户取消
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 详情对话框
|
||||
// ============================================================
|
||||
const detailVisible = ref(false)
|
||||
const detailData = ref<ApiAppFlatResponse | null>(null)
|
||||
|
||||
async function openDetailDialog(row: ApiAppItem) {
|
||||
detailVisible.value = true
|
||||
detailData.value = null
|
||||
try {
|
||||
const res = await apiAppDetail(row.id)
|
||||
if (SUCCESS_CODES.includes(res.code)) {
|
||||
detailData.value = res
|
||||
} else {
|
||||
// 兜底用列表行数据
|
||||
detailData.value = { ...res, ...row } as any
|
||||
}
|
||||
} catch {
|
||||
detailData.value = row as any
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 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)
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 工具函数
|
||||
// ============================================================
|
||||
function formatTime(value: any): string {
|
||||
if (!value) return '—'
|
||||
try {
|
||||
const d = new Date(value)
|
||||
if (isNaN(d.getTime())) return String(value)
|
||||
const pad = (n: number) => String(n).padStart(2, '0')
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
|
||||
} catch {
|
||||
return String(value)
|
||||
}
|
||||
}
|
||||
|
||||
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} 已复制到剪贴板`)
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 初始化
|
||||
// ============================================================
|
||||
loadData()
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.api-open-interface {
|
||||
padding: 4px 0;
|
||||
}
|
||||
|
||||
// 工具栏
|
||||
.toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
margin-bottom: 16px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.toolbar-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.toolbar-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
// 表格等宽字体
|
||||
.mono-text {
|
||||
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
// 分页
|
||||
.pagination-wrap {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
// IP 白名单
|
||||
.ip-whitelist {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.ip-tag {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
// 单位文本
|
||||
.unit-text {
|
||||
margin-left: 8px;
|
||||
font-size: 13px;
|
||||
color: #606266;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
// 密钥展示
|
||||
.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>
|
||||
|
||||
@ -215,6 +215,10 @@
|
||||
</el-form>
|
||||
</el-tab-pane>
|
||||
|
||||
<el-tab-pane label="API开放接口" name="apiOpen">
|
||||
<ApiOpenInterface />
|
||||
</el-tab-pane>
|
||||
|
||||
<!-- <el-tab-pane label="JSON 编辑" name="json">
|
||||
<p class="hint">
|
||||
直接编辑 <code>subject_config</code> 完整 JSON。保存前会校验格式;与表单页签共用同一份数据。
|
||||
@ -258,6 +262,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, reactive, ref, watch } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import ApiOpenInterface from './ApiOpenInterface.vue'
|
||||
import {
|
||||
settingWarehouseGetSettings,
|
||||
settingWarehouseGetFieldMeta,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user