修改请求地址

v1.4.0
于肖磊 2025-08-08 18:58:17 +08:00
parent 7ca12ec6e9
commit d30902d716
6 changed files with 129 additions and 5 deletions

View File

@ -8,4 +8,6 @@ VITE_APP_PORT = 3000
VITE_APP_BASE_API = '/dev-admin'
VITE_APP_BASE_API_URL = 'http://shopxo.com/admin.php/'
VITE_APP_BASE_API_PHP = '/dev-api'
VITE_APP_BASE_API_PHP_URL = 'http://shopxo.com/api.php'
VITE_APP_BASE_API_PHP_URL = 'http://shopxo.com/api.php'
VITE_APP_BASE_API_INDEX_PHP = '/dev-index'
VITE_APP_BASE_API_INDEX_PHP_URL = 'http://shopxo.com/index.php'

View File

@ -1,13 +1,13 @@
import { get_type } from '@/utils/common';
import request from '@/utils/request';
import api_request from '@/utils/api-request';
import index_request from '@/utils/index-request';
import { isEmpty } from 'lodash';
class CommonAPI {
/** 链接初始化接口 */
static getInit() {
if (get_type() == 'shop') {
return api_request({
return index_request({
url: `?s=plugins/index/pluginsname/shop/pluginscontrol/diyapi/pluginsaction/init.html`,
method: 'post',
});

View File

@ -24,7 +24,7 @@ const isLogoutModalShown = ref(true);
// 用于存储每个请求的CancelToken
const pendingRequests = new Map();
// 不需要认证的接口
const release_url = ['attachmentapi/attachmentupload'];
const release_url = ['attachmentapi/upload'];
// 创建 axios 实例
const index = window.location.href.lastIndexOf('?s=');
const new_data = window.location.href.substring(0, index);

116
src/utils/index-request.ts Normal file
View File

@ -0,0 +1,116 @@
import axios, { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
import { ElMessage, ElMessageBox, type MessageHandler } from 'element-plus';
import { get_cookie } from './index';
import { get_type } from './common';
// 提示拦截
let messageInstance: MessageHandler;
const message_error = (info: string) => {
if (messageInstance) {
messageInstance.close();
}
messageInstance = ElMessage.error({
type: 'error',
message: info,
duration: 30000,
showClose: true,
});
};
// 创建一个状态变量来跟踪是否已经弹出了退出登录的弹窗
const isLogoutModalShown = ref(true);
// 用于存储每个请求的CancelToken
const pendingRequests = new Map();
// 不需要认证的接口
const release_url = ['attachmentapi/upload'];
// 创建 axios 实例
const index = window.location.href.lastIndexOf('?s=');
const new_data = window.location.href.substring(0, index);
const new_index = new_data.lastIndexOf('/');
const pro_url = window.location.href.substring(0, new_index);
const service = axios.create({
baseURL: import.meta.env.VITE_APP_BASE_API_INDEX_PHP == '/dev-index' ? import.meta.env.VITE_APP_BASE_API_INDEX_PHP : pro_url + '/index.php',
timeout: 60000,
headers: { 'Content-Type': 'application/json;charset=utf-8', 'X-Requested-With': 'XMLHttpRequest' },
});
/** @ts-ignore */
// 请求拦截器
service.interceptors.request.use(
async (config: InternalAxiosRequestConfig) => {
// 如果是本地则使用静态tonken如果是线上则使用cookie的token
const symbol = config.url?.includes('?') ? '&' : '?';
if (import.meta.env.VITE_APP_BASE_API_INDEX_PHP == '/dev-index') {
let temp_data = await import(import.meta.env.VITE_APP_BASE_API_INDEX_PHP == '/dev-index' ? '../../temp.d' : '../../temp_pro.d');
config.url = config.url + symbol + 'token=' + temp_data.default.temp_token;
} else {
// 如果是shop认为是多商户插件使用user_info的cookie
const cookie = get_type() == 'shop' ? get_cookie('user_info') : get_cookie('admin_info');
if (cookie && cookie !== null && cookie !== 'null') {
config.url = config.url + '&token=' + (JSON.parse(cookie) !== 'null' ? JSON.parse(cookie)?.token : '');
}
}
// 判断是否是包含不需要认证的接口
const release_list = release_url.filter(item => config.url?.includes(item));
if (release_list.length === 0) {
// 检查是否有相同请求正在进行,如果有则取消, 防止重复请求导致返回数据有误
if (pendingRequests.has(config.url)) {
const cancelToken = pendingRequests.get(config.url);
cancelToken.cancel('canceled');
pendingRequests.delete(config.url);
}
// 创建一个新的 CancelToken
const source = axios.CancelToken.source();
config.cancelToken = source.token;
pendingRequests.set(config.url, source);
}
return config;
},
(error: any) => {
return Promise.reject(error);
}
);
// 响应拦截器
service.interceptors.response.use(
(response: AxiosResponse) => {
// 请求完成后从pendingRequests中移除
pendingRequests.delete(response.config.url);
const { code, msg, message, data } = response.data;
if (code == 0) {
return response.data;
} else if (code == -400) {
if (isLogoutModalShown.value) {
isLogoutModalShown.value = false;
ElMessageBox.alert(msg, '温馨提示', {
confirmButtonText: '确定',
showClose: false,
type: 'warning',
}).then(() => {
localStorage.clear(); // @vueuse/core 自动导入
window.location.href = data.logout;
});
}
} else {
message_error(msg || message || '系统出错');
return Promise.reject('Error');
// return Promise.reject(new Error(msg || 'Error'));
}
},
(error: any) => {
if (error.response && error.response.data) {
const { msg, message } = error.response.data;
message_error(msg || message || '系统出错');
} else if (error.message == 'canceled') {
console.log('请求已取消');
} else {
message_error(error.message);
}
return Promise.reject(error.message);
}
);
// 导出 axios 实例
export default service;

View File

@ -22,7 +22,7 @@ const isLogoutModalShown = ref(true);
// 用于存储每个请求的CancelToken
const pendingRequests = new Map();
// 不需要认证的接口
const release_url = ['attachmentapi/attachmentupload'];
const release_url = ['attachmentapi/upload'];
// 创建 axios 实例
const index = window.location.href.lastIndexOf('?s=');
const pro_url = window.location.href.substring(0, index);

View File

@ -45,6 +45,12 @@ export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
changeOrigin: true,
rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API_PHP), ''), // 替换 /dev-api 为 target 接口地址
},
// 反向代理解决跨域
[env.VITE_APP_BASE_API_INDEX_PHP]: {
target: env.VITE_APP_BASE_API_INDEX_PHP_URL, // 接口地址
changeOrigin: true,
rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API_INDEX_PHP), ''), // 替换 /dev-index 为 target 接口地址
},
},
},
plugins: [