216 lines
8.9 KiB
Vue
216 lines
8.9 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref } from 'vue'
|
|
import Keycloak from 'keycloak-js'
|
|
import zhCn from 'element-plus/es/locale/lang/zh-cn'
|
|
|
|
interface SpaceSummary { id: string; name: string }
|
|
interface Space extends SpaceSummary { items: number; photos: number }
|
|
interface SpacePage { spaces: SpaceSummary[]; hasMore: boolean }
|
|
|
|
const pageSize = 25
|
|
const identityUrl = import.meta.env.VITE_OIDC_URL || 'https://api-atlas.tutudog.com/auth'
|
|
|
|
const keycloak = new Keycloak({
|
|
url: new URL(identityUrl, window.location.origin).href,
|
|
realm: import.meta.env.VITE_OIDC_REALM || 'item-atlas',
|
|
clientId: 'atlas-admin',
|
|
})
|
|
const logout = () => keycloak.logout({ redirectUri: window.location.origin + '/' })
|
|
const initialized = ref(false)
|
|
const authenticated = ref(false)
|
|
const authorized = ref(false)
|
|
const input = ref('')
|
|
const activeQuery = ref('')
|
|
const pageOffset = ref(0)
|
|
const page = ref<SpacePage>()
|
|
const space = ref<Space>()
|
|
const busy = ref(false)
|
|
const error = ref('')
|
|
const success = ref('')
|
|
const confirming = ref(false)
|
|
const confirmation = ref('')
|
|
const isId = (value: string) => /^[\da-f]{8}(-[\da-f]{4}){3}-[\da-f]{12}$/i.test(value)
|
|
const pageNumber = computed(() => pageOffset.value / pageSize + 1)
|
|
|
|
onMounted(async () => {
|
|
try {
|
|
await keycloak.init({ onLoad: 'login-required', pkceMethod: 'S256', checkLoginIframe: false })
|
|
authenticated.value = keycloak.authenticated === true
|
|
const roles: unknown = keycloak.tokenParsed?.atlas_roles
|
|
authorized.value = authenticated.value && Array.isArray(roles) && roles.includes('atlas-space-admin')
|
|
if (authorized.value) await loadPage(0, '')
|
|
else error.value = '当前账号没有空间管理权限。'
|
|
} catch {
|
|
error.value = '无法连接登录服务,请刷新页面后重试。'
|
|
} finally { initialized.value = true }
|
|
})
|
|
|
|
function clearManagementData() {
|
|
authorized.value = false
|
|
page.value = undefined
|
|
space.value = undefined
|
|
confirming.value = false
|
|
confirmation.value = success.value = ''
|
|
}
|
|
|
|
function requireLogin(): never {
|
|
clearManagementData()
|
|
authenticated.value = false
|
|
throw new Error('登录已失效,请重新登录。')
|
|
}
|
|
|
|
async function request(path = '', method = 'GET') {
|
|
try { await keycloak.updateToken(30) }
|
|
catch { requireLogin() }
|
|
if (!keycloak.token) requireLogin()
|
|
const response = await fetch(`/api/admin/households${path}`, {
|
|
method, cache: 'no-store', headers: { Authorization: `Bearer ${keycloak.token}` },
|
|
})
|
|
if (!response.ok) {
|
|
if (response.status === 401) requireLogin()
|
|
if (response.status === 403) {
|
|
clearManagementData()
|
|
throw new Error('当前账号没有空间管理权限。')
|
|
}
|
|
if (response.status === 404) throw new Error('找不到这个空间。')
|
|
throw new Error('操作失败,请稍后重试。')
|
|
}
|
|
return response
|
|
}
|
|
|
|
async function readPage(offset: number, query: string) {
|
|
const params = new URLSearchParams({ q: query, limit: String(pageSize), offset: String(offset) })
|
|
const result: SpacePage = await (await request(`?${params}`)).json()
|
|
page.value = result
|
|
pageOffset.value = offset
|
|
activeQuery.value = query
|
|
}
|
|
|
|
async function loadPage(offset: number, query = activeQuery.value) {
|
|
if (busy.value) return
|
|
busy.value = true
|
|
space.value = undefined
|
|
success.value = error.value = ''
|
|
try { await readPage(offset, query) }
|
|
catch (failure) { error.value = failure instanceof Error ? failure.message : '列表加载失败。' }
|
|
finally { busy.value = false }
|
|
}
|
|
|
|
function findSpace() {
|
|
const query = input.value.trim()
|
|
if (isId(query)) {
|
|
page.value = undefined
|
|
return openSpace(query)
|
|
}
|
|
return loadPage(0, query)
|
|
}
|
|
|
|
async function openSpace(id: string) {
|
|
if (busy.value) return
|
|
busy.value = true
|
|
space.value = undefined
|
|
success.value = error.value = ''
|
|
try { space.value = await (await request(`/${id}`)).json() }
|
|
catch (failure) { error.value = failure instanceof Error ? failure.message : '查询失败。' }
|
|
finally { busy.value = false }
|
|
}
|
|
|
|
async function deleteSpace() {
|
|
if (!space.value || confirmation.value !== space.value.id || busy.value) return
|
|
busy.value = true
|
|
error.value = ''
|
|
try {
|
|
await request(`/${space.value.id}`, 'DELETE')
|
|
success.value = '空间已删除,原图和缩略图已进入清理队列。'
|
|
space.value = undefined
|
|
confirming.value = false
|
|
if (page.value) {
|
|
try {
|
|
await readPage(pageOffset.value, activeQuery.value)
|
|
if (page.value.spaces.length === 0 && pageOffset.value > 0) {
|
|
await readPage(pageOffset.value - pageSize, activeQuery.value)
|
|
}
|
|
} catch (failure) {
|
|
page.value = undefined
|
|
if (!authorized.value) throw failure
|
|
error.value = '空间已删除,列表刷新失败。请重新查询。'
|
|
}
|
|
}
|
|
} catch (failure) { error.value = failure instanceof Error ? failure.message : '删除失败。' }
|
|
finally { busy.value = false }
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<el-config-provider :locale="zhCn">
|
|
<main>
|
|
<header>
|
|
<h1>见物 <small>空间管理</small></h1>
|
|
<el-button v-if="authenticated" text @click="logout">退出登录</el-button>
|
|
<el-button v-else-if="initialized" text @click="keycloak.login()">重新登录</el-button>
|
|
</header>
|
|
<el-alert v-if="error" :title="error" type="error" show-icon :closable="false" />
|
|
<el-alert v-if="success" :title="success" type="success" show-icon :closable="false" />
|
|
<p v-if="!initialized">正在验证登录</p>
|
|
<template v-if="authorized">
|
|
<form @submit.prevent="findSpace">
|
|
<label for="space-search">查找空间</label>
|
|
<div class="lookup">
|
|
<el-input id="space-search" v-model="input" placeholder="空间名称或完整 ID" maxlength="100" clearable :disabled="busy" />
|
|
<el-button native-type="submit" type="primary" :disabled="busy">查询</el-button>
|
|
</div>
|
|
</form>
|
|
<el-card v-if="space" shadow="never">
|
|
<h2>{{ space.name }}</h2>
|
|
<el-descriptions :column="1">
|
|
<el-descriptions-item label="空间 ID">{{ space.id }}</el-descriptions-item>
|
|
<el-descriptions-item label="物品">{{ space.items }} 件</el-descriptions-item>
|
|
<el-descriptions-item label="图片">{{ space.photos }} 张</el-descriptions-item>
|
|
</el-descriptions>
|
|
<el-button type="danger" plain @click="confirmation = ''; confirming = true">删除空间</el-button>
|
|
</el-card>
|
|
<section v-if="page" v-loading="busy" class="space-list" aria-label="空间列表">
|
|
<el-table :data="page.spaces" row-key="id" empty-text="没有匹配的空间">
|
|
<el-table-column prop="name" label="空间名称" min-width="160" />
|
|
<el-table-column prop="id" label="空间 ID" min-width="250" />
|
|
<el-table-column label="操作" width="85">
|
|
<template #default="{ row }">
|
|
<el-button link type="primary" :disabled="busy" @click="openSpace(row.id)">查看</el-button>
|
|
</template>
|
|
</el-table-column>
|
|
</el-table>
|
|
<nav class="pages" aria-label="空间列表翻页">
|
|
<el-button :disabled="busy || pageOffset === 0" @click="loadPage(pageOffset - pageSize)">上一页</el-button>
|
|
<span>第 {{ pageNumber }} 页</span>
|
|
<el-button :disabled="busy || !page.hasMore" @click="loadPage(pageOffset + pageSize)">下一页</el-button>
|
|
</nav>
|
|
</section>
|
|
<el-dialog v-model="confirming" title="删除空间" width="min(500px, 92vw)" :close-on-click-modal="false" :close-on-press-escape="!busy" :show-close="!busy">
|
|
<p>物品和位置将被删除,成员会失去访问权限。原图和缩略图也会清理。</p>
|
|
<label for="confirm-id">请输入空间 ID {{ space?.id }}</label>
|
|
<el-input id="confirm-id" v-model="confirmation" :disabled="busy" />
|
|
<el-alert v-if="error" :title="error" type="error" :closable="false" />
|
|
<template #footer>
|
|
<el-button :disabled="busy" @click="confirming = false">取消</el-button>
|
|
<el-button type="danger" :disabled="confirmation !== space?.id" :loading="busy" @click="deleteSpace">确认删除</el-button>
|
|
</template>
|
|
</el-dialog>
|
|
</template>
|
|
</main>
|
|
</el-config-provider>
|
|
</template>
|
|
|
|
<style>
|
|
body { margin: 0; background: #f4f6fa; font-family: system-ui, sans-serif; color: #222c41; }
|
|
main { max-width: 920px; margin: 0 auto; padding: 26px 20px; }
|
|
header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
|
|
h1 { margin: 0; font-size: 26px; }
|
|
h1 small { color: #596476; font-size: 15px; font-weight: 500; margin-left: 8px; }
|
|
h2 { margin-top: 0; }
|
|
form, .el-alert { margin-bottom: 18px; }
|
|
label { display: block; margin-bottom: 8px; }
|
|
.lookup { display: flex; gap: 10px; }
|
|
.space-list { min-height: 120px; margin-top: 18px; }
|
|
.pages { display: flex; align-items: center; justify-content: flex-end; gap: 12px; margin: 16px 0; }
|
|
</style>
|