| | |
| | | |
| | | setLoading(true); |
| | | try { |
| | | const res = await request.post(`/warehouseAreas/many/${record.areas.join(',')}`); |
| | | // 提取排序信息和ID |
| | | // Old format: [1, 2, 3] (array of integers) |
| | | // New format: [{id: 1, sort: 1}, {id: 2, sort: 2}] (array of objects) |
| | | const isObjectArray = record.areas.length > 0 && |
| | | typeof record.areas[0] === 'object' && |
| | | record.areas[0] !== null && |
| | | 'id' in record.areas[0]; |
| | | |
| | | let areaIds = []; |
| | | let sortMap = new Map(); // 存储 id -> sort 的映射 |
| | | |
| | | if (isObjectArray) { |
| | | // 对象数组格式,提取ID和排序信息 |
| | | areaIds = record.areas.map(area => { |
| | | const id = area.id; |
| | | sortMap.set(id, area.sort || 0); |
| | | return id; |
| | | }); |
| | | } else { |
| | | // 纯ID数组格式 |
| | | areaIds = record.areas.map(id => Number(id)); |
| | | } |
| | | |
| | | const res = await request.post(`/warehouseAreas/many/${areaIds.join(',')}`); |
| | | if (res?.data?.code === 200) { |
| | | setAreaNames(res.data.data || []); |
| | | let areas = res.data.data || []; |
| | | |
| | | // 如果有排序信息,按排序值排序 |
| | | if (sortMap.size > 0) { |
| | | areas = areas.sort((a, b) => { |
| | | const sortA = sortMap.get(a.id) || 0; |
| | | const sortB = sortMap.get(b.id) || 0; |
| | | return sortA - sortB; |
| | | }); |
| | | } |
| | | |
| | | setAreaNames(areas); |
| | | } |
| | | } catch (error) { |
| | | console.error('获取区域名称失败:', error); |