94 lines
2.2 KiB
TypeScript
94 lines
2.2 KiB
TypeScript
|
|
import { api } from '@/utils/request'
|
||
|
|
import type {
|
||
|
|
PaginatedData,
|
||
|
|
OperationLogRecord,
|
||
|
|
OperationLogFilters,
|
||
|
|
} from '@/types/api'
|
||
|
|
|
||
|
|
export type { OperationLogRecord }
|
||
|
|
|
||
|
|
interface UserLookup {
|
||
|
|
id: number
|
||
|
|
username: string
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useOperationLogStore = defineStore('operationLog', () => {
|
||
|
|
const logs = ref<OperationLogRecord[]>([])
|
||
|
|
const loading = ref(false)
|
||
|
|
const pagination = reactive({
|
||
|
|
page: 1,
|
||
|
|
per_page: 20,
|
||
|
|
total: 0,
|
||
|
|
})
|
||
|
|
const filters = reactive<OperationLogFilters>({
|
||
|
|
user_id: undefined,
|
||
|
|
action: undefined,
|
||
|
|
target_type: undefined,
|
||
|
|
created_at_range: null,
|
||
|
|
})
|
||
|
|
|
||
|
|
const users = ref<UserLookup[]>([])
|
||
|
|
const userMap = computed(
|
||
|
|
() => new Map(users.value.map((u) => [u.id, u.username])),
|
||
|
|
)
|
||
|
|
|
||
|
|
async function loadLookups() {
|
||
|
|
try {
|
||
|
|
const data = await api.get<PaginatedData<UserLookup>>('/api/v1/users', {
|
||
|
|
per_page: 200,
|
||
|
|
})
|
||
|
|
users.value = data.items
|
||
|
|
} catch (err: unknown) {
|
||
|
|
console.warn('加载用户列表失败', err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function fetchLogs() {
|
||
|
|
loading.value = true
|
||
|
|
try {
|
||
|
|
const data = await api.get<PaginatedData<OperationLogRecord>>(
|
||
|
|
'/api/v1/logs/operations',
|
||
|
|
{
|
||
|
|
page: pagination.page,
|
||
|
|
per_page: pagination.per_page,
|
||
|
|
user_id: filters.user_id || undefined,
|
||
|
|
action: filters.action || undefined,
|
||
|
|
target_type: filters.target_type || undefined,
|
||
|
|
created_at_from: filters.created_at_range?.[0] || undefined,
|
||
|
|
created_at_to: filters.created_at_range?.[1] || undefined,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
logs.value = data.items
|
||
|
|
pagination.total = data.total
|
||
|
|
pagination.page = data.page
|
||
|
|
} catch (err: unknown) {
|
||
|
|
logs.value = []
|
||
|
|
pagination.total = 0
|
||
|
|
const msg = err instanceof Error ? err.message : '获取操作日志列表失败'
|
||
|
|
message.error(msg)
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function resetFilters() {
|
||
|
|
filters.user_id = undefined
|
||
|
|
filters.action = undefined
|
||
|
|
filters.target_type = undefined
|
||
|
|
filters.created_at_range = null
|
||
|
|
pagination.page = 1
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
logs,
|
||
|
|
loading,
|
||
|
|
pagination,
|
||
|
|
filters,
|
||
|
|
users,
|
||
|
|
userMap,
|
||
|
|
loadLookups,
|
||
|
|
fetchLogs,
|
||
|
|
resetFilters,
|
||
|
|
}
|
||
|
|
})
|