import React, { useState, useEffect } from 'react' import { View, Text, Input, Button, actionSheet } from '@tarojs/components' import Taro, { useRouter } from '@tarojs/taro' import { usePullDownRefresh } from '@tarojs/taro' import { listApiKey, createApiKey, deleteApiKey } from '@/api/developer/developer' import type { ApiKey, ApiKeyParam } from '@/types/developer' import './api-keys.scss' const ApiKeysPage: React.FC = () => { const router = useRouter() const projectId = Number(router.params.id) const [loading, setLoading] = useState(false) const [list, setList] = useState([]) const [showCreate, setShowCreate] = useState(false) const [createForm, setCreateForm] = useState({ name: '', remark: '' }) const [creating, setCreating] = useState(false) const loadData = async () => { setLoading(true) try { const params: ApiKeyParam = { websiteId: projectId } const data = await listApiKey(params) setList(data || []) } catch (err) { console.error('加载失败', err) } finally { setLoading(false) } } useEffect(() => { loadData() }, [projectId]) usePullDownRefresh(() => { loadData() }) const handleCreate = async () => { if (!createForm.name.trim()) { Taro.showToast({ title: '请输入名称', icon: 'none' }) return } setCreating(true) try { await createApiKey({ name: createForm.name, remark: createForm.remark, websiteId: projectId } as Partial) Taro.showToast({ title: '创建成功', icon: 'success' }) setShowCreate(false) setCreateForm({ name: '', remark: '' }) loadData() } catch { Taro.showToast({ title: '创建失败', icon: 'none' }) } finally { setCreating(false) } } const handleDelete = (item: ApiKey) => { actionSheet({ alertText: `确定删除 "${item.name}" 吗?`, actions: [{ name: '删除', color: '#ff4d4f', type: 'warn' as const }], confirmText: '取消', }).then(res => { if (res.confirm) { deleteApiKey(item.id!).then(() => { Taro.showToast({ title: '已删除', icon: 'success' }) loadData() }) } }).catch(() => {}) } const handleCopy = (text: string) => { Taro.setClipboardData({ data: text, success: () => Taro.showToast({ title: '已复制', icon: 'success' }) }) } return ( 🔑 API Key 管理 {list.length === 0 && !loading ? ( 暂无 API Key ) : ( list.map((item) => ( {item.name} {item.status ? '正常' : '禁用'} AppId: handleCopy(item.appId || '')}> {item.appId || '-'} AppSecret: handleCopy(item.appSecret || '')}> •••••••••••••••• {item.appSecret && handleCopy(item.appSecret || '')}>复制密钥} handleDelete(item)}>删除 )) )} {showCreate && ( setShowCreate(false)} /> 创建 API Key 名称 * setCreateForm(prev => ({ ...prev, name: e.detail.value }))} /> 备注 setCreateForm(prev => ({ ...prev, remark: e.detail.value }))} /> )} ) } export default ApiKeysPage