车位区域管理完成
This commit is contained in:
182
src/utils/house.ts
Normal file
182
src/utils/house.ts
Normal file
@@ -0,0 +1,182 @@
|
||||
interface Tree {
|
||||
label: string;
|
||||
value: number;
|
||||
type: string;
|
||||
children?: Tree[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 楼栋/单元/房屋 转 Tree 结构(跳过楼层)
|
||||
* @param buildings 原始楼栋数组
|
||||
* @returns Tree[]
|
||||
*/
|
||||
export function convertBuildingToTree(buildings: any[]): Tree[] {
|
||||
if (!buildings || !Array.isArray(buildings)) return [];
|
||||
|
||||
return buildings.map((building) => {
|
||||
// 1. 楼栋
|
||||
const buildingTree: Tree = {
|
||||
label: building.buildingName || '未知楼栋',
|
||||
type: 'buildingId',
|
||||
value: building.buildingId,
|
||||
children: []
|
||||
};
|
||||
|
||||
// 2. 单元
|
||||
const units = building.units || [];
|
||||
const unitTrees = units.map((unit: any) => {
|
||||
const unitTree: Tree = {
|
||||
label: `${unit.unitNo} 单元`,
|
||||
value: unit.unitNo,
|
||||
type: 'unitNo',
|
||||
children: []
|
||||
};
|
||||
|
||||
// ====================== 关键修改 ======================
|
||||
// 直接从 floors 里把所有 house 全部抽出来,跳过 floor 层级
|
||||
const allHouses: any[] = [];
|
||||
const floors = unit.floors || [];
|
||||
|
||||
floors.forEach((floor: any) => {
|
||||
const houses = floor.houses || [];
|
||||
allHouses.push(...houses); // 把所有房屋合并到一个数组
|
||||
});
|
||||
|
||||
// 房屋直接挂在单元下
|
||||
unitTree.children = allHouses.map((house: any) => ({
|
||||
label: house.houseNo || '未知房屋',
|
||||
value: house.houseId,
|
||||
type: 'houseId'
|
||||
}));
|
||||
// ======================================================
|
||||
|
||||
return unitTree;
|
||||
});
|
||||
|
||||
buildingTree.children = unitTrees;
|
||||
console.log(buildingTree);
|
||||
return buildingTree;
|
||||
});
|
||||
}
|
||||
|
||||
// 与业主关系
|
||||
export const ownerRelationshiplist = [
|
||||
{
|
||||
label: '业主',
|
||||
value: 0
|
||||
},
|
||||
{
|
||||
label: '亲属',
|
||||
value: 1
|
||||
},
|
||||
{
|
||||
label: '租户',
|
||||
value: 2
|
||||
}
|
||||
];
|
||||
|
||||
interface Tree {
|
||||
label: string;
|
||||
value: number;
|
||||
type: string;
|
||||
children?: Tree[];
|
||||
}
|
||||
// 只允许 字符串/数字 类型的 key
|
||||
type ValidTreeKey = {
|
||||
[K in keyof Tree]: Tree[K] extends string | number ? K : never;
|
||||
}[keyof Tree];
|
||||
|
||||
/**
|
||||
* 根据 houseId 查找完整路径(楼栋 > 单元 > 楼层 > 房屋)
|
||||
* @param treeData 转换后的树形数据
|
||||
* @param houseId 目标房屋ID
|
||||
* @returns string[] 路径数组,如 ['1号楼', '1单元', '3层', '301']
|
||||
*/
|
||||
export function getHousePathById(treeData: Tree[], houseId: number, response: ValidTreeKey = 'value'): (string | number)[] {
|
||||
const path: (string | number)[] = [];
|
||||
|
||||
// 递归查找
|
||||
function findNode(nodes: Tree[]): boolean {
|
||||
for (const node of nodes) {
|
||||
path.push(node[response]); // 把当前节点加入路径
|
||||
|
||||
// 找到目标房屋
|
||||
if (node.type === 'houseId' && node.value === houseId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 递归子节点
|
||||
if (node.children && node.children.length > 0) {
|
||||
if (findNode(node.children)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
path.pop(); // 回溯,移除当前节点
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
findNode(treeData);
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证身份证号码(18位,支持尾号x/X)
|
||||
* @param idCard 身份证号
|
||||
* @return {boolean} true 符合 false 不符合
|
||||
*/
|
||||
export function validateIdCard(idCard: string): boolean {
|
||||
// 18位身份证正则
|
||||
const reg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/;
|
||||
return reg.test(idCard.trim());
|
||||
}
|
||||
|
||||
export const houseUselist = ['普通住宅', '商业用地', '商住两用', '其他'];
|
||||
|
||||
// 对字符串 图片地址,处理
|
||||
export function splitStringUrl(str: string, splitLabel: string = ',') {
|
||||
const list = str.split(splitLabel);
|
||||
return list.map((item) => {
|
||||
return {
|
||||
name: item.split('/').pop(),
|
||||
url: item
|
||||
};
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 判断 FormData 是否有数据
|
||||
* @param formData 要判断的 FormData 对象
|
||||
* @returns {boolean} true 有数据 false 没有数据
|
||||
*/
|
||||
export function hasFormData(formData: FormData): boolean {
|
||||
if (!formData || !(formData instanceof FormData)) return false;
|
||||
|
||||
// 遍历一次,只要有一个字段就返回 true
|
||||
for (const entry of formData.entries()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 方法渲燃模板数据 */
|
||||
type dataType = {
|
||||
'label': string;
|
||||
'value': string;
|
||||
'elTagType': string;
|
||||
'elTagClass': string;
|
||||
};
|
||||
/**
|
||||
* 通用:根据 value 从字典数组中获取 label
|
||||
* @param dict 字典数组
|
||||
* @param value 要匹配的值
|
||||
* @returns 匹配到的label,无则返回原value
|
||||
*/
|
||||
export function getDictLabel(dict: Ref<dataType>, value: number): string {
|
||||
// 空值直接返回
|
||||
if (value == null) return '-';
|
||||
|
||||
// 统一转字符串匹配
|
||||
const item = (dict.value || []).find((i) => String(i.value) === String(value));
|
||||
return item ? item.label : String(value);
|
||||
}
|
||||
Reference in New Issue
Block a user