From 095ea0a45c5183896e6b3a00fc1dd777c38ce079 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 30 Jun 2025 08:59:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=20@umijs/openapi=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E9=80=9A=E8=BF=87swagger=E7=9A=84api?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E6=8E=A5=E5=8F=A3=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E7=9A=84api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + openapi.config.js | 7 ++++++ package.json | 5 ++++- src/api/index.ts | 8 +++++++ src/api/mainController.ts | 11 +++++++++ src/api/typings.d.ts | 7 ++++++ src/request.ts | 47 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 openapi.config.js create mode 100644 src/api/index.ts create mode 100644 src/api/mainController.ts create mode 100644 src/api/typings.d.ts create mode 100644 src/request.ts diff --git a/.gitignore b/.gitignore index 6fd7f6b..3bfe6da 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ coverage *.tsbuildinfo /yarn.lock +/package-lock.json diff --git a/openapi.config.js b/openapi.config.js new file mode 100644 index 0000000..9bd461b --- /dev/null +++ b/openapi.config.js @@ -0,0 +1,7 @@ +import { generateService } from '@umijs/openapi' + +generateService({ + requestLibPath: "import request from '@/request'", + schemaPath: 'http://localhost:8123/api/v2/api-docs', + serversPath: './src', +}) diff --git a/package.json b/package.json index 26cccb9..ce3f903 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "build-only": "vite build", "type-check": "vue-tsc --build", "lint": "eslint . --fix", - "format": "prettier --write src/" + "format": "prettier --write src/", + "openapi": "node openapi.config.js" }, "dependencies": { "ant-design-vue": "4.x", @@ -22,6 +23,7 @@ "devDependencies": { "@tsconfig/node22": "^22.0.2", "@types/node": "^22.15.32", + "@umijs/openapi": "^1.13.15", "@vitejs/plugin-vue": "^6.0.0", "@vue/eslint-config-prettier": "^10.2.0", "@vue/eslint-config-typescript": "^14.5.1", @@ -31,6 +33,7 @@ "jiti": "^2.4.2", "npm-run-all2": "^8.0.4", "prettier": "3.5.3", + "tslib": "^2.8.1", "typescript": "~5.8.0", "vite": "^7.0.0", "vite-plugin-vue-devtools": "^7.7.7", diff --git a/src/api/index.ts b/src/api/index.ts new file mode 100644 index 0000000..407903d --- /dev/null +++ b/src/api/index.ts @@ -0,0 +1,8 @@ +// @ts-ignore +/* eslint-disable */ +// API 更新时间: +// API 唯一标识: +import * as mainController from './mainController' +export default { + mainController, +} diff --git a/src/api/mainController.ts b/src/api/mainController.ts new file mode 100644 index 0000000..d8f638e --- /dev/null +++ b/src/api/mainController.ts @@ -0,0 +1,11 @@ +// @ts-ignore +/* eslint-disable */ +import request from '@/request' + +/** health GET /api/health */ +export async function healthUsingGet(options?: { [key: string]: any }) { + return request('/api/health', { + method: 'GET', + ...(options || {}), + }) +} diff --git a/src/api/typings.d.ts b/src/api/typings.d.ts new file mode 100644 index 0000000..839b127 --- /dev/null +++ b/src/api/typings.d.ts @@ -0,0 +1,7 @@ +declare namespace API { + type RObject_ = { + code?: number + data?: Record + msg?: string + } +} diff --git a/src/request.ts b/src/request.ts new file mode 100644 index 0000000..7dd6340 --- /dev/null +++ b/src/request.ts @@ -0,0 +1,47 @@ +import axios from 'axios' +import { message } from 'ant-design-vue' + +// 创建 Axios 实例 +const myAxios = axios.create({ + baseURL: 'http://localhost:8123', + timeout: 60000, + withCredentials: true, +}) + +// 全局请求拦截器 +myAxios.interceptors.request.use( + function (config) { + // Do something before request is sent + return config + }, + function (error) { + // Do something with request error + return Promise.reject(error) + }, +) + +// 全局响应拦截器 +myAxios.interceptors.response.use( + function (response) { + const { data } = response + // 未登录 + if (data.code === 40100) { + // 不是获取用户信息的请求,并且用户目前不是已经在用户登录页面,则跳转到登录页面 + if ( + !response.request.responseURL.includes('user/get/login') && + !window.location.pathname.includes('/user/login') + ) { + message.warning('请先登录') + window.location.href = `/user/login?redirect=${window.location.href}` + } + } + return response + }, + function (error) { + // Any status codes that falls outside the range of 2xx cause this function to trigger + // Do something with response error + return Promise.reject(error) + }, +) + +export default myAxios