等待后端更新接口
This commit is contained in:
+60
-72
@@ -33,25 +33,24 @@
|
||||
return String(displayName).charAt(0) || '家';
|
||||
}
|
||||
|
||||
function getFinalRegionCode(profile) {
|
||||
var source = profile || {};
|
||||
function getSexText(value) {
|
||||
var labels = { '0': '男', '1': '女', '2': '未知' };
|
||||
|
||||
return source.districtCode || source.cityCode || source.provinceCode || '';
|
||||
}
|
||||
|
||||
function buildRegionText(profile) {
|
||||
return getFinalRegionCode(profile) ? '加载中...' : '待填写';
|
||||
return labels[String(value)] || '待填写';
|
||||
}
|
||||
|
||||
function buildProfileView(profile) {
|
||||
// 页面展示模型和接口返回分开,后续改文案不影响接口契约。
|
||||
return {
|
||||
displayName: profileValue(profile, 'nickName', '未设置昵称'),
|
||||
realName: profileValue(profile, 'realName', '待填写'),
|
||||
avatarText: getAvatarText(profile),
|
||||
avatarStatus: profile && profile.avatar ? '已设置' : '未设置',
|
||||
phone: profileValue(profile, 'phone', '未绑定'),
|
||||
sex: profileValue(profile, 'sex', '待填写'),
|
||||
sex: getSexText(profile && profile.sex),
|
||||
birthday: profileValue(profile, 'birthday', '待填写'),
|
||||
regionText: buildRegionText(profile || {})
|
||||
email: profileValue(profile, 'email', '待填写'),
|
||||
regionText: '当前资料接口未提供'
|
||||
};
|
||||
}
|
||||
|
||||
@@ -82,6 +81,12 @@
|
||||
return body;
|
||||
}
|
||||
|
||||
function validateProfileUpdateBody(body) {
|
||||
if (!body || Object.keys(body).length === 0) return '请至少填写一项资料';
|
||||
if (body.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(body.email)) return '请输入正确的邮箱地址';
|
||||
return '';
|
||||
}
|
||||
|
||||
function getApi() {
|
||||
return root.GenealogyApi && root.GenealogyApi.defaultClient;
|
||||
}
|
||||
@@ -129,17 +134,22 @@
|
||||
}
|
||||
}
|
||||
|
||||
function setStatus(form, message) {
|
||||
function setStatus(form, type, message) {
|
||||
var statusNode = query('[data-profile-status]', form);
|
||||
|
||||
if (statusNode) {
|
||||
statusNode.textContent = message || '';
|
||||
if (!statusNode) return;
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState && type) {
|
||||
root.ProfileUI.setApiState(statusNode, type, message);
|
||||
return;
|
||||
}
|
||||
statusNode.textContent = message || '';
|
||||
}
|
||||
|
||||
function setSubmitting(form, isSubmitting) {
|
||||
queryAll('button[type="submit"]', form).forEach(function (button) {
|
||||
if (!button.dataset.defaultLabel) button.dataset.defaultLabel = button.textContent;
|
||||
button.disabled = isSubmitting;
|
||||
button.textContent = isSubmitting ? '保存中…' : button.dataset.defaultLabel;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -155,18 +165,27 @@
|
||||
});
|
||||
}
|
||||
|
||||
function setProfileLoadStatus(message) {
|
||||
function setProfileLoadStatus(type, message) {
|
||||
var statusNode = query('[data-profile-load-status]');
|
||||
|
||||
if (statusNode) statusNode.textContent = message || '';
|
||||
if (!statusNode) return;
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState && type) {
|
||||
root.ProfileUI.setApiState(statusNode, type, message);
|
||||
return;
|
||||
}
|
||||
statusNode.textContent = message || '';
|
||||
}
|
||||
|
||||
function buildUnavailableProfileView() {
|
||||
return {
|
||||
displayName: '账户资料暂时无法读取',
|
||||
realName: '暂无数据',
|
||||
avatarText: '!',
|
||||
avatarStatus: '暂无数据',
|
||||
phone: '暂无数据',
|
||||
birthday: '暂无数据',
|
||||
email: '暂无数据',
|
||||
sex: '暂无数据',
|
||||
regionText: '暂无数据'
|
||||
};
|
||||
}
|
||||
@@ -194,53 +213,6 @@
|
||||
});
|
||||
}
|
||||
|
||||
function buildRegionPathText(items) {
|
||||
return (Array.isArray(items) ? items : []).map(function (item) {
|
||||
return item && item.regionName || '';
|
||||
}).filter(Boolean).join(' / ');
|
||||
}
|
||||
|
||||
async function renderRegionText(profile, requestId) {
|
||||
var api = getApi();
|
||||
var regionCode = getFinalRegionCode(profile);
|
||||
var path;
|
||||
var text = '待填写';
|
||||
|
||||
if (!regionCode || !api || !api.regionPath) {
|
||||
renderProfile({ regionText: text });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
path = await api.regionPath(regionCode);
|
||||
text = buildRegionPathText(path) || text;
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
}
|
||||
|
||||
if (requestId === undefined || requestId === latestProfileRequestId) {
|
||||
renderProfile({ regionText: text });
|
||||
}
|
||||
}
|
||||
|
||||
async function fillRegionPicker(profile) {
|
||||
var regionPages = root.RegionPages;
|
||||
|
||||
if (!regionPages || !regionPages.applyProfileRegionSelection) return;
|
||||
|
||||
try {
|
||||
await regionPages.applyProfileRegionSelection({
|
||||
provinceCode: trimOrUndefined(profile && profile.provinceCode),
|
||||
cityCode: trimOrUndefined(profile && profile.cityCode),
|
||||
districtCode: trimOrUndefined(profile && profile.districtCode)
|
||||
});
|
||||
} catch (error) {
|
||||
var api = getApi();
|
||||
|
||||
if (!redirectUnauthorized(api, error)) showMessage(error.message || '地区资料加载失败');
|
||||
}
|
||||
}
|
||||
|
||||
var latestProfileRequestId = 0;
|
||||
|
||||
async function loadProfile() {
|
||||
@@ -252,40 +224,52 @@
|
||||
|
||||
if (!api || !documentRef || !documentRef.querySelector('[data-profile-page]')) return;
|
||||
if (redirectUnauthorized(api)) return;
|
||||
setProfileLoadStatus('正在读取账户资料…');
|
||||
setProfileLoadStatus('loading', '正在读取账户资料…');
|
||||
|
||||
try {
|
||||
profile = await api.currentProfile();
|
||||
if (requestId !== latestProfileRequestId) return;
|
||||
renderProfile(buildProfileView(profile));
|
||||
fillProfileForm(profile);
|
||||
fillRegionPicker(profile);
|
||||
renderRegionText(profile, requestId);
|
||||
setProfileLoadStatus('');
|
||||
setProfileLoadStatus('', '');
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
renderProfile(buildUnavailableProfileView());
|
||||
setProfileLoadStatus('资料读取失败,请刷新页面后重试。');
|
||||
setProfileLoadStatus('error', '资料读取失败,请刷新页面后重试。');
|
||||
showMessage(error.message || '个人资料加载失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function submitProfileForm(form) {
|
||||
var api = getApi();
|
||||
var body;
|
||||
var validation;
|
||||
|
||||
if (!api || !form) return;
|
||||
if (redirectUnauthorized(api)) return;
|
||||
|
||||
try {
|
||||
body = buildProfileUpdateBody(getFormValues(form));
|
||||
} catch (error) {
|
||||
setStatus(form, 'error', error.message || '资料校验失败');
|
||||
return;
|
||||
}
|
||||
validation = validateProfileUpdateBody(body);
|
||||
if (validation) {
|
||||
setStatus(form, 'error', validation);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setSubmitting(form, true);
|
||||
setStatus(form, '保存中...');
|
||||
await api.updateProfile(buildProfileUpdateBody(getFormValues(form)));
|
||||
setStatus(form, '已保存');
|
||||
setStatus(form, 'loading', '正在保存资料…');
|
||||
await api.updateProfile(body);
|
||||
setStatus(form, '', '已保存');
|
||||
showMessage('个人资料已保存');
|
||||
await loadProfile();
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
setStatus(form, '保存失败');
|
||||
setStatus(form, 'error', '保存失败,请稍后重试。');
|
||||
showMessage(error.message || '个人资料保存失败');
|
||||
} finally {
|
||||
setSubmitting(form, false);
|
||||
@@ -299,6 +283,11 @@
|
||||
|
||||
form.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
if (!form.checkValidity()) {
|
||||
setStatus(form, 'error', '请检查资料填写是否正确');
|
||||
form.reportValidity();
|
||||
return;
|
||||
}
|
||||
submitProfileForm(form);
|
||||
});
|
||||
}
|
||||
@@ -310,11 +299,10 @@
|
||||
|
||||
return {
|
||||
getAvatarText: getAvatarText,
|
||||
getFinalRegionCode: getFinalRegionCode,
|
||||
buildRegionText: buildRegionText,
|
||||
buildProfileView: buildProfileView,
|
||||
buildUnavailableProfileView: buildUnavailableProfileView,
|
||||
buildProfileUpdateBody: buildProfileUpdateBody,
|
||||
validateProfileUpdateBody: validateProfileUpdateBody,
|
||||
shouldRedirectToLogin: shouldRedirectToLogin,
|
||||
reloadProfile: loadProfile,
|
||||
init: init
|
||||
|
||||
Reference in New Issue
Block a user