33 lines
1.4 KiB
JavaScript
33 lines
1.4 KiB
JavaScript
import { resolveApiBase } from '@/config/api'
|
||
import { DEFAULT_PLACEHOLDER_IMAGE_URL } from '@/utils/staticAsset'
|
||
import { withOssVersion } from '@/utils/ossCache'
|
||
|
||
/** 未设置头像时统一使用叁强 logo(远程静态 images/logo.png) */
|
||
export const DEFAULT_USER_AVATAR_URL = DEFAULT_PLACEHOLDER_IMAGE_URL
|
||
|
||
/** 各角色默认已统一为 logo,保留参数仅为兼容旧调用 */
|
||
export function defaultAvatarPathByRole(_role) {
|
||
return DEFAULT_USER_AVATAR_URL
|
||
}
|
||
|
||
/**
|
||
* 将后端返回的头像地址转为小程序 `<image>` 可加载的绝对 URL。
|
||
* - 已是 http(s):// 或 // 则原样返回
|
||
* - 以 / 开头的站内路径(如 /uploads/...)拼接到 API 域名根(与静态资源同源)
|
||
*/
|
||
export function normalizeAvatarUrlForMini(avatarUrl) {
|
||
const u = String(avatarUrl || '').trim()
|
||
if (!u) return ''
|
||
if (/^https?:\/\//i.test(u) || u.startsWith('//')) return withOssVersion(u)
|
||
const origin = resolveApiBase().replace(/\/$/, '')
|
||
const path = u.startsWith('/') ? u : `/${u}`
|
||
return withOssVersion(`${origin}${path}`)
|
||
}
|
||
|
||
/** 展示用头像:有自定义 URL 则转为可访问地址,否则用叁强 logo(本地路径) */
|
||
export function resolveUserAvatarUrl(avatarUrl, role) {
|
||
const raw = avatarUrl != null ? String(avatarUrl).trim() : ''
|
||
if (raw) return normalizeAvatarUrlForMini(raw)
|
||
return defaultAvatarPathByRole(role)
|
||
}
|