27 lines
1.0 KiB
JavaScript
27 lines
1.0 KiB
JavaScript
/**
|
||
* H5 本地开发:将 OSS Post 地址换为同源 /dev-oss-proxy,由 vite.config.js 中中间件转发到 OSS 桶根 POST /,
|
||
* 避免浏览器从 localhost / 局域网端口直连 *.aliyuncs.com 触发 CORS。
|
||
*
|
||
* 正式 H5 部署到业务域名时仍直连 ossData.host;若遇 CORS,请在 OSS 控制台配置 CORS 或使用与站点同域的反代。
|
||
*
|
||
* @param {string} ossHost 签名接口返回的 host,如 https://bucket.oss-cn-shanghai.aliyuncs.com
|
||
* @returns {string} 实际用于 fetch POST 的 URL(开发态为同源代理前缀)
|
||
*/
|
||
export function resolveH5OssPostUrl(ossHost) {
|
||
if (!ossHost || typeof location === 'undefined') {
|
||
return ossHost
|
||
}
|
||
const port = String(location.port || '')
|
||
const hn = location.hostname || ''
|
||
const devPortOk = ['5173', '5174', '8080', '9528'].includes(port)
|
||
const devHostOk =
|
||
hn === 'localhost' ||
|
||
hn === '127.0.0.1' ||
|
||
/^192\.168\./.test(hn) ||
|
||
/^10\./.test(hn)
|
||
if (devPortOk && devHostOk) {
|
||
return `${location.protocol}//${location.host}/dev-oss-proxy`
|
||
}
|
||
return ossHost
|
||
}
|