Skip to content

Commit 54d27b9

Browse files
authored
Merge pull request #85 from realtvop/dev
fix: 修复藏品页在 items 为空时的遍历崩溃
2 parents 5d4e832 + c35eefd commit 54d27b9

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

src/pages/collections.vue

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,23 @@
3737
}
3838
3939
const ownedCollections = computed<Record<number, number[]>>(() => {
40-
if (!users || !users[0] || !users[0].data.items)
41-
return { 1: [], 2: [], 3: [], 9: [], 10: [], 11: [] };
42-
4340
const owned: Record<number, number[]> = { 1: [], 2: [], 3: [], 9: [], 10: [], 11: [] };
44-
for (const collection of users[0].data.items[1]) owned[1].push(collection.itemId);
45-
for (const collection of users[0].data.items[2]) owned[2].push(collection.itemId);
46-
for (const collection of users[0].data.items[3]) owned[3].push(collection.itemId);
47-
if (users[0].data.items[9]) {
48-
for (const collection of users[0].data.items[9]) owned[9].push(collection.itemId);
49-
}
50-
if (users[0].data.items[10]) {
51-
for (const collection of users[0].data.items[10]) owned[10].push(collection.itemId);
52-
}
53-
if (users[0].data.items[11]) {
54-
for (const collection of users[0].data.items[11]) owned[11].push(collection.itemId);
55-
}
41+
const items = users?.[0]?.data?.items;
42+
const getItemsByKind = (kind: CollectionKind) =>
43+
Array.isArray(items?.[kind]) ? items[kind] : [];
44+
45+
for (const collection of getItemsByKind(CollectionKind.Plate))
46+
owned[CollectionKind.Plate].push(collection.itemId);
47+
for (const collection of getItemsByKind(CollectionKind.Title))
48+
owned[CollectionKind.Title].push(collection.itemId);
49+
for (const collection of getItemsByKind(CollectionKind.Icon))
50+
owned[CollectionKind.Icon].push(collection.itemId);
51+
for (const collection of getItemsByKind(CollectionKind.Character))
52+
owned[CollectionKind.Character].push(collection.itemId);
53+
for (const collection of getItemsByKind(CollectionKind.Partner))
54+
owned[CollectionKind.Partner].push(collection.itemId);
55+
for (const collection of getItemsByKind(CollectionKind.Frame))
56+
owned[CollectionKind.Frame].push(collection.itemId);
5657
5758
return owned;
5859
});

0 commit comments

Comments
 (0)