Console.vue
11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!-- 后台布局:左侧导航(窄屏变抽屉)+ 顶栏(当前位置、环境、告警、用户菜单)。 -->
<script setup lang="ts">
import { Bell, Box, ChatDotRound, DataAnalysis, Document, Download, Expand, Files, Key, List,
MagicStick, Monitor, Odometer, Setting, SwitchButton, Warning } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { api, tokens } from '@/api'
import { lastList, listPath } from '@/nav'
import { logout, session } from '@/session'
const route = useRoute()
const router = useRouter()
const drawer = ref(false)
const pwdOpen = ref(false)
const pwd = ref({ old_password: '', new_password: '', confirm: '' })
const saving = ref(false)
const groups = [
{ title: '', items: [{ path: '/', label: '工作台', icon: Odometer }] },
{ title: '库存补货', items: [
{ path: '/runs', label: '补货单', icon: List },
{ path: '/categories', label: '在库分类', icon: Box },
{ path: '/so', label: 'SO 单同步', icon: Files },
{ path: '/rules', label: '补货规则', icon: DataAnalysis },
{ path: '/exports', label: '数据导出', icon: Download },
] },
{ title: '智能与连接', items: [
{ path: '/assistant', label: '智能助手', icon: MagicStick },
{ path: '/wecom', label: '消息通道', icon: ChatDotRound },
{ path: '/client', label: '取数端', icon: Monitor },
] },
{ title: '系统', items: [
{ path: '/alerts', label: '告警', icon: Warning },
{ path: '/audit', label: '操作日志', icon: Document },
{ path: '/settings', label: '系统设置', icon: Setting },
] },
]
// 普通账号(客户)不显示路由上标了 meta.admin 的菜单;只是隐藏,不做接口权限
const visibleGroups = computed(() => session.isAdmin ? groups
: groups.map((g) => ({ ...g, items: g.items.filter((it) => !router.resolve(it.path).meta.admin) })).filter((g) => g.items.length))
const active = computed(() => (route.meta.nav as string) || route.path)
// 面包屑:分组 → 菜单项 → 详情;除最后一项都能点(分组跳到组里第一项,菜单项回到上次的页码 / 筛选)
const crumb = computed(() => {
const g = groups.find((x) => x.items.some((it) => it.path === active.value))
const it = g?.items.find((x) => x.path === active.value)
const trail: { label: string; to?: string }[] = []
if (g?.title) trail.push({ label: g.title, to: listPath(g.items[0].path) })
if (it) trail.push({ label: it.label, to: listPath(it.path) })
if (route.meta.title && route.meta.title !== it?.label) trail.push({ label: String(route.meta.title) })
if (!trail.length) trail.push({ label: String(route.meta.title || '') })
trail[trail.length - 1].to = undefined
return trail
})
watch(() => route.fullPath, () => {
drawer.value = false
if (groups.some((g) => g.items.some((it) => it.path === route.path))) lastList[route.path] = route.fullPath
}, { immediate: true })
let timer: any = null
async function pollAlerts() {
try { session.openAlerts = (await api.get('/api/alerts?status=open&size=1')).total } catch { /* 忽略 */ }
}
onMounted(() => { pollAlerts(); timer = setInterval(pollAlerts, 30000) })
onUnmounted(() => clearInterval(timer))
function onCommand(cmd: string) {
if (cmd === 'password') pwdOpen.value = true
if (cmd === 'logout') { logout(); router.replace('/login') }
}
async function changePassword() {
if (pwd.value.new_password.length < 6) return ElMessage.warning('新密码至少 6 位')
if (pwd.value.new_password !== pwd.value.confirm) return ElMessage.warning('两次输入的新密码不一致')
saving.value = true
try {
const r = await api.post('/api/me/password', { old_password: pwd.value.old_password, new_password: pwd.value.new_password })
tokens.set(r.access_token, r.refresh_token)
session.user = r.user
pwdOpen.value = false
pwd.value = { old_password: '', new_password: '', confirm: '' }
ElMessage.success('密码已修改')
} catch (e: any) { ElMessage.error(e.message) } finally { saving.value = false }
}
</script>
<template>
<div class="shell">
<aside class="side" :class="{ open: drawer }">
<div class="brand">
<div class="logo">
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"><path d="M4 8l8-4.5L20 8v8l-8 4.5L4 16z" /><path d="M4 8l8 4.5L20 8M12 12.5v8" /></svg>
</div>
<div class="brand-text">
<div class="brand-name">库存补货助手</div>
<div class="brand-store ellipsis">{{ session.store?.name || '—' }}</div>
</div>
</div>
<nav class="nav">
<div v-for="g in visibleGroups" :key="g.title" class="nav-group">
<div v-if="g.title" class="nav-title">{{ g.title }}</div>
<router-link v-for="it in g.items" :key="it.path" :to="it.path" class="nav-item" :class="{ active: active === it.path }">
<el-icon :size="16"><component :is="it.icon" /></el-icon>
<span>{{ it.label }}</span>
<span v-if="it.path === '/alerts' && session.openAlerts" class="nav-badge">{{ session.openAlerts }}</span>
</router-link>
</div>
</nav>
<div class="side-foot muted small">v{{ session.version }} · {{ session.envLabel }}</div>
</aside>
<div v-if="drawer" class="mask" @click="drawer = false" />
<div class="main">
<header class="top">
<button class="menu-btn" aria-label="打开菜单" @click="drawer = true"><el-icon :size="18"><Expand /></el-icon></button>
<nav class="crumb" aria-label="当前位置">
<template v-for="(c, i) in crumb" :key="i">
<span v-if="i" class="crumb-sep">/</span>
<router-link v-if="c.to" :to="c.to" class="crumb-link">{{ c.label }}</router-link>
<span v-else class="cur">{{ c.label }}</span>
</template>
</nav>
<div class="top-right">
<el-tag v-if="session.env && session.env !== 'prd'" size="small" type="warning" effect="plain" round>{{ session.envLabel }}</el-tag>
<router-link to="/alerts" class="icon-btn" :class="{ hot: session.openAlerts }" title="未处理告警">
<el-badge :value="session.openAlerts" :hidden="!session.openAlerts" :max="99"><el-icon :size="18"><Bell /></el-icon></el-badge>
</router-link>
<el-dropdown trigger="click" @command="onCommand">
<button class="user">
<span class="avatar">{{ (session.user?.display_name || session.user?.username || '?').slice(0, 1) }}</span>
<span class="user-name">{{ session.user?.display_name || session.user?.username }}</span>
</button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="password" :icon="Key">修改密码</el-dropdown-item>
<el-dropdown-item command="logout" :icon="SwitchButton" divided>退出登录</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</header>
<main class="content"><router-view /></main>
</div>
<el-dialog v-model="pwdOpen" title="修改密码" width="420px" align-center>
<el-form label-position="top" @submit.prevent="changePassword">
<el-form-item label="原密码"><el-input v-model="pwd.old_password" type="password" show-password autocomplete="current-password" /></el-form-item>
<el-form-item label="新密码(至少 6 位)"><el-input v-model="pwd.new_password" type="password" show-password autocomplete="new-password" /></el-form-item>
<el-form-item label="再输一次新密码"><el-input v-model="pwd.confirm" type="password" show-password autocomplete="new-password" /></el-form-item>
</el-form>
<template #footer>
<el-button @click="pwdOpen = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="changePassword">保存</el-button>
</template>
</el-dialog>
</div>
</template>
<style scoped>
.shell { display: flex; min-height: 100vh; }
.side { position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w); background: var(--c-surface); border-right: 1px solid var(--c-border);
display: flex; flex-direction: column; z-index: 40; transition: transform 0.2s ease; }
.brand { display: flex; align-items: center; gap: 10px; height: 64px; padding: 0 18px; border-bottom: 1px solid var(--c-border); }
.logo { width: 32px; height: 32px; border-radius: 9px; background: var(--c-primary); color: #fff; display: grid; place-items: center; flex-shrink: 0; }
.brand-text { min-width: 0; }
.brand-name { font-weight: 650; font-size: 14.5px; letter-spacing: 0.02em; }
.brand-store { font-size: 12px; color: var(--c-text-3); max-width: 160px; }
.nav { flex: 1; overflow-y: auto; padding: 10px 10px 16px; }
.nav-group + .nav-group { margin-top: 10px; }
.nav-title { padding: 12px 10px 6px; font-size: 11.5px; font-weight: 600; color: var(--c-text-3); letter-spacing: 0.06em; }
.nav-group + .nav-group { border-top: 1px solid var(--c-border); padding-top: 4px; }
.nav-item { display: flex; align-items: center; gap: 10px; height: 36px; padding: 0 10px; border-radius: 8px; color: var(--c-text-2);
font-size: 13.5px; font-weight: 500; margin-bottom: 2px; transition: background 0.12s, color 0.12s; }
.nav-item:hover { background: var(--c-bg); color: var(--c-text); }
.nav-item.active { background: var(--c-primary-soft); color: var(--c-primary); font-weight: 600; }
.nav-badge { margin-left: auto; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--c-err); color: #fff;
font-size: 11px; display: grid; place-items: center; font-variant-numeric: tabular-nums; }
.side-foot { padding: 12px 20px; border-top: 1px solid var(--c-border); }
.main { flex: 1; min-width: 0; margin-left: var(--sidebar-w); display: flex; flex-direction: column; }
.top { position: sticky; top: 0; z-index: 30; height: var(--topbar-h); display: flex; align-items: center; gap: 12px; padding: 0 16px;
background: rgba(245, 246, 248, 0.86); backdrop-filter: saturate(160%) blur(8px); border-bottom: 1px solid var(--c-border); }
.crumb { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--c-text-3); min-width: 0; }
.crumb .cur { color: var(--c-text); font-weight: 600; white-space: nowrap; }
.crumb-link { color: var(--c-text-3); white-space: nowrap; border-radius: 4px; transition: color 0.12s; }
.crumb-link:hover { color: var(--c-primary); }
.crumb-sep { color: var(--c-border-strong); }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.icon-btn { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 8px; color: var(--c-text-2); }
.icon-btn:hover { background: var(--c-surface); color: var(--c-text); }
.icon-btn.hot { color: var(--c-err); }
.user { display: flex; align-items: center; gap: 8px; height: 34px; padding: 0 10px 0 4px; border: 0; border-radius: 17px; background: transparent;
cursor: pointer; font: inherit; color: var(--c-text); }
.user:hover { background: var(--c-surface); }
.avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--c-primary-soft); color: var(--c-primary); display: grid; place-items: center;
font-size: 12.5px; font-weight: 600; }
.user-name { font-size: 13.5px; }
.menu-btn { display: none; width: 34px; height: 34px; border: 0; background: transparent; border-radius: 8px; cursor: pointer; color: var(--c-text-2); place-items: center; }
.content { flex: 1; min-width: 0; }
.mask { display: none; }
@media (max-width: 900px) {
.side { transform: translateX(-100%); box-shadow: var(--shadow-lg); }
.side.open { transform: none; }
.mask { display: block; position: fixed; inset: 0; background: rgba(17, 24, 39, 0.32); z-index: 35; }
.main { margin-left: 0; }
.menu-btn { display: grid; }
.top { padding: 0 12px; }
.user-name { display: none; }
}
</style>