chore: 初始化 monorepo 脚手架与代码骨架
- Bun workspaces:apps/web(Vue 3.5 + Vite 8 + naive-ui)· apps/server(Hono 4.13 + Drizzle)· packages/shared(契约类型) - 代码骨架:四表 Drizzle schema · 11 个端点占位(501)· 管理面口令中间件 · 同步引擎占位(5 分钟 tick + 防重入) - 命令链:dev / build / test / lint / type-check / db:*(见 AGENTS.md)
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<script setup lang="ts">
|
||||
// 根组件:全局 provider(message / dialog / notification)+ 路由出口
|
||||
import { RouterView } from 'vue-router'
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<n-message-provider>
|
||||
<n-dialog-provider>
|
||||
<n-notification-provider>
|
||||
<RouterView />
|
||||
</n-notification-provider>
|
||||
</n-dialog-provider>
|
||||
</n-message-provider>
|
||||
</template>
|
||||
|
||||
<style>
|
||||
/* 全局基础样式(浅色单主题,交互设计 §2 #2) */
|
||||
html,
|
||||
body,
|
||||
#app {
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family:
|
||||
system-ui,
|
||||
-apple-system,
|
||||
'Segoe UI',
|
||||
'PingFang SC',
|
||||
'Microsoft YaHei',
|
||||
sans-serif;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import router from '@/router'
|
||||
|
||||
describe('router', () => {
|
||||
it('路由表包含查看面与管理面三页', () => {
|
||||
const paths = router.getRoutes().map((r) => r.path)
|
||||
for (const p of ['/usage-log', '/manage/api-keys', '/manage/purchases', '/manage/qpu']) {
|
||||
expect(paths).toContain(p)
|
||||
}
|
||||
})
|
||||
|
||||
it(
|
||||
'根路径重定向到 /usage-log(真实导航;首次含懒加载组件转换,放宽超时)',
|
||||
async () => {
|
||||
await router.push('/')
|
||||
expect(router.currentRoute.value.path).toBe('/usage-log')
|
||||
},
|
||||
20_000,
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* API 客户端(契约 约定 20 / 24):
|
||||
* - 成功 = 裸数据体;失败 = `{ error: { code, message } }`(非 2xx)
|
||||
* - 管理面请求自动附加 `X-Manage-Token`(存 sessionStorage,关标签即忘)
|
||||
* - 401 `manage_unauthorized` → 通知口令门帘(ManageLayout 监听)
|
||||
*/
|
||||
import { MANAGE_TOKEN_HEADER, type ApiErrorBody } from '@naku/shared'
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
readonly code: string,
|
||||
readonly status: number,
|
||||
message: string,
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'ApiError'
|
||||
}
|
||||
}
|
||||
|
||||
const MANAGE_TOKEN_KEY = 'naku:manage-token'
|
||||
|
||||
export function getManageToken(): string {
|
||||
return sessionStorage.getItem(MANAGE_TOKEN_KEY) ?? ''
|
||||
}
|
||||
|
||||
export function setManageToken(token: string): void {
|
||||
sessionStorage.setItem(MANAGE_TOKEN_KEY, token)
|
||||
}
|
||||
|
||||
export function clearManageToken(): void {
|
||||
sessionStorage.removeItem(MANAGE_TOKEN_KEY)
|
||||
}
|
||||
|
||||
/** 口令失效通知(订阅返回取消函数) */
|
||||
const unauthorizedListeners = new Set<() => void>()
|
||||
|
||||
export function onManageUnauthorized(fn: () => void): () => void {
|
||||
unauthorizedListeners.add(fn)
|
||||
return () => {
|
||||
unauthorizedListeners.delete(fn)
|
||||
}
|
||||
}
|
||||
|
||||
interface RequestOptions {
|
||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE'
|
||||
body?: unknown
|
||||
/** 管理面请求:附加 X-Manage-Token */
|
||||
manage?: boolean
|
||||
}
|
||||
|
||||
export async function request<T>(path: string, options: RequestOptions = {}): Promise<T> {
|
||||
const { method = 'GET', body, manage = false } = options
|
||||
|
||||
const headers: Record<string, string> = {}
|
||||
if (body !== undefined) headers['content-type'] = 'application/json'
|
||||
if (manage) headers[MANAGE_TOKEN_HEADER] = getManageToken()
|
||||
|
||||
const res = await fetch(path, {
|
||||
method,
|
||||
headers,
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
// 失败体未必是我们的 JSON(反代 502 / 网关错误页)——兜底解析
|
||||
const parsed = (await res.json().catch(() => null)) as ApiErrorBody | null
|
||||
const code = parsed?.error?.code ?? 'unknown'
|
||||
const message = parsed?.error?.message ?? `HTTP ${res.status}`
|
||||
if (manage && res.status === 401 && code === 'manage_unauthorized') {
|
||||
unauthorizedListeners.forEach((fn) => fn())
|
||||
}
|
||||
throw new ApiError(code, res.status, message)
|
||||
}
|
||||
|
||||
return (await res.json()) as T
|
||||
}
|
||||
|
||||
// ---- 查看面(无会话;key 集合随请求体,契约 约定 5 / 19) ----
|
||||
|
||||
export const api = {
|
||||
usageLogsQuery: <T>(body: unknown) => request<T>('/api/usage/logs/query', { method: 'POST', body }),
|
||||
usageSummary: <T>(body: unknown) => request<T>('/api/usage/summary', { method: 'POST', body }),
|
||||
|
||||
// ---- 管理面(manage: true 自动带口令) ----
|
||||
apiKeysList: <T>() => request<T>('/api/manage/api-keys', { manage: true }),
|
||||
apiKeyRegister: <T>(body: unknown) =>
|
||||
request<T>('/api/manage/api-keys', { method: 'POST', body, manage: true }),
|
||||
apiKeyPatch: <T>(id: number, body: unknown) =>
|
||||
request<T>(`/api/manage/api-keys/${id}`, { method: 'PATCH', body, manage: true }),
|
||||
|
||||
purchasesList: <T>() => request<T>('/api/manage/purchases', { manage: true }),
|
||||
purchasesCreate: <T>(body: unknown) =>
|
||||
request<T>('/api/manage/purchases', { method: 'POST', body, manage: true }),
|
||||
purchasesPatch: <T>(id: number, body: unknown) =>
|
||||
request<T>(`/api/manage/purchases/${id}`, { method: 'PATCH', body, manage: true }),
|
||||
purchasesDelete: <T>(id: number) =>
|
||||
request<T>(`/api/manage/purchases/${id}`, { method: 'DELETE', manage: true }),
|
||||
purchasesObservations: <T>() => request<T>('/api/manage/purchases/observations', { manage: true }),
|
||||
|
||||
qpuList: <T>() => request<T>('/api/manage/qpu', { manage: true }),
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
/* eslint-disable */
|
||||
/* prettier-ignore */
|
||||
/* oxlint-disable */
|
||||
/* oxfmt-ignore */
|
||||
// @ts-nocheck
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
// Generated by unplugin-auto-import
|
||||
// biome-ignore lint: disable
|
||||
export {}
|
||||
declare global {
|
||||
const useDialog: typeof import('naive-ui').useDialog
|
||||
const useLoadingBar: typeof import('naive-ui').useLoadingBar
|
||||
const useMessage: typeof import('naive-ui').useMessage
|
||||
const useNotification: typeof import('naive-ui').useNotification
|
||||
}
|
||||
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
/* eslint-disable */
|
||||
// @ts-nocheck
|
||||
// biome-ignore lint: disable
|
||||
// oxlint-disable
|
||||
// ------
|
||||
// Generated by unplugin-vue-components
|
||||
// Read more: https://github.com/vuejs/core/pull/3399
|
||||
|
||||
export {}
|
||||
|
||||
/* prettier-ignore */
|
||||
declare module 'vue' {
|
||||
export interface GlobalComponents {
|
||||
NButton: typeof import('naive-ui')['NButton']
|
||||
NCard: typeof import('naive-ui')['NCard']
|
||||
NDialogProvider: typeof import('naive-ui')['NDialogProvider']
|
||||
NEmpty: typeof import('naive-ui')['NEmpty']
|
||||
NInput: typeof import('naive-ui')['NInput']
|
||||
NLayout: typeof import('naive-ui')['NLayout']
|
||||
NLayoutContent: typeof import('naive-ui')['NLayoutContent']
|
||||
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
|
||||
NMenu: typeof import('naive-ui')['NMenu']
|
||||
NMessageProvider: typeof import('naive-ui')['NMessageProvider']
|
||||
NNotificationProvider: typeof import('naive-ui')['NNotificationProvider']
|
||||
NSpace: typeof import('naive-ui')['NSpace']
|
||||
NText: typeof import('naive-ui')['NText']
|
||||
RouterLink: typeof import('vue-router')['RouterLink']
|
||||
RouterView: typeof import('vue-router')['RouterView']
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { createApp } from 'vue'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
|
||||
const app = createApp(App)
|
||||
|
||||
app.use(router)
|
||||
|
||||
app.mount('#app')
|
||||
@@ -0,0 +1,42 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
|
||||
/**
|
||||
* 路由(交互设计 §0.1 / §1):
|
||||
* - 无前缀 = 查看面(一期仅 `/usage-log`,`/` 重定向到它;二期候选 `/dashboard`)
|
||||
* - `/manage` 前缀 = 管理面(口令门帘由 ManageLayout 承担;入口在查看面不暴露)
|
||||
*/
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes: [
|
||||
{ path: '/', redirect: '/usage-log' },
|
||||
{
|
||||
path: '/usage-log',
|
||||
name: 'usage-log',
|
||||
component: () => import('@/views/UsageLogView.vue'),
|
||||
},
|
||||
{
|
||||
path: '/manage',
|
||||
component: () => import('@/views/manage/ManageLayout.vue'),
|
||||
children: [
|
||||
{ path: '', redirect: '/manage/api-keys' },
|
||||
{
|
||||
path: 'api-keys',
|
||||
name: 'manage-api-keys',
|
||||
component: () => import('@/views/manage/ApiKeysView.vue'),
|
||||
},
|
||||
{
|
||||
path: 'purchases',
|
||||
name: 'manage-purchases',
|
||||
component: () => import('@/views/manage/PurchasesView.vue'),
|
||||
},
|
||||
{
|
||||
path: 'qpu',
|
||||
name: 'manage-qpu',
|
||||
component: () => import('@/views/manage/QpuView.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,20 @@
|
||||
<script setup lang="ts">
|
||||
// 用量日志页(交互设计 §3.1)——骨架占位:
|
||||
// 筛选区(key 标签输入 + datetimerange + 查询)→ 卡片区(次数 / 用量 / 消费)→ 未收录提示 → 明细表格 + 分页器
|
||||
// 数据形状见 docs/design/API契约.md §2.1
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="usage-log">
|
||||
<h1>用量日志</h1>
|
||||
<n-empty description="骨架占位——页面结构见 docs/design/交互设计.md §3.1" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.usage-log {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 24px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,13 @@
|
||||
<script setup lang="ts">
|
||||
// key 管理页(交互设计 §3.2)——骨架占位:
|
||||
// 清单 8 列(key 名 / 状态徽章 / 已用 / 剩余 / 到期 / 登记时间 / 数据截止 / 操作)
|
||||
// 登记:页头「+ 登记 key」→ n-modal;启停:行内按钮(停用弹确认、启用直接执行)
|
||||
// 数据形状见 docs/design/API契约.md §2.2
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="api-keys">
|
||||
<h2>key 管理</h2>
|
||||
<n-empty description="骨架占位——页面结构见 docs/design/交互设计.md §3.2" />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,140 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* 管理面布局(交互设计 §2 #7 + 口令门帘细节):
|
||||
* - 未验证:全屏居中卡片(口令 + 「进入」)——校验直接用管理面请求,401 即口令错
|
||||
* - 已验证:侧边栏菜单(三页)+ 内容区;底部「← 查看面」
|
||||
* - 口令存 sessionStorage(关标签即忘);401 时自动退回门帘
|
||||
*/
|
||||
import { onMounted, onUnmounted, ref } from 'vue'
|
||||
import { RouterView, useRoute, useRouter } from 'vue-router'
|
||||
|
||||
import {
|
||||
ApiError,
|
||||
api,
|
||||
clearManageToken,
|
||||
getManageToken,
|
||||
onManageUnauthorized,
|
||||
setManageToken,
|
||||
} from '@/api/client'
|
||||
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
|
||||
const unlocked = ref(false)
|
||||
const checking = ref(false)
|
||||
const tokenInput = ref('')
|
||||
const errorText = ref('')
|
||||
|
||||
async function verifyAndUnlock(): Promise<void> {
|
||||
checking.value = true
|
||||
errorText.value = ''
|
||||
try {
|
||||
await api.apiKeysList()
|
||||
unlocked.value = true
|
||||
} catch (err) {
|
||||
clearManageToken()
|
||||
errorText.value =
|
||||
err instanceof ApiError && err.code === 'manage_unauthorized'
|
||||
? '口令不正确'
|
||||
: '校验失败,请稍后重试'
|
||||
unlocked.value = false
|
||||
} finally {
|
||||
checking.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function tryUnlock(): Promise<void> {
|
||||
if (!tokenInput.value) {
|
||||
errorText.value = '请输入口令'
|
||||
return
|
||||
}
|
||||
setManageToken(tokenInput.value)
|
||||
await verifyAndUnlock()
|
||||
}
|
||||
|
||||
let off: (() => void) | null = null
|
||||
|
||||
onMounted(() => {
|
||||
off = onManageUnauthorized(() => {
|
||||
unlocked.value = false
|
||||
errorText.value = '口令已失效,请重新输入'
|
||||
})
|
||||
if (getManageToken()) {
|
||||
void verifyAndUnlock()
|
||||
}
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
off?.()
|
||||
})
|
||||
|
||||
const menuOptions = [
|
||||
{ label: 'key 管理', key: 'manage-api-keys' },
|
||||
{ label: '购买记录', key: 'manage-purchases' },
|
||||
{ label: '换算参数', key: 'manage-qpu' },
|
||||
]
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="!unlocked" class="gate">
|
||||
<n-card class="gate-card" title="管理通道">
|
||||
<n-space vertical :size="16">
|
||||
<n-input
|
||||
v-model:value="tokenInput"
|
||||
type="password"
|
||||
show-password-on="click"
|
||||
placeholder="部署口令"
|
||||
@keyup.enter="tryUnlock"
|
||||
/>
|
||||
<n-text v-if="errorText" type="error">{{ errorText }}</n-text>
|
||||
<n-button type="primary" block :loading="checking" @click="tryUnlock">进入</n-button>
|
||||
</n-space>
|
||||
</n-card>
|
||||
</div>
|
||||
|
||||
<n-layout v-else has-sider class="manage">
|
||||
<n-layout-sider bordered :width="200">
|
||||
<div class="sider">
|
||||
<n-menu
|
||||
:value="String(route.name ?? '')"
|
||||
:options="menuOptions"
|
||||
@update:value="(key: string) => router.push({ name: key })"
|
||||
/>
|
||||
<n-button text class="back" @click="router.push('/usage-log')">← 查看面</n-button>
|
||||
</div>
|
||||
</n-layout-sider>
|
||||
<n-layout-content class="content">
|
||||
<RouterView />
|
||||
</n-layout-content>
|
||||
</n-layout>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.gate {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
}
|
||||
.gate-card {
|
||||
width: 360px;
|
||||
}
|
||||
.manage {
|
||||
height: 100vh;
|
||||
}
|
||||
.sider {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
height: 100%;
|
||||
padding: 16px 0;
|
||||
}
|
||||
.back {
|
||||
margin-left: 24px;
|
||||
align-self: flex-start;
|
||||
}
|
||||
.content {
|
||||
padding: 24px;
|
||||
overflow: auto;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
// 购买记录页(交互设计 §3.3)——骨架占位:
|
||||
// 清单 8 列 + 核对提示(未登记观测提示条 + 折叠区 + 补录)+ 登记/编辑用右侧 n-drawer
|
||||
// 数据形状见 docs/design/API契约.md §2.3
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="purchases">
|
||||
<h2>购买记录</h2>
|
||||
<n-empty description="骨架占位——页面结构见 docs/design/交互设计.md §3.3" />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
// 换算参数时间线页(交互设计 §3.3)——骨架占位:
|
||||
// 表格两列(生效时间 / QPU 值)+「当前生效」标记 + 页首说明文案;纯只读
|
||||
// 数据形状见 docs/design/API契约.md §2.4
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="qpu">
|
||||
<h2>换算参数时间线</h2>
|
||||
<n-empty description="骨架占位——页面结构见 docs/design/交互设计.md §3.3" />
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user