Files
xingque/utils/avatar.js
T

39 lines
1.1 KiB
JavaScript

/** 统一头像 URL:无有效头像时始终返回 ossImageUrl + morentouxiang */
function isBlankPath(p) {
if (p == null) return true;
if (typeof p !== 'string') return true;
const s = p.trim();
return !s || s === 'null' || s === 'undefined' || s === 'none';
}
export function getDefaultAvatarUrl(app) {
return resolveAvatarUrl('', app);
}
export function resolveAvatarUrl(rawPath, app) {
app = app || getApp();
const oss = app.globalData.ossImageUrl || '';
const def = app.globalData.morentouxiang || 'avatar/default.jpg';
if (!isBlankPath(rawPath)) {
if (rawPath.startsWith('http://') || rawPath.startsWith('https://')) {
return rawPath;
}
const path = rawPath.startsWith('/') ? rawPath : '/' + rawPath;
return oss + path;
}
if (def.startsWith('http://') || def.startsWith('https://')) {
return def;
}
const defPath = def.startsWith('/') ? def : '/' + def;
return oss + defPath;
}
export function resolveAvatarFromStorage(app) {
app = app || getApp();
const tx = wx.getStorageSync('touxiang') || '';
return resolveAvatarUrl(tx, app);
}