家谱现有接口调试50%
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
(function (root, factory) {
|
||||
// 行政区划模块同时支持浏览器页面和 Node 单元测试。
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
module.exports = factory(root);
|
||||
return;
|
||||
}
|
||||
|
||||
root.RegionPages = factory(root);
|
||||
if (root.document) {
|
||||
root.document.addEventListener('DOMContentLoaded', function () {
|
||||
root.RegionPages.init();
|
||||
});
|
||||
}
|
||||
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
||||
'use strict';
|
||||
|
||||
var documentRef = root.document;
|
||||
|
||||
function normalizeList(data) {
|
||||
// 行政区划接口使用通用 ListResult,兼容常见数组包裹字段。
|
||||
if (Array.isArray(data)) return data;
|
||||
if (!data || typeof data !== 'object') return [];
|
||||
if (Array.isArray(data.rows)) return data.rows;
|
||||
if (Array.isArray(data.records)) return data.records;
|
||||
if (Array.isArray(data.list)) return data.list;
|
||||
if (Array.isArray(data.data)) return data.data;
|
||||
return [];
|
||||
}
|
||||
|
||||
function pick(item, keys, fallback) {
|
||||
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 escapeHtml(value) {
|
||||
// 地区名称和编码进入 option 前统一转义。
|
||||
return String(value === undefined || value === null ? '' : value)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function getRegionCode(item) {
|
||||
return pick(item, ['regionCode', 'code', 'value'], '');
|
||||
}
|
||||
|
||||
function getRegionName(item) {
|
||||
return pick(item, ['regionName', 'name', 'label'], '未命名地区');
|
||||
}
|
||||
|
||||
function buildRegionOption(item) {
|
||||
return '<option value="' + escapeHtml(getRegionCode(item)) + '">' + escapeHtml(getRegionName(item)) + '</option>';
|
||||
}
|
||||
|
||||
function buildPathText(items) {
|
||||
return normalizeList(items).map(function (item) {
|
||||
return getRegionName(item);
|
||||
}).filter(Boolean).join(' / ');
|
||||
}
|
||||
|
||||
function getFinalRegionCode(values) {
|
||||
var source = values || {};
|
||||
|
||||
return source.districtCode || source.cityCode || source.provinceCode || '';
|
||||
}
|
||||
|
||||
function trimOrUndefined(value) {
|
||||
var text = String(value === undefined || value === null ? '' : value).trim();
|
||||
|
||||
return text || undefined;
|
||||
}
|
||||
|
||||
function buildProfileRegionBody(values) {
|
||||
var source = values || {};
|
||||
|
||||
return {
|
||||
provinceCode: trimOrUndefined(source.provinceCode),
|
||||
cityCode: trimOrUndefined(source.cityCode),
|
||||
districtCode: trimOrUndefined(source.districtCode)
|
||||
};
|
||||
}
|
||||
|
||||
function getApi() {
|
||||
return root.GenealogyApi && root.GenealogyApi.defaultClient;
|
||||
}
|
||||
|
||||
function query(selector, rootNode) {
|
||||
return (rootNode || documentRef).querySelector(selector);
|
||||
}
|
||||
|
||||
function queryAll(selector, rootNode) {
|
||||
return Array.prototype.slice.call((rootNode || documentRef).querySelectorAll(selector));
|
||||
}
|
||||
|
||||
function showMessage(message) {
|
||||
if (root.layui && root.layui.layer) {
|
||||
root.layui.layer.msg(message);
|
||||
return;
|
||||
}
|
||||
|
||||
root.alert(message);
|
||||
}
|
||||
|
||||
function getFormValues(form) {
|
||||
var values = {};
|
||||
|
||||
queryAll('[name]', form).forEach(function (field) {
|
||||
values[field.name] = field.value;
|
||||
});
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
function setOptions(select, items, placeholder) {
|
||||
var list = normalizeList(items);
|
||||
|
||||
if (!select) return;
|
||||
select.innerHTML = '<option value="">' + escapeHtml(placeholder) + '</option>' + list.map(buildRegionOption).join('');
|
||||
select.disabled = false;
|
||||
}
|
||||
|
||||
function resetSelect(select, placeholder) {
|
||||
if (!select) return;
|
||||
select.innerHTML = '<option value="">' + escapeHtml(placeholder) + '</option>';
|
||||
select.disabled = true;
|
||||
}
|
||||
|
||||
function updateTargetCode(picker) {
|
||||
var values = {
|
||||
provinceCode: query('[data-region-level="province"]', picker).value,
|
||||
cityCode: query('[data-region-level="city"]', picker).value,
|
||||
districtCode: query('[data-region-level="district"]', picker).value
|
||||
};
|
||||
var target = query('[data-region-code]', picker);
|
||||
|
||||
if (target) target.value = getFinalRegionCode(values);
|
||||
}
|
||||
|
||||
async function loadChildrenInto(select, parentCode, placeholder) {
|
||||
var api = getApi();
|
||||
|
||||
if (!api || !select) return;
|
||||
setOptions(select, await api.regionChildren(parentCode || '0'), placeholder);
|
||||
}
|
||||
|
||||
async function initPicker(picker) {
|
||||
var province = query('[data-region-level="province"]', picker);
|
||||
var city = query('[data-region-level="city"]', picker);
|
||||
var district = query('[data-region-level="district"]', picker);
|
||||
|
||||
resetSelect(city, '请选择市');
|
||||
resetSelect(district, '请选择区县');
|
||||
await loadChildrenInto(province, '0', '请选择省');
|
||||
|
||||
province.addEventListener('change', async function () {
|
||||
resetSelect(city, '请选择市');
|
||||
resetSelect(district, '请选择区县');
|
||||
updateTargetCode(picker);
|
||||
if (province.value) await loadChildrenInto(city, province.value, '请选择市');
|
||||
});
|
||||
|
||||
city.addEventListener('change', async function () {
|
||||
resetSelect(district, '请选择区县');
|
||||
updateTargetCode(picker);
|
||||
if (city.value) await loadChildrenInto(district, city.value, '请选择区县');
|
||||
});
|
||||
|
||||
district.addEventListener('change', function () {
|
||||
updateTargetCode(picker);
|
||||
});
|
||||
}
|
||||
|
||||
async function submitProfileRegion(form) {
|
||||
var api = getApi();
|
||||
var values = getFormValues(form);
|
||||
var body = buildProfileRegionBody(values);
|
||||
|
||||
if (!api) return;
|
||||
if (!body.provinceCode) {
|
||||
showMessage('请选择省份');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await api.updateProfile(body);
|
||||
showMessage('地区资料已保存');
|
||||
} catch (error) {
|
||||
showMessage(error.message || '地区资料保存失败');
|
||||
}
|
||||
}
|
||||
|
||||
function bindProfileForms() {
|
||||
queryAll('[data-region-profile-form]').forEach(function (form) {
|
||||
form.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
submitProfileRegion(form);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (!documentRef) return;
|
||||
|
||||
queryAll('[data-region-picker]').forEach(function (picker) {
|
||||
initPicker(picker);
|
||||
});
|
||||
bindProfileForms();
|
||||
}
|
||||
|
||||
return {
|
||||
normalizeList: normalizeList,
|
||||
buildRegionOption: buildRegionOption,
|
||||
buildPathText: buildPathText,
|
||||
getFinalRegionCode: getFinalRegionCode,
|
||||
buildProfileRegionBody: buildProfileRegionBody,
|
||||
init: init
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user