60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import axios from 'axios'
|
||
import { request as baseRequest } from '@/api'
|
||
|
||
// 抑制未使用导入告警
|
||
void baseRequest
|
||
|
||
/**
|
||
* config.json 中与 ios 相关的配置结构
|
||
*/
|
||
export type IosConfig = {
|
||
/** iOS 下载地址(跳转目标) */
|
||
upload_url?: string
|
||
}
|
||
|
||
/**
|
||
* /config.json 整体结构(按需扩展字段)
|
||
*/
|
||
export type AppConfig = {
|
||
ios?: IosConfig
|
||
// 后续若有 android / pc / 版本号等配置也可在此扩展
|
||
[key: string]: unknown
|
||
}
|
||
|
||
/**
|
||
* 静态文件请求专用的 axios 实例:
|
||
* - 不带 baseURL(绕开 src/api/index.ts 的 VITE_API_BASE_URL)
|
||
* - 不带业务 token、不触发全局 loading / 错误通知
|
||
* - 与项目里 request 拦截器彻底解耦
|
||
*/
|
||
const staticRequest = axios.create({
|
||
timeout: 10000,
|
||
headers: { 'Content-Type': 'application/json;charset=utf-8' },
|
||
})
|
||
|
||
/**
|
||
* 获取当前环境下的 config.json
|
||
*
|
||
* 关键说明:
|
||
* 1. 项目里 src/api/index.ts 的 service 默认 baseURL = VITE_API_BASE_URL(业务后端),
|
||
* 直接 request.get('/config.json') 会请求业务后端下的 /config.json,自然 404。
|
||
* 2. config.json 通常独立部署在 h5 域名(如 https://adminh5.xxx.com/config.json),
|
||
* 需要直接请求该地址。这里用 VITE_UPDATE_URL(无协议/域名兜底为 VITE_EXE_DOWNLOAD_BASE_URL)作为基础域名。
|
||
* 3. 跨域:若管理后台与 config.json 部署不同源,需要 h5 域名侧 Nginx / CDN 配置 CORS 头:
|
||
* Access-Control-Allow-Origin: <管理后台域名>
|
||
* Access-Control-Allow-Methods: GET
|
||
* 否则浏览器会拦截请求。
|
||
*
|
||
* @returns Promise<AppConfig> 配置文件内容(业务调用方按需取字段)
|
||
*/
|
||
export async function getConfigApi(): Promise<AppConfig> {
|
||
// 使用相对路径 /config.json:
|
||
// - 开发环境由 vite.config.ts 的 server.proxy 转发到 h5 域名,避免 CORS
|
||
// - 生产环境需要运维在 Nginx / 网关上做同等代理(例如把 /config.json 反代到 h5 域名)
|
||
const url = '/config.json'
|
||
const sep = url.includes('?') ? '&' : '?'
|
||
const finalUrl = `${url}${sep}t=${new Date().getTime()}&channel=web`
|
||
|
||
const res = await staticRequest.get<AppConfig>(finalUrl)
|
||
return res.data
|
||
} |