From 9840e6d8230659d4d86cd6dba096c34d646a300c Mon Sep 17 00:00:00 2001 From: LiuYuanchi Date: Sun, 27 Apr 2025 12:29:47 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E4=BF=AE=E6=94=B9=E4=BA=86=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E8=AF=B7=E6=B1=82=E7=9B=B8=E5=85=B3=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.prod | 2 +- .env.test | 11 +++++----- src/typings/components.d.ts | 35 +++++++++++++++++++++++++++++++ src/utils/api.ts | 28 ++++++++++++++++++++++--- src/views/department/index.vue | 2 +- src/views/employee/list/index.vue | 10 +++------ src/views/facility/index.vue | 2 +- src/views/orders/create/index.vue | 9 ++------ src/views/orders/list/index.vue | 9 +++----- src/views/plan/index.vue | 2 +- 10 files changed, 78 insertions(+), 32 deletions(-) diff --git a/.env.prod b/.env.prod index eb6154b..6fd6f89 100644 --- a/.env.prod +++ b/.env.prod @@ -1,5 +1,5 @@ # backend service base url, prod environment -VITE_SERVICE_BASE_URL=http://154.219.110.17:8080 +VITE_SERVICE_BASE_URL=http://154.219.110.17:8080/api # other backend service base url, prod environment # VITE_OTHER_SERVICE_BASE_URL= `{ diff --git a/.env.test b/.env.test index 979c9dc..b121306 100644 --- a/.env.test +++ b/.env.test @@ -1,7 +1,8 @@ # backend service base url, test environment -VITE_SERVICE_BASE_URL=http://localhost:8080/api/ +VITE_SERVICE_BASE_URL=http://localhost:8080/api +#VITE_SERVICE_BASE_URL=http://154.219.110.17:8080/api -# other backend service base url, test environment -VITE_OTHER_SERVICE_BASE_URL= `{ - "demo": "http://localhost:9528" -}` +# # other backend service base url, test environment +# VITE_OTHER_SERVICE_BASE_URL= `{ +# "demo": "http://localhost:9528" +# }` diff --git a/src/typings/components.d.ts b/src/typings/components.d.ts index 305a155..f5f2db9 100644 --- a/src/typings/components.d.ts +++ b/src/typings/components.d.ts @@ -7,22 +7,44 @@ export {} /* prettier-ignore */ declare module 'vue' { export interface GlobalComponents { + ABreadcrumb: typeof import('ant-design-vue/es')['Breadcrumb'] + ABreadcrumbItem: typeof import('ant-design-vue/es')['BreadcrumbItem'] AButton: typeof import('ant-design-vue/es')['Button'] ACard: typeof import('ant-design-vue/es')['Card'] + ACheckbox: typeof import('ant-design-vue/es')['Checkbox'] ACol: typeof import('ant-design-vue/es')['Col'] + ADescriptions: typeof import('ant-design-vue/es')['Descriptions'] + ADescriptionsItem: typeof import('ant-design-vue/es')['DescriptionsItem'] + ADivider: typeof import('ant-design-vue/es')['Divider'] + ADrawer: typeof import('ant-design-vue/es')['Drawer'] + ADropdown: typeof import('ant-design-vue/es')['Dropdown'] AEmpty: typeof import('ant-design-vue/es')['Empty'] AForm: typeof import('ant-design-vue/es')['Form'] AFormItem: typeof import('ant-design-vue/es')['FormItem'] AInput: typeof import('ant-design-vue/es')['Input'] AInputNumber: typeof import('ant-design-vue/es')['InputNumber'] + AInputPassword: typeof import('ant-design-vue/es')['InputPassword'] + AInputSearch: typeof import('ant-design-vue/es')['InputSearch'] + AMenu: typeof import('ant-design-vue/es')['Menu'] + AMenuDivider: typeof import('ant-design-vue/es')['MenuDivider'] + AMenuItem: typeof import('ant-design-vue/es')['MenuItem'] AModal: typeof import('ant-design-vue/es')['Modal'] APagination: typeof import('ant-design-vue/es')['Pagination'] + APopconfirm: typeof import('ant-design-vue/es')['Popconfirm'] + APopover: typeof import('ant-design-vue/es')['Popover'] AppProvider: typeof import('./../components/common/app-provider.vue')['default'] + ARadio: typeof import('ant-design-vue/es')['Radio'] + ARadioGroup: typeof import('ant-design-vue/es')['RadioGroup'] ARow: typeof import('ant-design-vue/es')['Row'] + ASegmented: typeof import('ant-design-vue/es')['Segmented'] ASelect: typeof import('ant-design-vue/es')['Select'] ASelectOption: typeof import('ant-design-vue/es')['SelectOption'] + ASpace: typeof import('ant-design-vue/es')['Space'] + AStatistic: typeof import('ant-design-vue/es')['Statistic'] + ASwitch: typeof import('ant-design-vue/es')['Switch'] ATable: typeof import('ant-design-vue/es')['Table'] ATag: typeof import('ant-design-vue/es')['Tag'] + ATooltip: typeof import('ant-design-vue/es')['Tooltip'] ATree: typeof import('ant-design-vue/es')['Tree'] AWatermark: typeof import('ant-design-vue/es')['Watermark'] BetterScroll: typeof import('./../components/custom/better-scroll.vue')['default'] @@ -31,6 +53,19 @@ declare module 'vue' { DarkModeContainer: typeof import('./../components/common/dark-mode-container.vue')['default'] ExceptionBase: typeof import('./../components/common/exception-base.vue')['default'] FullScreen: typeof import('./../components/common/full-screen.vue')['default'] + IconAntDesignReloadOutlined: typeof import('~icons/ant-design/reload-outlined')['default'] + IconAntDesignSettingOutlined: typeof import('~icons/ant-design/setting-outlined')['default'] + IconCarbonAdd: typeof import('~icons/carbon/add')['default'] + IconGridiconsFullscreen: typeof import('~icons/gridicons/fullscreen')['default'] + IconGridiconsFullscreenExit: typeof import('~icons/gridicons/fullscreen-exit')['default'] + 'IconIc:roundPlus': typeof import('~icons/ic/round-plus')['default'] + IconIcRoundDelete: typeof import('~icons/ic/round-delete')['default'] + IconIcRoundPlus: typeof import('~icons/ic/round-plus')['default'] + IconIcRoundRefresh: typeof import('~icons/ic/round-refresh')['default'] + IconIcRoundRemove: typeof import('~icons/ic/round-remove')['default'] + IconIcRoundSearch: typeof import('~icons/ic/round-search')['default'] + IconMdiDrag: typeof import('~icons/mdi/drag')['default'] + IconMdiRefresh: typeof import('~icons/mdi/refresh')['default'] LangSwitch: typeof import('./../components/common/lang-switch.vue')['default'] LookForward: typeof import('./../components/custom/look-forward.vue')['default'] MenuToggler: typeof import('./../components/common/menu-toggler.vue')['default'] diff --git a/src/utils/api.ts b/src/utils/api.ts index d7dcd6a..e173a98 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -2,9 +2,11 @@ import axios from 'axios'; import { localStg } from '@/utils/storage'; +let baseUrl = import.meta.env.VITE_SERVICE_BASE_URL + // 封装网络请求的接口 export const apiRequest = async (method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, data?: any) => { - let url = "http://localhost:8080" + path; + let url = baseUrl + path; const token = localStg.get('token'); // 获取 token try { const response = await axios({ @@ -21,8 +23,8 @@ export const apiRequest = async (method: 'GET' | 'POST' | 'PUT' | 'DELETE', path } }; -export const apiRequestGet = async (path: string) => { - let url = "http://localhost:8080" + path; +export const apiRequestGet = async (path: string, params?:any) => { + let url = baseUrl + path; const token = localStg.get('token'); // 获取 token try { return axios.get( @@ -31,6 +33,26 @@ export const apiRequestGet = async (path: string) => { headers: { Authorization: `Bearer ${token}`, // 添加 Authorization 头 }, + params: params, + } + ); + } catch (error) { + throw new Error('请求失败'); // 抛出错误 + } +}; + +export const apiRequestPost = async (path: string,data:any, params?:any) => { + let url = baseUrl + path; + const token = localStg.get('token'); // 获取 token + try { + return axios.post( + url, + data, + { + headers: { + Authorization: `Bearer ${token}`, // 添加 Authorization 头 + }, + params: params, } ); } catch (error) { diff --git a/src/views/department/index.vue b/src/views/department/index.vue index 3eebb0b..699f9c5 100644 --- a/src/views/department/index.vue +++ b/src/views/department/index.vue @@ -97,7 +97,7 @@ const selectedKeys = ref([]); // 获取部门数据 const fetchDepartments = async () => { try { - const response = await apiRequestGet('/api/department/allDepartment'); + const response = await apiRequestGet('/department/allDepartment'); if (response.data.success) { // 过滤重复数据并构建树结构 diff --git a/src/views/employee/list/index.vue b/src/views/employee/list/index.vue index 7754b1f..1f68041 100644 --- a/src/views/employee/list/index.vue +++ b/src/views/employee/list/index.vue @@ -31,7 +31,7 @@ import { message, Tag as ATag, Divider as ADivider } from 'ant-design-vue'; import axios from 'axios'; import { localStg } from '@/utils/storage'; - + import { apiRequestGet } from '@/utils/api'; // 表格列配置 const columns = [ { @@ -132,15 +132,11 @@ try { // 请求远端接口 const token = localStg.get('token'); - const response = await axios.get('http://localhost:8080/api/employees/allEmployees', { - params: { + let params = { page: pagination.value.current, size: pagination.value.pageSize, - }, - headers: { - Authorization: `Bearer ${token}` // 添加 Authorization 头 } - }); + const response = await apiRequestGet('/employees/allEmployees', params); if (response.data.success) { dataSource.value = response.data.data; diff --git a/src/views/facility/index.vue b/src/views/facility/index.vue index 4f927ef..ab53d3e 100644 --- a/src/views/facility/index.vue +++ b/src/views/facility/index.vue @@ -100,7 +100,7 @@ const fetchData = async () => { code: searchForm.code || undefined, type: searchForm.type || undefined, }; - const response = await apiRequestGet('/api/facilities/list', { params }); + const response = await apiRequestGet('/facilities/list', params); if (response.data.success) { dataSource.value = response.data.data; pagination.total = response.data.total || response.data.data.length; diff --git a/src/views/orders/create/index.vue b/src/views/orders/create/index.vue index 45e854c..6c110dd 100644 --- a/src/views/orders/create/index.vue +++ b/src/views/orders/create/index.vue @@ -39,7 +39,7 @@ import axios from 'axios'; // 用于 HTTP 请求 import { useRouter } from 'vue-router'; // 用于导航,假设您使用了 Vue Router import { localStg } from '@/utils/storage'; - + import { apiRequestPost } from '@/utils/api'; // 假设您有一个 API 请求的工具函数 const formRef = ref(); // 表单引用 const router = useRouter(); // 获取路由实例 @@ -71,12 +71,7 @@ try { //await formRef.value.validate(); // 验证表单 - const token = localStg.get('token'); // 替换为你的 token 键 - const response = await axios.post('http://localhost:8080/api/work-orders', formState,{ - headers: { - Authorization: `Bearer ${token}` // 添加 Authorization 头 - } - }); // 发送 POST 请求 + const response = await apiRequestPost('/work-orders', formState); // 发送 POST 请求 if (response.data.success) { message.success('工单创建成功!'); diff --git a/src/views/orders/list/index.vue b/src/views/orders/list/index.vue index 347a7fb..12c170f 100644 --- a/src/views/orders/list/index.vue +++ b/src/views/orders/list/index.vue @@ -28,7 +28,7 @@ import { ref, onMounted } from 'vue'; import { Table as ATable, Tag as ATag } from 'ant-design-vue'; import axios from 'axios'; import { localStg } from '@/utils/storage'; - +import { apiRequestGet } from '@/utils/api'; // 表格列定义 const columns = [ { @@ -131,14 +131,11 @@ const fetchData = async (page = 1, pageSize = 10) => { const token = localStg.get('token'); // 替换为你的 token 键 try { - const response = await axios.get('http://localhost:8080/api/work-orders/my-orders', { + const response = await apiRequestGet('/work-orders/my-orders', { params: { page: page - 1, // 后端通常从0开始计数 size: pageSize, - }, - headers: { - Authorization: `Bearer ${token}` // 添加 Authorization 头 - } + } }); if (response.data.success) { diff --git a/src/views/plan/index.vue b/src/views/plan/index.vue index 3211ca6..b35005d 100644 --- a/src/views/plan/index.vue +++ b/src/views/plan/index.vue @@ -112,7 +112,7 @@ export default { loading.value = true; try { // 模拟分页参数传递给后端 - const response = await apiRequestGet("/api/inspection-plans/employee/my-plan") + const response = await apiRequestGet("/inspection-plans/employee/my-plan") if (response.data.success) { planData.value = response.data.data;