topfans/frontend/utils/h5OssPostUrl.js

27 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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
}