xiangyixiangqin/miniapp/utils/image.js
2026-01-06 19:00:44 +08:00

34 lines
818 B
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.

/**
* 图片URL处理工具
* 将相对路径转换为完整的后端URL
*/
// 后端服务器基础地址(图片资源 - 使用AppApi因为图片存储在AppApi的wwwroot下
const IMAGE_BASE_URL = 'http://localhost:5000'
/**
* 获取完整的图片URL
* @param {string} url 图片路径可能是相对路径或完整URL
* @returns {string} 完整的图片URL
*/
export function getFullImageUrl(url) {
if (!url) return ''
// 如果已经是完整URL直接返回
if (url.startsWith('http://') || url.startsWith('https://')) {
return url
}
// 如果是相对路径拼接基础URL
if (url.startsWith('/')) {
return `${IMAGE_BASE_URL}${url}`
}
// 其他情况,添加斜杠后拼接
return `${IMAGE_BASE_URL}/${url}`
}
export default {
getFullImageUrl
}