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