feat: add base url utility and update api endpoints

This commit is contained in:
2025-11-11 11:27:56 +08:00
parent 21d46e7440
commit a606b5766a
12 changed files with 44 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ import { IAnswer, Message } from '@/interfaces/database/chat';
import { IKnowledgeFile } from '@/interfaces/database/knowledge';
import { IClientConversation, IMessage } from '@/pages/chat/interface';
import api from '@/utils/api';
import { withBaseUrl } from '@/utils/url';
import { getAuthorization } from '@/utils/authorization-util';
import { buildMessageUuid } from '@/utils/chat';
import { PaginationProps, message } from 'antd';
@@ -239,7 +240,7 @@ export const useSendMessageWithSse = (
initializeSseRef();
try {
setDoneValue(body, false);
const response = await fetch(url, {
const response = await fetch(withBaseUrl(url), {
method: 'POST',
headers: {
[Authorization]: getAuthorization(),
@@ -319,7 +320,7 @@ export const useSendMessageWithSse = (
export const useSpeechWithSse = (url: string = api.tts) => {
const read = useCallback(
async (body: any) => {
const response = await fetch(url, {
const response = await fetch(withBaseUrl(url), {
method: 'POST',
headers: {
[Authorization]: getAuthorization(),

View File

@@ -3,6 +3,7 @@ import { Authorization } from '@/constants/authorization';
import { IReferenceObject } from '@/interfaces/database/chat';
import { BeginQuery } from '@/pages/agent/interface';
import api from '@/utils/api';
import { withBaseUrl } from '@/utils/url';
import { getAuthorization } from '@/utils/authorization-util';
import { EventSourceParserStream } from 'eventsource-parser/stream';
import { useCallback, useRef, useState } from 'react';
@@ -108,7 +109,7 @@ export const useSendMessageBySSE = (url: string = api.completeConversation) => {
initializeSseRef();
try {
setDone(false);
const response = await fetch(url, {
const response = await fetch(withBaseUrl(url), {
method: 'POST',
headers: {
[Authorization]: getAuthorization(),

View File

@@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
import { Authorization } from '@/constants/authorization';
import { getAuthorization } from '@/utils/authorization-util';
import { withBaseUrl } from '@/utils/url';
interface ImageProps {
src: string;
@@ -15,7 +16,7 @@ const Image = ({ src, preview = false }: ImageProps) => {
useEffect(() => {
const loadImage = async () => {
try {
const response = await fetch(src, {
const response = await fetch(withBaseUrl(src), {
headers: {
[Authorization]: getAuthorization(),
},

View File

@@ -10,6 +10,7 @@ import {
} from '@/hooks/logic-hooks';
import { IAnswer } from '@/interfaces/database/chat';
import api from '@/utils/api';
import { withBaseUrl } from '@/utils/url';
import { get, isEmpty, isEqual, trim } from 'lodash';
import {
ChangeEventHandler,
@@ -156,7 +157,7 @@ export const useFetchBackgroundImage = () => {
const fetchImage = useCallback(async () => {
try {
const res = await fetch(
'/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN',
withBaseUrl('/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN'),
);
const ret = await res.json();
const url = get(ret, 'images.0.url');

View File

@@ -0,0 +1,24 @@
export const getBaseUrl = (): string => {
const url = process.env.UMI_APP_API_BASE_URL || '';
return url.replace(/\/+$/, '');
};
const isAbsoluteUrl = (u: string): boolean => {
if (!u) return false;
const lower = u.toLowerCase();
return (
lower.startsWith('http://') ||
lower.startsWith('https://') ||
lower.startsWith('data:') ||
lower.startsWith('blob:')
);
};
export const withBaseUrl = (input: string): string => {
if (!input) return input;
if (isAbsoluteUrl(input)) return input;
const base = getBaseUrl();
if (!base) return input;
if (input.startsWith('/')) return `${base}${input}`;
return `${base}/${input}`;
};