fix: get api url (#4020)

This commit is contained in:
chenos 2024-04-11 23:02:34 +08:00 committed by GitHub
parent beab81818f
commit e1533ec800
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 13 additions and 18 deletions

View File

@ -167,6 +167,15 @@ export class Application {
return this.options.publicPath || '/'; return this.options.publicPath || '/';
} }
getApiUrl(pathname = '') {
let baseURL = this.apiClient.axios['defaults']['baseURL'];
if (!baseURL.startsWith('http://') || !baseURL.startsWith('https://')) {
const { protocol, host } = window.location;
baseURL = `${protocol}//${host}/`;
}
return baseURL + pathname;
}
getRouteUrl(pathname: string) { getRouteUrl(pathname: string) {
return this.options.publicPath.replace(/\/$/g, '') + pathname; return this.options.publicPath.replace(/\/$/g, '') + pathname;
} }

View File

@ -41,8 +41,7 @@ export class WebSocketClient {
if (!this.app) { if (!this.app) {
return; return;
} }
const options = this.app.getOptions(); const apiBaseURL = this.app.getApiUrl();
const apiBaseURL = options?.apiClient?.['baseURL'];
if (!apiBaseURL) { if (!apiBaseURL) {
return; return;
} }

View File

@ -26,9 +26,7 @@ export const Iframe: any = observer(
const src = React.useMemo(() => { const src = React.useMemo(() => {
if (mode === 'html') { if (mode === 'html') {
const options = app.getOptions(); return app.getApiUrl(`iframeHtml:getHtml/${htmlId}?token=${api.auth.getToken()}&v=${field.data?.v || ''}`);
const apiBaseURL: string = options?.apiClient?.['baseURL'];
return `${apiBaseURL}iframeHtml:getHtml/${htmlId}?token=${api.auth.getToken()}&v=${field.data?.v || ''}`;
} }
return url; return url;
}, [app, url, mode, htmlId, field.data?.v]); }, [app, url, mode, htmlId, field.data?.v]);

View File

@ -330,12 +330,7 @@ const Usage = observer(
const app = useApp(); const app = useApp();
const url = useMemo(() => { const url = useMemo(() => {
const options = app.getOptions(); return app.getApiUrl('oidc:redirect');
const apiBaseURL: string = options?.apiClient?.['baseURL'];
const { protocol, host } = window.location;
return apiBaseURL.startsWith('http')
? `${apiBaseURL}oidc:redirect`
: `${protocol}//${host}${apiBaseURL}oidc:redirect`;
}, [app]); }, [app]);
const copy = (text: string) => { const copy = (text: string) => {

View File

@ -77,14 +77,8 @@ const Usage = observer(
const name = form.values.name ?? record.name; const name = form.values.name ?? record.name;
const url = useMemo(() => { const url = useMemo(() => {
const options = app.getOptions();
const apiBaseURL: string = options?.apiClient?.['baseURL'];
const { protocol, host } = window.location;
const appName = getSubAppName(app.getPublicPath()) || 'main'; const appName = getSubAppName(app.getPublicPath()) || 'main';
return app.getApiUrl(`saml:redirect?authenticator=${name}&__appName=${appName}`);
return apiBaseURL.startsWith('http')
? `${apiBaseURL}saml:redirect?authenticator=${name}&__appName=${appName}`
: `${protocol}//${host}${apiBaseURL}saml:redirect?authenticator=${name}&__appName=${appName}`;
}, [app, name]); }, [app, name]);
const copy = (text: string) => { const copy = (text: string) => {