引入 @umijs/openapi 组件,通过swagger的api自动生成接口对应的api

This commit is contained in:
huangge1199 2025-06-30 08:59:54 +08:00
parent 7ee3a1d240
commit 095ea0a45c
7 changed files with 85 additions and 1 deletions

1
.gitignore vendored
View File

@ -29,3 +29,4 @@ coverage
*.tsbuildinfo *.tsbuildinfo
/yarn.lock /yarn.lock
/package-lock.json

7
openapi.config.js Normal file
View File

@ -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',
})

View File

@ -10,7 +10,8 @@
"build-only": "vite build", "build-only": "vite build",
"type-check": "vue-tsc --build", "type-check": "vue-tsc --build",
"lint": "eslint . --fix", "lint": "eslint . --fix",
"format": "prettier --write src/" "format": "prettier --write src/",
"openapi": "node openapi.config.js"
}, },
"dependencies": { "dependencies": {
"ant-design-vue": "4.x", "ant-design-vue": "4.x",
@ -22,6 +23,7 @@
"devDependencies": { "devDependencies": {
"@tsconfig/node22": "^22.0.2", "@tsconfig/node22": "^22.0.2",
"@types/node": "^22.15.32", "@types/node": "^22.15.32",
"@umijs/openapi": "^1.13.15",
"@vitejs/plugin-vue": "^6.0.0", "@vitejs/plugin-vue": "^6.0.0",
"@vue/eslint-config-prettier": "^10.2.0", "@vue/eslint-config-prettier": "^10.2.0",
"@vue/eslint-config-typescript": "^14.5.1", "@vue/eslint-config-typescript": "^14.5.1",
@ -31,6 +33,7 @@
"jiti": "^2.4.2", "jiti": "^2.4.2",
"npm-run-all2": "^8.0.4", "npm-run-all2": "^8.0.4",
"prettier": "3.5.3", "prettier": "3.5.3",
"tslib": "^2.8.1",
"typescript": "~5.8.0", "typescript": "~5.8.0",
"vite": "^7.0.0", "vite": "^7.0.0",
"vite-plugin-vue-devtools": "^7.7.7", "vite-plugin-vue-devtools": "^7.7.7",

8
src/api/index.ts Normal file
View File

@ -0,0 +1,8 @@
// @ts-ignore
/* eslint-disable */
// API 更新时间:
// API 唯一标识:
import * as mainController from './mainController'
export default {
mainController,
}

11
src/api/mainController.ts Normal file
View File

@ -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.RObject_>('/api/health', {
method: 'GET',
...(options || {}),
})
}

7
src/api/typings.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
declare namespace API {
type RObject_ = {
code?: number
data?: Record<string, any>
msg?: string
}
}

47
src/request.ts Normal file
View File

@ -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