Files
Wechat/utils/avatar.js

39 lines
1.1 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.
/** 统一头像 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);
}