修改底层请求逻辑,封装请求方法

This commit is contained in:
rain
2026-07-10 14:11:10 +08:00
parent 6050508144
commit 6129a9221a
66 changed files with 5861 additions and 451 deletions
+21 -49
View File
@@ -1,5 +1,5 @@
(function (root, factory) {
// 全局配置同时支持浏览器页面和 Node 测试
// 根配置只负责运行环境与接口基础地址
if (typeof module === 'object' && module.exports) {
module.exports = factory;
return;
@@ -9,69 +9,41 @@
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
'use strict';
var DEFAULT_ENV = 'development';
var API_BASE_URLS = {
development: 'http://test-genealogy-api.ddxcjp.cn',
production: ''
};
var TEST_API_HTTP_URL = 'http://test-genealogy-api.ddxcjp.cn';
var TEST_API_HTTPS_URL = 'https://test-genealogy-api.ddxcjp.cn';
var CLIENT_ID = 'ced7e5f0498645c6ec642dcf450b036f';
var TENANT_ID = '000000';
var TOKEN_KEY = 'genealogy_auth_token';
var TOKEN_HEADER_NAME = 'Authorization';
var ENV_KEY = 'genealogy_env';
var BASE_URL_KEY = 'genealogy_api_base_url';
function readStorage(key) {
try {
return root.localStorage && root.localStorage.getItem(key);
} catch (error) {
return null;
var ENVIRONMENTS = {
development: {
apiBaseUrl: 'http://test-genealogy-api.ddxcjp.cn'
},
production: {
// 后端尚未提供正式生产域名,暂时保持用户指定的测试地址。
apiBaseUrl: 'http://test-genealogy-api.ddxcjp.cn'
}
}
};
function getEnv() {
return readStorage(ENV_KEY) || DEFAULT_ENV;
}
function getEnvironment() {
var location = root.location || {};
var hostname = location.hostname || '';
function normalizeApiBaseUrl(value) {
// 测试域名当前 HTTPS 握手失败,开发环境统一走可访问的 HTTP 地址。
var url = String(value || '').replace(/\/+$/, '');
if (hostname === 'localhost' || hostname === '127.0.0.1' || location.port) {
return 'development';
}
if (url === TEST_API_HTTPS_URL) return TEST_API_HTTP_URL;
return url;
return 'production';
}
function getApiBaseUrl() {
return normalizeApiBaseUrl(readStorage(BASE_URL_KEY) || API_BASE_URLS[getEnv()] || API_BASE_URLS.development);
return ENVIRONMENTS[getEnvironment()].apiBaseUrl;
}
function getConfig() {
return {
env: getEnv(),
apiBaseUrl: getApiBaseUrl(),
clientId: CLIENT_ID,
tenantId: TENANT_ID,
tokenKey: TOKEN_KEY,
tokenHeaderName: TOKEN_HEADER_NAME
environment: getEnvironment(),
apiBaseUrl: getApiBaseUrl()
};
}
return {
getConfig: getConfig,
getEnvironment: getEnvironment,
getApiBaseUrl: getApiBaseUrl,
getClientId: function () {
return CLIENT_ID;
},
getTenantId: function () {
return TENANT_ID;
},
getTokenKey: function () {
return TOKEN_KEY;
},
getTokenHeaderName: function () {
return TOKEN_HEADER_NAME;
}
getConfig: getConfig
};
});