第一次提交:微信小程序前端最新完整代码
This commit is contained in:
38
utils/avatar.js
Normal file
38
utils/avatar.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/** 统一头像 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);
|
||||
}
|
||||
Reference in New Issue
Block a user