feat(user-center): deepen zibll parity and profile media
This commit is contained in:
1 parent
c13f686cdc
commit
e47ee1b029
16 files changed
+479
-65
No files matched your search
@@ -199,9 +199,69 @@
|
||||
});
|
||||
}
|
||||
|
||||
function initAvatarUpload() {
|
||||
var upload = (window.aksrtUserCenter || {}).avatarUpload;
|
||||
if (!upload || !upload.enabled || !upload.restUrl) return;
|
||||
|
||||
var button = document.getElementById(upload.buttonId || 'aksrt-upload-avatar-btn');
|
||||
var input = document.getElementById(upload.inputId || 'aksrt-avatar-file-input');
|
||||
var image = document.getElementById(upload.imageId || 'aksrt-user-avatar-image');
|
||||
if (!button || !input || button.dataset.aksrtAvatarBound === '1') return;
|
||||
button.dataset.aksrtAvatarBound = '1';
|
||||
|
||||
var label = button.querySelector('span');
|
||||
var idleText = upload.idleText || (label ? label.textContent : '更换头像');
|
||||
|
||||
function setBusy(busy) {
|
||||
button.disabled = !!busy;
|
||||
if (label) label.textContent = busy ? (upload.loadingText || '上传中…') : idleText;
|
||||
}
|
||||
|
||||
button.addEventListener('click', function () { input.click(); });
|
||||
input.addEventListener('change', function () {
|
||||
var file = input.files && input.files[0];
|
||||
if (!file) return;
|
||||
if (upload.maxSize && file.size > upload.maxSize) {
|
||||
notify(upload.tooLargeText || '图片大小不能超过 2MB', 'warning');
|
||||
input.value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
var body = new FormData();
|
||||
body.append(upload.fieldName || 'avatar', file);
|
||||
setBusy(true);
|
||||
fetch(upload.restUrl, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: { 'X-WP-Nonce': upload.restNonce || '' },
|
||||
body: body
|
||||
})
|
||||
.then(function (response) {
|
||||
return response.json().then(function (payload) {
|
||||
if (!response.ok || !payload || !payload.success) throw payload || {};
|
||||
return payload;
|
||||
});
|
||||
})
|
||||
.then(function (payload) {
|
||||
var url = payload.url || (payload.data && payload.data.url) || '';
|
||||
if (!url) throw payload;
|
||||
if (image) image.src = url;
|
||||
notify(payload.message || upload.successText || '头像已更新', 'success');
|
||||
})
|
||||
.catch(function (error) {
|
||||
notify((error && error.message) || upload.errorText || '上传失败', 'error');
|
||||
})
|
||||
.finally(function () {
|
||||
setBusy(false);
|
||||
input.value = '';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function init() {
|
||||
initPublicDashboards();
|
||||
initCoverUpload();
|
||||
initAvatarUpload();
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
|
||||
Reference in new issue
Block a user