diff --git a/package.json b/package.json index 36919ea..4ddd682 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "preview": "rsbuild preview" }, "dependencies": { + "@cap.js/widget": "^0.1.41", "@hookform/resolvers": "^5.2.2", "@tabler/icons-react": "^3.36.1", "@tanstack/react-query": "^5.90.17", diff --git a/src/api/auth/index.ts b/src/api/auth/index.ts index 425cd7d..eaecc5d 100644 --- a/src/api/auth/index.ts +++ b/src/api/auth/index.ts @@ -8,11 +8,21 @@ export const UpdatePasswordApiBody = z.object({ newPassword: z.string().min(8, "密码长度至少为8位"), }); export class AuthAPI { - static async login(data: { email: string; password: string }) { + static async login(data: { email: string; password: string; captchaToken: string }) { const res = await Axios.post<{ token: string; user: User }>("/auth/login", data); return res.data; } + static async register(data: { + name: string; + password: string; + email: string; + captchaToken: string; + }) { + const res = await Axios.post<{ token: string; user: User }>("/auth/register", data); + return res.data; + } + static async getCurrentUser() { const res = await Axios.get("/auth/me"); return res.data; diff --git a/src/api/developer/application.ts b/src/api/developer/application.ts index 26acb1d..d92f5dc 100644 --- a/src/api/developer/application.ts +++ b/src/api/developer/application.ts @@ -1,8 +1,39 @@ import Axios from "@/api"; -import { Application } from "@/types/application"; +import { InferZodType } from "@/types/common"; import { List } from "@/types/Request"; +import { Application } from "@/types/application"; +import { z } from "zod"; + +export const EditApplicationApiBody = z.object({ + name: z.string().min(1), + description: z.string().nullish(), +}); + +export class ApplicationApi { + static async getApplicationList(params?: { pageSize?: number; current?: number; search?: string; orgSearch?: string }) { + const res = await Axios.get>("/developer/application", { + params, + }); + return res.data; + } + + static async getApplicationDetail(id: string) { + const res = await Axios.get(`/developer/application/${id}`); + return res.data; + } + + static async createApplication(application: InferZodType) { + const res = await Axios.post("/developer/application", application); + return res.data; + } + + static async updateApplication(id: string, application: InferZodType) { + const res = await Axios.post(`/developer/application/${id}`, application); + return res.data; + } -export async function getApplicationList() { - const res = await Axios.get>("/developer/application"); - return res.data; + static async deleteApplication(id: string) { + const res = await Axios.delete<{ success: boolean }>(`/developer/application/${id}`); + return res.data; + } } diff --git a/src/components/Application/ApplicationEditor.tsx b/src/components/Application/ApplicationEditor.tsx new file mode 100644 index 0000000..56f2cee --- /dev/null +++ b/src/components/Application/ApplicationEditor.tsx @@ -0,0 +1,95 @@ +import { ApplicationApi, EditApplicationApiBody } from "@/api/developer/application"; +import { InferZodType } from "@/types/common"; +import { Application } from "@/types/application"; +import { useZodValidateData } from "@/utils/form"; +import { pickBy } from "es-toolkit"; +import { App, Button, Flex, Form, Input, Modal } from "antd"; + +const { TextArea } = Input; + +export default function ApplicationEditor({ + opened, + onClose, + editingApplication, +}: { + opened: boolean; + onClose: () => void; + editingApplication: Application | null; +}) { + return ( + + + + ); +} + +function EditorContent({ + editingApplication, + onClose, +}: { + editingApplication: Application | null; + onClose: () => void; +}) { + const { message, modal } = App.useApp(); + const cleanedApplication = editingApplication ? pickBy(editingApplication, (v) => v !== "" && v != null) : {}; + const [form] = Form.useForm(); + + const initialValues = { + name: cleanedApplication.name, + description: cleanedApplication.description, + }; + + const onSubmit = async (value: InferZodType) => { + try { + if (editingApplication?.id) { + await ApplicationApi.updateApplication(editingApplication.id, value); + message.success("更新应用成功"); + return onClose(); + } + await ApplicationApi.createApplication(value); + message.success("创建应用成功"); + return onClose(); + } catch (error) { + message.error(`有错误发生: ${JSON.stringify(error)}`); + } + }; + + const handleFinish = (value: typeof initialValues) => { + const processedValues = useZodValidateData(value, EditApplicationApiBody); + if (processedValues.errors.length > 0) { + return modal.warning({ + title: "接口数据校验失败☹️", + content: processedValues.prettyErrors, + }); + } + if (processedValues.values) { + return onSubmit(processedValues.values); + } + return; + }; + + return ( +
+ + + + + +