| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- import axios, { AxiosInstance, AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse } from "axios";
- import { showFullScreenLoading, tryHideFullScreenLoading } from "@/components/Loading/fullScreen";
- import { LOGIN_URL } from "@/config";
- import { ElMessage } from "element-plus";
- import { ResultData } from "@/api/interface";
- import { ResultEnum } from "@/enums/httpEnum";
- import { checkStatus } from "./helper/checkStatus";
- import { AxiosCanceler } from "./helper/axiosCancel";
- import { useUserStore } from "@/stores/modules/user";
- import router from "@/routers";
- import { localGet, localSet } from "@/utils";
- import { cryptoUtil } from "@/utils/crypto";
- import { cryptoStrategy } from "@/utils/cryptoStrategy";
- export interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
- loading?: boolean;
- cancel?: boolean;
- encrypt?: boolean; // 是否加密请求参数
- }
- const config = {
- // AI服务地址,使用 /ai-api 前缀,由 VITE_PROXY_AI 代理到 http://192.168.2.250:9000
- baseURL: "/ai-api",
- // 设置超时时间
- timeout: ResultEnum.TIMEOUT as number,
- // 跨域时候允许携带凭证
- withCredentials: true
- };
- const axiosCanceler = new AxiosCanceler();
- // AI Token存储key
- const AI_TOKEN_KEY = "ai-service-token";
- /**
- * 获取AI服务Token
- */
- const getAiToken = (): string | null => {
- return localGet(AI_TOKEN_KEY) || null;
- };
- /**
- * 设置AI服务Token
- */
- const setAiToken = (token: string) => {
- localSet(AI_TOKEN_KEY, token);
- };
- /**
- * AI服务登录
- * @returns AI服务token,如果登录失败返回null
- */
- export const aiLogin = async (): Promise<string | null> => {
- try {
- // 获取主系统的用户信息
- const userInfo = localGet("geeker-user");
- if (!userInfo || !userInfo.userInfo) {
- console.error("无法获取用户信息,AI服务登录失败");
- return null;
- }
- // 创建一个临时的axios实例用于登录(不使用拦截器,避免循环)
- const loginInstance = axios.create({
- baseURL: "/ai-api",
- timeout: ResultEnum.TIMEOUT as number,
- withCredentials: true
- });
- // 调用AI登录接口,使用主系统的用户名和密码
- // 根据实际接口要求调整参数
- const loginParams: any = {
- username: userInfo.userInfo.nickName,
- password: userInfo.userInfo.password
- };
- // 如果没有密码,可能需要使用主系统的token来换取AI服务的token
- // 或者使用其他认证方式,这里需要根据实际接口文档调整
- if (!loginParams.password) {
- // 如果主系统没有存储密码,可能需要使用token或其他方式
- // 暂时尝试使用主系统的token
- loginParams.token = userInfo.token;
- }
- const response = await loginInstance.post("/ai/user-auth-core/api/v1/auth/login", loginParams);
- // 根据实际返回格式调整
- if (response.data) {
- // 可能返回格式:{ code: 200, data: { token: "..." } }
- // 或者:{ code: 200, token: "..." }
- const token = response.data.data?.token || response.data.token;
- if (token) {
- setAiToken(token);
- return token;
- }
- }
- return null;
- } catch (error: any) {
- console.error("AI服务登录失败:", error);
- // 如果登录失败,返回null,让请求失败
- return null;
- }
- };
- class RequestHttp {
- service: AxiosInstance;
- private isLoggingIn = false; // 防止重复登录
- private loginPromise: Promise<string | null> | null = null; // 登录Promise,用于并发请求时共享登录结果
- public constructor(config: AxiosRequestConfig) {
- // instantiation
- this.service = axios.create(config);
- /**
- * @description 请求拦截器
- * 客户端发送请求 -> [请求拦截器] -> 服务器
- * token校验(JWT) : 先获取AI服务token,如果没有则先登录获取token
- */
- this.service.interceptors.request.use(
- async (config: CustomAxiosRequestConfig) => {
- // 重复请求不需要取消,在 api 服务中通过指定的第三个参数: { cancel: false } 来控制
- config.cancel ??= true;
- config.cancel && axiosCanceler.addPending(config);
- // 当前请求不需要显示 loading,在 api 服务中通过指定的第三个参数: { loading: false } 来控制
- config.loading ??= true;
- config.loading && showFullScreenLoading();
- // 获取AI服务token
- let aiToken = getAiToken();
- // 如果没有token,先登录获取token
- if (!aiToken) {
- // 如果正在登录,等待登录完成
- if (this.isLoggingIn && this.loginPromise) {
- aiToken = await this.loginPromise;
- } else {
- // 开始登录
- this.isLoggingIn = true;
- this.loginPromise = aiLogin();
- aiToken = await this.loginPromise;
- this.isLoggingIn = false;
- this.loginPromise = null;
- }
- }
- // 设置Authorization header
- if (config.headers) {
- if (typeof config.headers.set === "function") {
- config.headers.set("Authorization", aiToken || "");
- } else {
- (config.headers as any)["Authorization"] = aiToken || "";
- }
- }
- // 加密处理
- if (cryptoStrategy.shouldEncrypt(config) && config.data) {
- try {
- config.data = cryptoUtil.encrypt(config.data);
- } catch (error) {
- return Promise.reject(error);
- }
- }
- return config;
- },
- (error: AxiosError) => {
- return Promise.reject(error);
- }
- );
- /**
- * @description 响应拦截器
- * 服务器换返回信息 -> [拦截统一处理] -> 客户端JS获取到信息
- */
- this.service.interceptors.response.use(
- (response: AxiosResponse & { config: CustomAxiosRequestConfig }) => {
- const { data, config, headers } = response;
- const userStore = useUserStore();
- axiosCanceler.removePending(config);
- config.loading && tryHideFullScreenLoading();
- // 解密处理
- if (cryptoStrategy.shouldDecrypt(config, headers, data)) {
- try {
- const decryptedData = cryptoUtil.decrypt(data);
- response.data = decryptedData;
- } catch (error) {
- console.error("解密失败:", error);
- ElMessage.error("响应数据解密失败");
- return Promise.reject(new Error("响应数据解密失败"));
- }
- }
- const processedData = response.data;
- // 登录失效 - AI服务token过期,清除token,下次请求时会重新登录
- if (processedData.code == ResultEnum.OVERDUE || processedData.code === 401) {
- localSet(AI_TOKEN_KEY, null);
- // 如果是登录接口本身失败,不显示错误(避免循环)
- if (!config.url?.includes("/auth/login")) {
- ElMessage.error(processedData.msg || "AI服务认证失败,请重试");
- }
- return Promise.reject(processedData);
- }
- // 全局错误信息拦截(防止下载文件的时候返回数据流,没有 code 直接报错)
- if (processedData.code && processedData.code !== ResultEnum.SUCCESS) {
- ElMessage.error(processedData.msg);
- return Promise.reject(processedData);
- }
- // 成功请求(在页面上除非特殊情况,否则不用处理失败逻辑)
- return processedData;
- },
- async (error: AxiosError) => {
- const { response } = error;
- tryHideFullScreenLoading();
- // 请求超时 && 网络错误单独判断,没有 response
- if (error.message.indexOf("timeout") !== -1) ElMessage.error("请求超时!请您稍后重试");
- if (error.message.indexOf("Network Error") !== -1) ElMessage.error("网络错误!请您稍后重试");
- // 根据服务器响应的错误状态码,做不同的处理
- if (response && response.data && (response.data as any).message) {
- ElMessage.error((response.data as any).message);
- } else {
- if (response) checkStatus(response.status);
- }
- // 服务器结果都没有返回(可能服务器错误可能客户端断网),断网处理:可以跳转到断网页面
- if (!window.navigator.onLine) router.replace("/500");
- return Promise.reject(error);
- }
- );
- }
- /**
- * @description 常用请求方法封装
- */
- get<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
- return this.service.get(url, { params, ..._object });
- }
- post<T>(url: string, params?: object | string, _object = {}): Promise<ResultData<T>> {
- return this.service.post(url, params, _object);
- }
- put<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
- return this.service.put(url, params, _object);
- }
- delete<T>(url: string, params?: any, _object = {}): Promise<ResultData<T>> {
- return this.service.delete(url, { params, ..._object });
- }
- download(url: string, params?: object, _object = {}): Promise<BlobPart> {
- return this.service.post(url, params, { ..._object, responseType: "blob" });
- }
- }
- export default new RequestHttp(config);
|