修改问题
This commit is contained in:
@@ -58,6 +58,37 @@
|
||||
return pick(item, ['regionName', 'name', 'label'], '未命名地区');
|
||||
}
|
||||
|
||||
function getRegionLevel(item) {
|
||||
var source = item || {};
|
||||
var level = Number(pick(source, ['level', 'regionLevel'], 0));
|
||||
var code = getRegionCode(source);
|
||||
|
||||
if (level >= 1 && level <= 3) return level;
|
||||
if (!/^\d{6}$/.test(code)) return 0;
|
||||
if (/0000$/.test(code)) return 1;
|
||||
if (/00$/.test(code)) return 2;
|
||||
return 3;
|
||||
}
|
||||
|
||||
function buildRegionSelection(items) {
|
||||
var selection = {
|
||||
provinceCode: '',
|
||||
cityCode: '',
|
||||
districtCode: ''
|
||||
};
|
||||
|
||||
normalizeList(items).forEach(function (item) {
|
||||
var code = getRegionCode(item);
|
||||
var level = getRegionLevel(item);
|
||||
|
||||
if (level === 1) selection.provinceCode = code;
|
||||
if (level === 2) selection.cityCode = code;
|
||||
if (level === 3) selection.districtCode = code;
|
||||
});
|
||||
|
||||
return selection;
|
||||
}
|
||||
|
||||
function buildRegionOption(item) {
|
||||
return '<option value="' + escapeHtml(getRegionCode(item)) + '">' + escapeHtml(getRegionName(item)) + '</option>';
|
||||
}
|
||||
@@ -180,6 +211,91 @@
|
||||
});
|
||||
}
|
||||
|
||||
async function applyRegionSelection(picker, selection) {
|
||||
var province = query('[data-region-level="province"]', picker);
|
||||
var city = query('[data-region-level="city"]', picker);
|
||||
var district = query('[data-region-level="district"]', picker);
|
||||
var values = selection || {};
|
||||
|
||||
if (!values.provinceCode) return;
|
||||
|
||||
province.value = values.provinceCode;
|
||||
resetSelect(city, '请选择市');
|
||||
resetSelect(district, '请选择区县');
|
||||
|
||||
if (values.cityCode) {
|
||||
await loadChildrenInto(city, values.provinceCode, '请选择市');
|
||||
city.value = values.cityCode;
|
||||
}
|
||||
|
||||
if (values.districtCode && values.cityCode) {
|
||||
await loadChildrenInto(district, values.cityCode, '请选择区县');
|
||||
district.value = values.districtCode;
|
||||
}
|
||||
|
||||
updateTargetCode(picker);
|
||||
}
|
||||
|
||||
function renderSearchResults(container, items) {
|
||||
var list = normalizeList(items);
|
||||
|
||||
if (!container) return;
|
||||
container.innerHTML = list.length ? list.map(function (item) {
|
||||
var code = getRegionCode(item);
|
||||
var name = getRegionName(item);
|
||||
|
||||
return '<button class="btn ghost magnetic" type="button" data-region-search-result="' + escapeHtml(code) + '">' + escapeHtml(name) + ' ' + escapeHtml(code) + '</button>';
|
||||
}).join('') : '<p class="api-empty">未找到匹配地区</p>';
|
||||
}
|
||||
|
||||
async function submitRegionSearch(form) {
|
||||
var api = getApi();
|
||||
var values = getFormValues(form);
|
||||
var results = query('[data-region-search-results]', form.parentNode);
|
||||
var pathText = query('[data-region-search-path]', form.parentNode);
|
||||
|
||||
if (!api) return;
|
||||
if (!values.regionKeyword || !values.regionKeyword.trim()) {
|
||||
showMessage('请输入地区名称或编码');
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathText) pathText.textContent = '搜索中...';
|
||||
try {
|
||||
renderSearchResults(results, await api.regionSearch({
|
||||
keyword: values.regionKeyword.trim(),
|
||||
limit: 20
|
||||
}));
|
||||
if (pathText) pathText.textContent = '请选择一个地区';
|
||||
} catch (error) {
|
||||
if (pathText) pathText.textContent = error.message || '地区搜索失败';
|
||||
showMessage(error.message || '地区搜索失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function selectSearchResult(button) {
|
||||
var api = getApi();
|
||||
var code = button.getAttribute('data-region-search-result');
|
||||
var section = button.closest('.module-panel');
|
||||
var picker = section && query('[data-region-picker]', section);
|
||||
var pathText = section && query('[data-region-search-path]', section);
|
||||
var detail;
|
||||
var path;
|
||||
|
||||
if (!api || !code || !picker) return;
|
||||
|
||||
if (pathText) pathText.textContent = '正在定位地区...';
|
||||
try {
|
||||
detail = await api.regionDetail(code);
|
||||
path = await api.regionPath(getRegionCode(detail) || code);
|
||||
await applyRegionSelection(picker, buildRegionSelection(path));
|
||||
if (pathText) pathText.textContent = buildPathText(path);
|
||||
} catch (error) {
|
||||
if (pathText) pathText.textContent = error.message || '地区定位失败';
|
||||
showMessage(error.message || '地区定位失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function submitProfileRegion(form) {
|
||||
var api = getApi();
|
||||
var values = getFormValues(form);
|
||||
@@ -208,6 +324,23 @@
|
||||
});
|
||||
}
|
||||
|
||||
function bindSearchForms() {
|
||||
queryAll('[data-region-search-form]').forEach(function (form) {
|
||||
form.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
submitRegionSearch(form);
|
||||
});
|
||||
});
|
||||
|
||||
documentRef.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-region-search-result]');
|
||||
|
||||
if (!button) return;
|
||||
event.preventDefault();
|
||||
selectSearchResult(button);
|
||||
});
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (!documentRef) return;
|
||||
|
||||
@@ -215,12 +348,14 @@
|
||||
initPicker(picker);
|
||||
});
|
||||
bindProfileForms();
|
||||
bindSearchForms();
|
||||
}
|
||||
|
||||
return {
|
||||
normalizeList: normalizeList,
|
||||
buildRegionOption: buildRegionOption,
|
||||
buildPathText: buildPathText,
|
||||
buildRegionSelection: buildRegionSelection,
|
||||
getFinalRegionCode: getFinalRegionCode,
|
||||
buildProfileRegionBody: buildProfileRegionBody,
|
||||
init: init
|
||||
|
||||
Reference in New Issue
Block a user