item-atlas/apps/admin/src/App.vue

125 lines
5.4 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 Space { id: string; name: string; items: number; photos: number }
const keycloak = new Keycloak({
url: import.meta.env.VITE_OIDC_URL || 'https://api-atlas.tutudog.com/auth',
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 authorized = ref(false)
const input = ref('')
const space = ref<Space>()
const busy = ref(false)
const error = ref('')
const success = ref('')
const confirming = ref(false)
const confirmation = ref('')
const validId = computed(() => /^[\da-f]{8}(-[\da-f]{4}){3}-[\da-f]{12}$/i.test(input.value.trim()))
onMounted(async () => {
try {
await keycloak.init({ onLoad: 'login-required', pkceMethod: 'S256', checkLoginIframe: false })
authorized.value = keycloak.tokenParsed?.atlas_roles?.includes('atlas-space-admin') === true
if (!authorized.value) error.value = '当前账号没有空间管理权限。'
} catch {
error.value = '无法连接登录服务,请刷新页面后重试。'
} finally { initialized.value = true }
})
async function request(id: string, method = 'GET') {
await keycloak.updateToken(30)
const response = await fetch(`/api/admin/households/${id}`, {
method, cache: 'no-store', headers: { Authorization: `Bearer ${keycloak.token}` },
})
if (!response.ok) {
if (response.status === 404) throw new Error('找不到这个空间。')
if (response.status === 403) throw new Error('当前账号没有空间管理权限。')
throw new Error('操作失败,请稍后重试。')
}
return response
}
async function findSpace() {
if (!validId.value || busy.value) return
busy.value = true
space.value = undefined
success.value = error.value = ''
try { space.value = await (await request(input.value.trim())).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
} 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="keycloak.authenticated" text @click="logout">退出登录</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-id">空间 ID</label>
<div class="lookup">
<el-input id="space-id" v-model="input" placeholder="输入要查询的空间 UUID" :disabled="busy" />
<el-button native-type="submit" type="primary" :disabled="!validId" :loading="busy && !confirming">查询</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>
<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: 720px; 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; }
</style>