Add utility functions for handling public URLs and update API base URL logic

- Implement getProductionApiBaseUrl and normalizePublicUrl functions to manage API URLs based on the browser's origin.
- Update API base URL in request configuration to use getProductionApiBaseUrl.
- Enhance QR code handling in ParticipatingCompaniesTab and OutdoorFairList components to normalize URLs.
- Create a deployment script for frontend builds and server uploads.
- Add tests for the new URL utility functions.
This commit is contained in:
2026-07-15 08:46:40 +08:00
parent 5672bfcbc6
commit 00ba967685
7 changed files with 241 additions and 6 deletions

View File

@@ -23,6 +23,7 @@ import {
import { PageEnum } from './enums/pagesEnums';
import { stringify } from 'querystring';
import { message } from 'antd';
import { getProductionApiBaseUrl } from './utils/publicUrl';
const isDev = process.env.NODE_ENV === 'development';
const loginOut = async () => {
@@ -44,6 +45,8 @@ const loginOut = async () => {
}
};
/**
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */
@@ -268,7 +271,7 @@ export const request = {
// baseURL: process.env.NODE_ENV === 'development' ? '' : 'http://ks.zhaopinzao8dian.com/api/ks',
// baseURL: process.env.NODE_ENV === 'development' ? '' : 'http://36.105.163.21:30081/api/ks',
// baseURL: process.env.NODE_ENV === 'development' ? '' : 'https://xjshzly.longbiosphere.com:30081/api/ks',
baseURL: process.env.NODE_ENV === 'development' ? '' : 'http://39.98.44.136:6024/api/shihezi/',
baseURL: process.env.NODE_ENV === 'development' ? '' : getProductionApiBaseUrl(),
// baseURL: 'http://39.98.44.136:8080',
requestInterceptors: [
(url: any, options: { headers: any }) => {
@@ -321,4 +324,3 @@ export const request = {
},
],
};

View File

@@ -4,6 +4,7 @@ import { PlusOutlined } from '@ant-design/icons';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { getDictValueEnum } from '@/services/system/dict';
import { normalizePublicUrl } from '@/utils/publicUrl';
import {
getParticipatingCompanyQrCode,
getParticipatingCompanies,
@@ -98,7 +99,11 @@ const ParticipatingCompaniesTab: React.FC<Props> = ({ fairId }) => {
message.error(res.msg || '二维码内容获取失败');
return;
}
setQrInfo(res.data);
setQrInfo({
...res.data,
h5Url: normalizePublicUrl(res.data.h5Url) || res.data.h5Url,
qrCodeContent: normalizePublicUrl(res.data.qrCodeContent) || res.data.qrCodeContent,
});
setQrCompanyName(record.companyName);
setQrModalOpen(true);
};

View File

@@ -29,6 +29,7 @@ import type {
import { getParticipatingJobs } from '@/services/jobportal/outdoorFairDetail';
import { getDictSelectOption, getDictValueEnum } from '@/services/system/dict';
import { getVenueInfoList } from '@/services/jobportal/venueInfo';
import { normalizePublicUrl } from '@/utils/publicUrl';
import EditModal from './components/EditModal';
import JobEditModal from './Detail/components/JobEditModal';
@@ -175,7 +176,11 @@ const OutdoorFairList: React.FC = () => {
const openQrCode = async (record: OutdoorFairItem) => {
const res = await getMyOutdoorFairQrCode(record.id);
if (res.code === 200) {
setQrInfo(res.data);
setQrInfo({
...res.data,
h5Url: normalizePublicUrl(res.data.h5Url) || res.data.h5Url,
qrCodeContent: normalizePublicUrl(res.data.qrCodeContent) || res.data.qrCodeContent,
});
setQrModalOpen(true);
} else {
message.error(res.msg || '二维码加载失败');

37
src/utils/publicUrl.ts Normal file
View File

@@ -0,0 +1,37 @@
const INTERNAL_HOSTS = new Set(['127.0.0.1', 'localhost', '0.0.0.0', '::1']);
function getBrowserOrigin() {
return typeof window === 'undefined' ? '' : window.location.origin;
}
/**
* 生产环境 API 使用当前浏览器访问的 origin避免把后端内网地址编译进前端产物。
*/
export function getProductionApiBaseUrl(origin = getBrowserOrigin()) {
const normalizedOrigin = origin.replace(/\/+$/, '');
return `${normalizedOrigin || ''}/api/shihezi/`;
}
/**
* 后端可能因为反向代理配置返回内网 H5 地址,前端展示和打开二维码时使用当前公开 origin。
*/
export function normalizePublicUrl(value?: string, currentOrigin = getBrowserOrigin()) {
if (!value || !currentOrigin || !/^https?:\/\//i.test(value)) {
return value;
}
try {
const url = new URL(value);
const browserUrl = new URL(currentOrigin);
if (
INTERNAL_HOSTS.has(url.hostname.toLowerCase()) &&
!INTERNAL_HOSTS.has(browserUrl.hostname.toLowerCase())
) {
url.protocol = browserUrl.protocol;
url.host = browserUrl.host;
}
return url.toString();
} catch {
return value;
}
}