ryui-vue
Version:
ry公共组件库
176 lines (164 loc) • 4.15 kB
JavaScript
export default class AreaUtil {
/**
* 区域Map对象
* [区域长度 - 区域等级]
*/
[ ],
[ ],
[ ],
[ ],
[ ]
]);
constructor(
{
getSettings,
getTree,
getSubByCode,
getWholeByCode
}) {
this.
this.
this.
this.
}
/**
* 获取区域prop配置
* @return {Object} prop配置对象
*/
getAreaProp() {
return {
lazy: true,
checkStrictly: true,
lazyLoad: async (node, resolve) => {
const {level, value} = node;
if (level === 0) {
// 请求第一级
const {data} = await this.
const {data: districts} = await this.
startLevel: data.rootAreaLevelId,
leverCount: 1
});
resolve(districts);
} else {
// 请求其他级别
const {data} = await this.
const districts = data.map((item) => ({
id: item.id,
value: item.code,
label: item.name,
level: item.level
}));
resolve(districts);
}
}
};
}
/**
* 按照区域编码反推所有地区编码 - 前端自行获取
* @param {String} code 区域编码
* @return {Array} 从第一级到最后一级的区域编码数组
* @example '330502001005'->['33', '3305', '330502', '330502001', '330502001005']
*/
getWholeAreaCodes = (code) => {
if (!code) return [];
let areaCodes = [];
let level = this.
let lastAreaCode;
while (level >= 1) {
for (const area of this.
if (area[1] === level) {
let lastAreaLength = area[0];
lastAreaCode = code.slice(0, lastAreaLength);
areaCodes.unshift(lastAreaCode);
level--;
}
}
}
return areaCodes;
};
/**
* 根据区域编码获取所有地区编码 - 查接口获取
* @param {String} code 区域编码
* @return {Array} 从第一级到最后一级的区域编码数组
* @example '330502001005'->['33', '3305', '330502', '330502001', '330502001005']
*/
getWholeAreaCodesByApi = async (code) => {
const {
data: {wholeParents}
} = await this.getWholeByCode(code);
return wholeParents?.reverse().map((v) => {
return v.code;
});
};
}
// import {
// getSettings,
// getTree,
// getSubByCode,
// getWholeByCode
// } from '@/api/districts';
/*
let level = 5;
const areaProp = {
lazy: true,
checkStrictly: true,
lazyLoad: async (node, resolve) => {
const {level, value} = node;
if (level === 0) {
// 请求第一级
const {data} = await getSettings();
const {data: districts} = await getTree({
startLevel: data.rootAreaLevelId,
leverCount: 1
});
resolve(districts);
} else {
// 请求其他级别
const {data} = await getSubByCode({code: value});
const districts = data.map((item) => ({
id: item.id,
value: item.code,
label: item.name,
level: item.level
}));
resolve(districts);
}
}
};
const areaPropNew = {
lazy: true,
checkStrictly: true,
lazyLoad: async (node, resolve) => {
const {level, value} = node;
if (level === 0) {
// 请求第一级
const {data} = await getSettings();
const {data: districts} = await getTree({
startLevel: data.rootAreaLevelId,
leverCount: 1
});
resolve(districts);
} else {
// 请求其他级别
const {data} = await getSubByCode({code: value});
if (data.length && data[0].level > 3) {
resolve([]);
return;
}
const districts = data.map((item) => ({
id: item.id,
value: item.code,
label: item.name,
level: item.level
}));
resolve(districts);
}
}
};
*/