完成10%

This commit is contained in:
rain
2026-07-24 18:11:16 +08:00
parent 8716a68fdb
commit 8870136d1b
33 changed files with 3853 additions and 336 deletions
+82 -42
View File
@@ -16,59 +16,41 @@
var documentRef = root.document;
function pick(item, keys, fallback) {
// 后端资料字段可能有 nickName、userName、phone 等不同名称,按优先级取值。
var source = item || {};
var index;
var value;
for (index = 0; index < keys.length; index += 1) {
value = source[keys[index]];
if (value !== undefined && value !== null && value !== '') return value;
}
return fallback || '';
}
function trimOrUndefined(value) {
var text = value === undefined || value === null ? '' : String(value).trim();
return text || undefined;
}
function toNumberOrUndefined(value) {
var text = trimOrUndefined(value);
var numberValue;
function profileValue(profile, key, fallback) {
var value = profile && profile[key];
if (!text) return undefined;
numberValue = Number(text);
return Number.isNaN(numberValue) ? undefined : numberValue;
return value === undefined || value === null || value === '' ? fallback : value;
}
function getAvatarText(profile) {
// 头像字只取展示名第一个字符,避免接口没有头像时页面空白。
var displayName = pick(profile, ['nickName', 'userName', 'phone'], '家');
var displayName = profileValue(profile, 'nickName', '家');
return String(displayName).charAt(0) || '家';
}
function buildRegionText(profile) {
var parts = [
profile && profile.provinceCode,
profile && profile.cityCode,
profile && profile.districtCode
].filter(Boolean);
function getFinalRegionCode(profile) {
var source = profile || {};
return parts.length ? parts.join(' / ') : '待填写';
return source.districtCode || source.cityCode || source.provinceCode || '';
}
function buildRegionText(profile) {
return getFinalRegionCode(profile) ? '加载中...' : '待填写';
}
function buildProfileView(profile) {
// 页面展示模型和接口返回分开,后续改文案不影响接口契约。
return {
displayName: pick(profile, ['nickName', 'userName', 'phone'], '未设置昵称'),
displayName: profileValue(profile, 'nickName', '未设置昵称'),
avatarText: getAvatarText(profile),
phone: pick(profile, ['phone', 'phonenumber', 'mobile'], '未绑定'),
sex: pick(profile, ['sex'], '待填写'),
birthday: pick(profile, ['birthday'], '待填写'),
phone: profileValue(profile, 'phone', '未绑定'),
sex: profileValue(profile, 'sex', '待填写'),
birthday: profileValue(profile, 'birthday', '待填写'),
regionText: buildRegionText(profile || {})
};
}
@@ -79,7 +61,8 @@
return {
nickName: trimOrUndefined(source.nickName),
avatarOssId: toNumberOrUndefined(source.avatarOssId),
// int64 在浏览器中必须保留字符串,避免 Number 转换丢失精度。
avatarOssId: trimOrUndefined(source.avatarOssId),
sex: trimOrUndefined(source.sex),
birthday: trimOrUndefined(source.birthday),
provinceCode: trimOrUndefined(source.provinceCode),
@@ -92,25 +75,25 @@
return root.GenealogyApi && root.GenealogyApi.defaultClient;
}
function shouldRedirectToHome(api, error) {
function shouldRedirectToLogin(api, error) {
var status = error && (error.status || error.code);
return !api || !api.getToken || !api.getToken() || Number(status) === 401;
}
function redirectToHome() {
function redirectToLogin() {
if (!root.location) return;
if (typeof root.location.replace === 'function') {
root.location.replace('index.html');
root.location.replace('login.html');
return;
}
root.location.href = 'index.html';
root.location.href = 'login.html';
}
function redirectUnauthorized(api, error) {
if (!shouldRedirectToHome(api, error)) return false;
if (!shouldRedirectToLogin(api, error)) return false;
if (api && api.clearToken) api.clearToken();
redirectToHome();
redirectToLogin();
return true;
}
@@ -184,17 +167,72 @@
});
}
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() {
var api = getApi();
var profile;
var requestId = latestProfileRequestId + 1;
latestProfileRequestId = requestId;
if (!api || !documentRef || !documentRef.querySelector('[data-profile-page]')) return;
if (redirectUnauthorized(api)) return;
try {
profile = await api.currentProfile();
if (requestId !== latestProfileRequestId) return;
renderProfile(buildProfileView(profile));
fillProfileForm(profile);
fillRegionPicker(profile);
renderRegionText(profile, requestId);
} catch (error) {
if (redirectUnauthorized(api, error)) return;
showMessage(error.message || '个人资料加载失败');
@@ -240,11 +278,13 @@
}
return {
pick: pick,
getAvatarText: getAvatarText,
getFinalRegionCode: getFinalRegionCode,
buildRegionText: buildRegionText,
buildProfileView: buildProfileView,
buildProfileUpdateBody: buildProfileUpdateBody,
shouldRedirectToHome: shouldRedirectToHome,
shouldRedirectToLogin: shouldRedirectToLogin,
reloadProfile: loadProfile,
init: init
};
});