refactor: 统一使用 generateId 函数生成唯一标识符

- 在 GMView、FleetView 中替换 Date.now() 生成 ID 的方式
- 在 DiplomacyView 中优化排序函数,避免重复过滤与排序
This commit is contained in:
wenyu
2026-03-18 21:04:11 +08:00
parent 15eccd8f0d
commit d8dd4e7317
3 changed files with 9 additions and 7 deletions

View File

@@ -427,7 +427,7 @@
import NpcRelationCard from '@/components/npc/NpcRelationCard.vue' import NpcRelationCard from '@/components/npc/NpcRelationCard.vue'
import NpcRelationRow from '@/components/npc/NpcRelationRow.vue' import NpcRelationRow from '@/components/npc/NpcRelationRow.vue'
import { RelationStatus } from '@/types/game' import { RelationStatus } from '@/types/game'
import type { DiplomaticRelation } from '@/types/game' import type { DiplomaticRelation, NPC } from '@/types/game'
import * as npcBehaviorLogic from '@/logic/npcBehaviorLogic' import * as npcBehaviorLogic from '@/logic/npcBehaviorLogic'
import { import {
Search, Search,
@@ -481,8 +481,8 @@
} }
// 排序函数 // 排序函数
const sortNpcs = (npcs: typeof npcStore.npcs) => { const sortNpcs = (npcs: NPC[], predicate: (npc: NPC) => boolean = () => true) => {
return [...npcs].sort((a, b) => { return npcs.filter(predicate).sort((a, b) => {
let valA = 0 let valA = 0
let valB = 0 let valB = 0
@@ -707,7 +707,7 @@
} }
// 搜索过滤函数 // 搜索过滤函数
const matchesSearch = (npc: (typeof npcStore.npcs)[0]) => { const matchesSearch = (npc: NPC) => {
if (!searchQuery.value.trim()) return true if (!searchQuery.value.trim()) return true
const query = searchQuery.value.toLowerCase().trim() const query = searchQuery.value.toLowerCase().trim()
return npc.name.toLowerCase().includes(query) || npc.id.toLowerCase().includes(query) return npc.name.toLowerCase().includes(query) || npc.id.toLowerCase().includes(query)
@@ -715,7 +715,7 @@
// 按关系状态分类NPC同时应用搜索过滤 // 按关系状态分类NPC同时应用搜索过滤
// 先统一排序一次,避免不同标签页在同一批数据上重复排序 // 先统一排序一次,避免不同标签页在同一批数据上重复排序
const sortedNpcs = computed(() => sortNpcs(npcStore.npcs.filter(matchesSearch))) const sortedNpcs = computed(() => sortNpcs(npcStore.npcs, matchesSearch))
const allNpcs = computed(() => sortedNpcs.value) const allNpcs = computed(() => sortedNpcs.value)

View File

@@ -642,6 +642,7 @@
import * as diplomaticLogic from '@/logic/diplomaticLogic' import * as diplomaticLogic from '@/logic/diplomaticLogic'
import * as gameLogic from '@/logic/gameLogic' import * as gameLogic from '@/logic/gameLogic'
import * as moonLogic from '@/logic/moonLogic' import * as moonLogic from '@/logic/moonLogic'
import { generateId } from '@/utils/id'
const route = useRoute() const route = useRoute()
const gameStore = useGameStore() const gameStore = useGameStore()
@@ -934,7 +935,7 @@
// 生成唯一ID // 生成唯一ID
const generatePresetId = (): string => { const generatePresetId = (): string => {
return `preset_${Date.now()}_${Math.random().toString(36).substr(2, 9)}` return generateId('fleet_preset')
} }
// 保存当前配置为预设 // 保存当前配置为预设

View File

@@ -319,6 +319,7 @@
import * as publicLogic from '@/logic/publicLogic' import * as publicLogic from '@/logic/publicLogic'
import { calculateMaxFleetStorage } from '@/logic/fleetStorageLogic' import { calculateMaxFleetStorage } from '@/logic/fleetStorageLogic'
import { calculateMissileSiloCapacity } from '@/logic/missileLogic' import { calculateMissileSiloCapacity } from '@/logic/missileLogic'
import { generateId } from '@/utils/id'
import { Home, Trash2 } from 'lucide-vue-next' import { Home, Trash2 } from 'lucide-vue-next'
// --- 预设系统 --- // --- 预设系统 ---
@@ -461,7 +462,7 @@
} }
const newPreset: GMPreset = { const newPreset: GMPreset = {
id: Date.now().toString(), id: generateId('gm_preset'),
name, name,
values values
} }