From 90a83973503087f1f9bd73830f2e8acf581938d3 Mon Sep 17 00:00:00 2001 From: dzl <786316265@qq.com> Date: Wed, 24 Sep 2025 16:01:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Captcha.vue | 4 +- src/utils/api.js | 248 +++++++++++++++++++------------------ vite.config.js | 11 +- 3 files changed, 141 insertions(+), 122 deletions(-) diff --git a/src/components/Captcha.vue b/src/components/Captcha.vue index 99d6a86..48f7bb4 100644 --- a/src/components/Captcha.vue +++ b/src/components/Captcha.vue @@ -81,7 +81,9 @@ const loading = ref(false) const getCaptcha = async () => { try { loading.value = true - const response = await api.get('/captcha/generate') + // const response = await api.get('/captcha/generate') + // const response = await api.get('/auth/login') + const response = await api.auth.getcap() if (response.data.success) { captchaImage.value = response.data.data.image diff --git a/src/utils/api.js b/src/utils/api.js index 27ca86e..f4308e0 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -3,43 +3,17 @@ import { ElMessage, ElLoading } from 'element-plus' import NProgress from 'nprogress' // 创建axios实例 -const request = axios.create({ - baseURL: '/api', - timeout: 10000, - headers: { - 'Content-Type': 'application/json' - } -}) - -let loadingInstance = null -let requestCount = 0 -let isLoggingOut = false // 防止重复登出 - -// 显示加载 -const showLoading = () => { - if (requestCount === 0) { - loadingInstance = ElLoading.service({ - text: '加载中...', - background: 'rgba(0, 0, 0, 0.7)' - }) - } - requestCount++ -} - -// 隐藏加载 -const hideLoading = () => { - requestCount-- - if (requestCount <= 0) { - requestCount = 0 - if (loadingInstance) { - loadingInstance.close() - loadingInstance = null +// 工厂函数(复用拦截器逻辑) +export const createRequest = (baseURL) => { + const request = axios.create({ + baseURL, + timeout: 10000, + headers: { + 'Content-Type': 'application/json' } - } -} - -// 请求拦截器 -request.interceptors.request.use( + }) + // 请求拦截器 + request.interceptors.request.use( (config) => { // 开始进度条 NProgress.start() @@ -64,8 +38,8 @@ request.interceptors.request.use( } ) -// 响应拦截器 -request.interceptors.response.use( + // 响应拦截器 + request.interceptors.response.use( (response) => { hideLoading() NProgress.done() @@ -156,38 +130,76 @@ request.interceptors.response.use( } ) + return request +} + +// 生成不同的实例 +export const apiRequest = createRequest(import.meta.env.VITE_API_BASE_URL || '/api') +export const midRequest = createRequest(import.meta.env.VITE_UPLOAD_BASE_URL || '/mid') +export const statsRequest = createRequest(import.meta.env.VITE_STATS_BASE_URL || '/stats') + +let loadingInstance = null +let requestCount = 0 +let isLoggingOut = false // 防止重复登出 + +// 显示加载 +const showLoading = () => { + if (requestCount === 0) { + loadingInstance = ElLoading.service({ + text: '加载中...', + background: 'rgba(0, 0, 0, 0.7)' + }) + } + requestCount++ +} + +// 隐藏加载 +const hideLoading = () => { + requestCount-- + if (requestCount <= 0) { + requestCount = 0 + if (loadingInstance) { + loadingInstance.close() + loadingInstance = null + } + } +} + + + // API接口定义 const api = { // 认证相关 auth: { - login: (data) => request.post('/auth/login', data), - register: (data) => request.post('/auth/register', data), - getCurrentUser: () => request.get('/auth/me'), - changePassword: (data) => request.put('/auth/change-password', data) + login: (data) => midRequest.post('/auth/login', data), + getcap: () => midRequest.get('/captcha/generate'), + register: (data) => apiRequest.post('/auth/register', data), + getCurrentUser: () => apiRequest.get('/auth/me'), + changePassword: (data) => apiRequest.put('/auth/change-password', data) }, // 用户管理 users: { - getUsers: (params) => request.get('/users', { params }), - getUserById: (id) => request.get(`/users/${id}`), - createUser: (data) => request.post('/users', data), - updateUser: (id, data) => request.put(`/users/${id}`, data), - deleteUser: (id) => request.delete(`/users/${id}`), - getUserStats: () => request.get('/users/stats'), - getUserGrowthTrend: (params) => request.get('/users/growth-trend', { params }), - getDailyRevenue: (params) => request.get('/users/daily-revenue', { params }), - getAgentOptions: (params) => request.get('/admin/agents', { params }) + getUsers: (params) => apiRequest.get('/users', { params }), + getUserById: (id) => apiRequest.get(`/users/${id}`), + createUser: (data) => apiRequest.post('/users', data), + updateUser: (id, data) => apiRequest.put(`/users/${id}`, data), + deleteUser: (id) => apiRequest.delete(`/users/${id}`), + getUserStats: () => apiRequest.get('/users/stats'), + getUserGrowthTrend: (params) => apiRequest.get('/users/growth-trend', { params }), + getDailyRevenue: (params) => apiRequest.get('/users/daily-revenue', { params }), + getAgentOptions: (params) => apiRequest.get('/admin/agents', { params }) }, // 积分管理 points: { - getStats: () => request.get('/points/stats'), - getHistory: (params) => request.get('/points/history', { params }), - adjustPoints: (data) => request.post('/points/adjust', data) + getStats: () => apiRequest.get('/points/stats'), + getHistory: (params) => apiRequest.get('/points/history', { params }), + adjustPoints: (data) => apiRequest.post('/points/adjust', data) }, beans: { - getHistory: (params) => request.get('/transfers/history', { params }), + getHistory: (params) => apiRequest.get('/transfers/history', { params }), }, // 文件上传 @@ -195,7 +207,7 @@ const api = { uploadImage: (file) => { const formData = new FormData() formData.append('image', file) - return request.post('/upload/image', formData, { + return midRequest.post('/upload/image', formData, { headers: { 'Content-Type': 'multipart/form-data' } @@ -204,7 +216,7 @@ const api = { uploadFile: (file) => { const formData = new FormData() formData.append('file', file) - return request.post('/upload/file', formData, { + return midRequest.post('/upload/file', formData, { headers: { 'Content-Type': 'multipart/form-data' } @@ -214,118 +226,118 @@ const api = { // 系统统计 dashboard: { - getStats: () => request.get('/dashboard/stats', { hideLoading: true }), - getChartData: (type) => request.get(`/dashboard/charts/${type}`, { hideLoading: true }) + getStats: () => apiRequest.get('/dashboard/stats', { hideLoading: true }), + getChartData: (type) => apiRequest.get(`/dashboard/charts/${type}`, { hideLoading: true }) }, // 激活码管理 registrationCodes: { - getRegistrationCodes: (params) => request.get('/users/registration-codes', { params }), - createRegistrationCode: (data) => request.post('/users/registration-codes', data), - batchCreateRegistrationCodes: (data) => request.post('/users/registration-codes/batch', data), - deleteRegistrationCode: (id) => request.delete(`/users/registration-codes/${id}`) + getRegistrationCodes: (params) => apiRequest.get('/users/registration-codes', { params }), + createRegistrationCode: (data) => apiRequest.post('/users/registration-codes', data), + batchCreateRegistrationCodes: (data) => apiRequest.post('/users/registration-codes/batch', data), + deleteRegistrationCode: (id) => apiRequest.delete(`/users/registration-codes/${id}`) }, // 转账管理 transfers: { - getTransfers: (params) => request.get('/transfers', { params }), - getTransferById: (id) => request.get(`/transfers/${id}`), - confirmTransfer: (id) => request.post('/transfers/confirm', { transfer_id: id }), - rejectTransfer: (id) => request.post('/transfers/reject', { transfer_id: id }), - confirmReceived: (id) => request.post('/transfers/confirm-received', { transfer_id: id }), - confirmNotReceived: (id) => request.post('/transfers/confirm-not-received', { transfer_id: id }), - getTransferStats: () => request.get('/transfers/stats'), - getTransferTrend: (params) => request.get('/transfers/trend', { params }), - getPublicAccount: () => request.get('/transfers/public-account'), - getUserTransfers: (userId, params) => request.get(`/transfers/user/${userId}`, { params }), - getPendingTransfers: () => request.get('/transfers/pending'), - getAccountInfo: (userId) => request.get(`/transfers/account/${userId}`), + getTransfers: (params) => apiRequest.get('/transfers', { params }), + getTransferById: (id) => apiRequest.get(`/transfers/${id}`), + confirmTransfer: (id) => apiRequest.post('/transfers/confirm', { transfer_id: id }), + rejectTransfer: (id) => apiRequest.post('/transfers/reject', { transfer_id: id }), + confirmReceived: (id) => apiRequest.post('/transfers/confirm-received', { transfer_id: id }), + confirmNotReceived: (id) => apiRequest.post('/transfers/confirm-not-received', { transfer_id: id }), + getTransferStats: () => apiRequest.get('/transfers/stats'), + getTransferTrend: (params) => apiRequest.get('/transfers/trend', { params }), + getPublicAccount: () => apiRequest.get('/transfers/public-account'), + getUserTransfers: (userId, params) => apiRequest.get(`/transfers/user/${userId}`, { params }), + getPendingTransfers: () => apiRequest.get('/transfers/pending'), + getAccountInfo: (userId) => apiRequest.get(`/transfers/account/${userId}`), createAdminTransfer: (fromUserId, data) => { - return request.post('/transfers/admin/create', { + return apiRequest.post('/transfers/admin/create', { from_user_id: fromUserId, ...data }) }, - removeBadDebt: (transferId, reason) => request.post(`/transfers/remove-bad-debt/${transferId}`, { reason }), + removeBadDebt: (transferId, reason) => apiRequest.post(`/transfers/remove-bad-debt/${transferId}`, { reason }), // 强制变更转账状态 - forceChangeStatus: (transferId, data) => request.post(`/transfers/force-change-status/${transferId}`, data), + forceChangeStatus: (transferId, data) => apiRequest.post(`/transfers/force-change-status/${transferId}`, data), // 数据库监控 - getDatabaseStatus: () => request.get('/transfers/admin/database/status'), - getDatabaseReport: () => request.get('/transfers/admin/database/report'), + getDatabaseStatus: () => apiRequest.get('/transfers/admin/database/status'), + getDatabaseReport: () => apiRequest.get('/transfers/admin/database/report'), // 待处理匹配订单相关 - getPendingAllocations: (params) => request.get('/transfers/pending-allocations', { params }), - getPendingAllocationStats: () => request.get('/transfers/pending-allocations/stats'), + getPendingAllocations: (params) => apiRequest.get('/transfers/pending-allocations', { params }), + getPendingAllocationStats: () => apiRequest.get('/transfers/pending-allocations/stats'), // 昨日用户转账统计 - getDailyStats: () => request.get('/transfers/daily-stats') + getDailyStats: () => apiRequest.get('/transfers/daily-stats') }, // 系统设置 system: { - getSettings: () => request.get('/system/settings'), + getSettings: () => apiRequest.get('/system/settings'), updateSettings: (category, data) => { // 构造符合后端期望的数据格式 const payload = { [category]: data } - return request.put('/system/settings', payload) + return apiRequest.put('/system/settings', payload) }, - getSystemInfo: () => request.get('/system/info'), + getSystemInfo: () => apiRequest.get('/system/info'), }, // 匹配管理 matching: { - getUnreasonableMatches: (params) => request.get('/admin/matching/unreasonable-matches', { params }), - getMatchingStats: () => request.get('/admin/matching/matching-stats'), - fixUnreasonableMatch: (allocationId, data) => request.post(`/admin/matching/fix-unreasonable-match/${allocationId}`, data), - fixAllUnreasonable: () => request.post('/admin/matching/fix-all-unreasonable'), - confirmAllocation: (allocationId) => request.post(`/admin/matching/confirm-allocation/${allocationId}`), - cancelAllocation: (allocationId) => request.post(`/admin/matching/cancel-allocation/${allocationId}`) + getUnreasonableMatches: (params) => apiRequest.get('/admin/matching/unreasonable-matches', { params }), + getMatchingStats: () => apiRequest.get('/admin/matching/matching-stats'), + fixUnreasonableMatch: (allocationId, data) => apiRequest.post(`/admin/matching/fix-unreasonable-match/${allocationId}`, data), + fixAllUnreasonable: () => apiRequest.post('/admin/matching/fix-all-unreasonable'), + confirmAllocation: (allocationId) => apiRequest.post(`/admin/matching/confirm-allocation/${allocationId}`), + cancelAllocation: (allocationId) => apiRequest.post(`/admin/matching/cancel-allocation/${allocationId}`) }, // 商品管理 products: { - getProducts: (params) => request.get('/products', { params }), - getProductById: (id) => request.get(`/products/${id}`), - createProduct: (data) => request.post('/products', data), - updateProduct: (id, data) => request.put(`/products/${id}`, data), - deleteProduct: (id) => request.delete(`/products/${id}`), - getCategories: () => request.get('/products/categories'), + getProducts: (params) => apiRequest.get('/products', { params }), + getProductById: (id) => apiRequest.get(`/products/${id}`), + createProduct: (data) => apiRequest.post('/products', data), + updateProduct: (id, data) => apiRequest.put(`/products/${id}`, data), + deleteProduct: (id) => apiRequest.delete(`/products/${id}`), + getCategories: () => apiRequest.get('/products/categories'), // 商品属性 - getAttributes: (productId) => request.get(`/products/${productId}/attributes`), - createAttribute: (productId, data) => request.post(`/products/${productId}/attributes`, data), - updateAttribute: (productId, attrId, data) => request.put(`/products/${productId}/attributes/${attrId}`, data), - deleteAttribute: (productId, attrId) => request.delete(`/products/${productId}/attributes/${attrId}`) + getAttributes: (productId) => apiRequest.get(`/products/${productId}/attributes`), + createAttribute: (productId, data) => apiRequest.post(`/products/${productId}/attributes`, data), + updateAttribute: (productId, attrId, data) => apiRequest.put(`/products/${productId}/attributes/${attrId}`, data), + deleteAttribute: (productId, attrId) => apiRequest.delete(`/products/${productId}/attributes/${attrId}`) }, // 新的规格管理系统(笛卡尔积) specifications: { // 规格名称管理 - getSpecNames: () => request.get('/specifications/names'), - createSpecName: (data) => request.post('/specifications/names', data), - updateSpecName: (id, data) => request.put(`/specifications/names/${id}`, data), - deleteSpecName: (id) => request.delete(`/specifications/names/${id}`), + getSpecNames: () => apiRequest.get('/specifications/names'), + createSpecName: (data) => apiRequest.post('/specifications/names', data), + updateSpecName: (id, data) => apiRequest.put(`/specifications/names/${id}`, data), + deleteSpecName: (id) => apiRequest.delete(`/specifications/names/${id}`), // 规格值管理 - getSpecValues: (specNameId) => request.get('/specifications/values', { params: { spec_name_id: specNameId } }), - createSpecValue: (data) => request.post('/specifications/values', data), - updateSpecValue: (id, data) => request.put(`/specifications/values/${id}`, data), - deleteSpecValue: (id) => request.delete(`/specifications/values/${id}`), + getSpecValues: (specNameId) => apiRequest.get('/specifications/values', { params: { spec_name_id: specNameId } }), + createSpecValue: (data) => apiRequest.post('/specifications/values', data), + updateSpecValue: (id, data) => apiRequest.put(`/specifications/values/${id}`, data), + deleteSpecValue: (id) => apiRequest.delete(`/specifications/values/${id}`), // 规格组合管理 - getCombinations: (productId) => request.get(`/specifications/combinations/${productId}`), - generateCombinations: (data) => request.post('/specifications/generate-combinations', data), - updateCombination: (id, data) => request.put(`/specifications/combinations/${id}`, data), - deleteCombination: (id) => request.delete(`/specifications/combinations/${id}`) + getCombinations: (productId) => apiRequest.get(`/specifications/combinations/${productId}`), + generateCombinations: (data) => apiRequest.post('/specifications/generate-combinations', data), + updateCombination: (id, data) => apiRequest.put(`/specifications/combinations/${id}`, data), + deleteCombination: (id) => apiRequest.delete(`/specifications/combinations/${id}`) }, // 为了向后兼容,添加直接的 get、post 等方法 - get: (url, config) => request.get(url, config), - post: (url, data, config) => request.post(url, data, config), - put: (url, data, config) => request.put(url, data, config), - delete: (url, config) => request.delete(url, config) + get: (url, config) => apiRequest.get(url, config), + post: (url, data, config) => apiRequest.post(url, data, config), + put: (url, data, config) => apiRequest.put(url, data, config), + delete: (url, config) => apiRequest.delete(url, config) } export default api \ No newline at end of file diff --git a/vite.config.js b/vite.config.js index 3b21ff2..2d04f54 100644 --- a/vite.config.js +++ b/vite.config.js @@ -17,15 +17,20 @@ export default defineConfig({ host:'0.0.0.0', proxy: { '/api': { - target: 'http://localhost:3000', + target: 'http://192.168.0.11:3000', changeOrigin: true }, + '/mid': { + target: 'http://192.168.0.12:3005', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/mid/, '') + }, // '/admin': { - // target: 'http://localhost:3000', + // target: 'http://192.168.208.158:3000', // changeOrigin: true // }, '/uploads': { - target: 'http://localhost:3000', + target: 'http://192.168.0.11:3000', changeOrigin: true } }