家谱现有接口调试50%
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
(function (root, factory) {
|
||||
// 个人资料模块同时支持浏览器页面和 Node 测试。
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
module.exports = factory(root);
|
||||
return;
|
||||
}
|
||||
|
||||
root.ProfilePages = factory(root);
|
||||
if (root.document) {
|
||||
root.document.addEventListener('DOMContentLoaded', function () {
|
||||
root.ProfilePages.init();
|
||||
});
|
||||
}
|
||||
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
||||
'use strict';
|
||||
|
||||
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;
|
||||
|
||||
if (!text) return undefined;
|
||||
|
||||
numberValue = Number(text);
|
||||
return Number.isNaN(numberValue) ? undefined : numberValue;
|
||||
}
|
||||
|
||||
function getAvatarText(profile) {
|
||||
// 头像字只取展示名第一个字符,避免接口没有头像时页面空白。
|
||||
var displayName = pick(profile, ['nickName', 'userName', 'phone'], '家');
|
||||
return String(displayName).charAt(0) || '家';
|
||||
}
|
||||
|
||||
function buildRegionText(profile) {
|
||||
var parts = [
|
||||
profile && profile.provinceCode,
|
||||
profile && profile.cityCode,
|
||||
profile && profile.districtCode
|
||||
].filter(Boolean);
|
||||
|
||||
return parts.length ? parts.join(' / ') : '待填写';
|
||||
}
|
||||
|
||||
function buildProfileView(profile) {
|
||||
// 页面展示模型和接口返回分开,后续改文案不影响接口契约。
|
||||
return {
|
||||
displayName: pick(profile, ['nickName', 'userName', 'phone'], '未设置昵称'),
|
||||
avatarText: getAvatarText(profile),
|
||||
phone: pick(profile, ['phone', 'phonenumber', 'mobile'], '未绑定'),
|
||||
sex: pick(profile, ['sex'], '待填写'),
|
||||
birthday: pick(profile, ['birthday'], '待填写'),
|
||||
regionText: buildRegionText(profile || {})
|
||||
};
|
||||
}
|
||||
|
||||
function buildProfileUpdateBody(values) {
|
||||
// 字段名称严格对应 PC YAML 里的 ProfileUpdateBody。
|
||||
var source = values || {};
|
||||
|
||||
return {
|
||||
nickName: trimOrUndefined(source.nickName),
|
||||
avatarOssId: toNumberOrUndefined(source.avatarOssId),
|
||||
sex: trimOrUndefined(source.sex),
|
||||
birthday: trimOrUndefined(source.birthday),
|
||||
provinceCode: trimOrUndefined(source.provinceCode),
|
||||
cityCode: trimOrUndefined(source.cityCode),
|
||||
districtCode: trimOrUndefined(source.districtCode)
|
||||
};
|
||||
}
|
||||
|
||||
function getApi() {
|
||||
return root.GenealogyApi && root.GenealogyApi.defaultClient;
|
||||
}
|
||||
|
||||
function query(selector, rootNode) {
|
||||
if (!documentRef) return null;
|
||||
return (rootNode || documentRef).querySelector(selector);
|
||||
}
|
||||
|
||||
function queryAll(selector, rootNode) {
|
||||
if (!documentRef) return [];
|
||||
return Array.prototype.slice.call((rootNode || documentRef).querySelectorAll(selector));
|
||||
}
|
||||
|
||||
function showMessage(message) {
|
||||
if (root.layui && root.layui.layer) {
|
||||
root.layui.layer.msg(message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (root.alert) {
|
||||
root.alert(message);
|
||||
}
|
||||
}
|
||||
|
||||
function setStatus(form, message) {
|
||||
var statusNode = query('[data-profile-status]', form);
|
||||
|
||||
if (statusNode) {
|
||||
statusNode.textContent = message || '';
|
||||
}
|
||||
}
|
||||
|
||||
function setSubmitting(form, isSubmitting) {
|
||||
queryAll('button[type="submit"]', form).forEach(function (button) {
|
||||
button.disabled = isSubmitting;
|
||||
});
|
||||
}
|
||||
|
||||
function renderProfile(view) {
|
||||
// 所有资料展示点用 data-profile-text 标记,避免按中文文案查找节点。
|
||||
queryAll('[data-profile-text]').forEach(function (node) {
|
||||
var key = node.getAttribute('data-profile-text');
|
||||
var value = view[key];
|
||||
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
node.textContent = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getFormValues(form) {
|
||||
var values = {};
|
||||
|
||||
queryAll('[name]', form).forEach(function (field) {
|
||||
values[field.name] = field.value;
|
||||
});
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
function fillProfileForm(profile) {
|
||||
var form = query('[data-profile-form]');
|
||||
var source = profile || {};
|
||||
|
||||
if (!form) return;
|
||||
|
||||
queryAll('[name]', form).forEach(function (field) {
|
||||
if (source[field.name] !== undefined && source[field.name] !== null) {
|
||||
field.value = source[field.name];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function loadProfile() {
|
||||
var api = getApi();
|
||||
var profile;
|
||||
|
||||
if (!api || !documentRef || !documentRef.querySelector('[data-profile-page]')) return;
|
||||
|
||||
try {
|
||||
profile = await api.currentProfile();
|
||||
renderProfile(buildProfileView(profile));
|
||||
fillProfileForm(profile);
|
||||
} catch (error) {
|
||||
showMessage(error.message || '个人资料加载失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function submitProfileForm(form) {
|
||||
var api = getApi();
|
||||
|
||||
if (!api || !form) return;
|
||||
|
||||
try {
|
||||
setSubmitting(form, true);
|
||||
setStatus(form, '保存中...');
|
||||
await api.updateProfile(buildProfileUpdateBody(getFormValues(form)));
|
||||
setStatus(form, '已保存');
|
||||
showMessage('个人资料已保存');
|
||||
await loadProfile();
|
||||
} catch (error) {
|
||||
setStatus(form, '保存失败');
|
||||
showMessage(error.message || '个人资料保存失败');
|
||||
} finally {
|
||||
setSubmitting(form, false);
|
||||
}
|
||||
}
|
||||
|
||||
function bindProfileForm() {
|
||||
var form = query('[data-profile-form]');
|
||||
|
||||
if (!form) return;
|
||||
|
||||
form.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
submitProfileForm(form);
|
||||
});
|
||||
}
|
||||
|
||||
function init() {
|
||||
bindProfileForm();
|
||||
loadProfile();
|
||||
}
|
||||
|
||||
return {
|
||||
pick: pick,
|
||||
getAvatarText: getAvatarText,
|
||||
buildProfileView: buildProfileView,
|
||||
buildProfileUpdateBody: buildProfileUpdateBody,
|
||||
init: init
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user