xiangyixiangqin/miniapp/utils/image.js
2026-01-06 22:08:53 +08:00

36 lines
800 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
*/
import config from '../config/index'
// 从统一配置获取静态资源服务器地址
const IMAGE_BASE_URL = config.STATIC_BASE_URL
/**
* 获取完整的图片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
}