feat(web): integrate command balance rail

This commit is contained in:
yibaiba committed 2026-08-13 20:59:47 +08:00
1 parent c187788d74
commit cb7cfdac62
5 files changed
+248 -123

No files matched your search

+30 -29
View File
@@ -1,14 +1,16 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { nextTick, ref, watch } from 'vue'
import BalancePanel from './BalancePanel.vue'
import type { DeviceMgmtListItem } from '../../types/api'
import type { BalanceQuery } from '../../types/commands'
import type { BalanceQuery, CarrierQueryRule } from '../../types/commands'
defineProps<{
const props = defineProps<{
modelValue: boolean
devices: DeviceMgmtListItem[]
selectedDevice: string
queries: BalanceQuery[]
builtInRules: CarrierQueryRule[]
customRules: CarrierQueryRule[]
loading: boolean
querying: boolean
}>()
@@ -20,47 +22,46 @@ const emit = defineEmits<{
editRules: []
}>()
const narrowViewport = ref(false)
const direction = computed<'btt' | 'rtl'>(() => narrowViewport.value ? 'btt' : 'rtl')
const size = computed(() => narrowViewport.value ? '70%' : '400px')
let mediaQuery: MediaQueryList | null = null
const rail = ref<HTMLElement | null>(null)
onMounted(() => {
mediaQuery = window.matchMedia('(max-width: 640px)')
narrowViewport.value = mediaQuery.matches
mediaQuery.addEventListener('change', updateViewport)
watch(() => props.modelValue, async (open) => {
if (!open) return
await nextTick()
rail.value?.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
rail.value?.focus({ preventScroll: true })
emit('update:modelValue', false)
})
onUnmounted(() => mediaQuery?.removeEventListener('change', updateViewport))
function updateViewport(event: MediaQueryListEvent) {
narrowViewport.value = event.matches
}
</script>
<template>
<el-drawer
:model-value="modelValue"
class="balance-drawer"
:with-header="false"
:direction="direction"
:size="size"
append-to-body
@update:model-value="emit('update:modelValue', $event)"
>
<aside id="command-balance-rail" ref="rail" class="balance-rail" tabindex="-1">
<BalancePanel
:selected-device="selectedDevice"
:devices="devices"
:queries="queries"
:built-in-rules="builtInRules"
:custom-rules="customRules"
:loading="loading"
:querying="querying"
@update:selected-device="emit('update:selectedDevice', $event)"
@query="emit('query')"
@edit-rules="emit('editRules')"
/>
</el-drawer>
</aside>
</template>
<style>
.balance-drawer .el-drawer__body { padding: 0; overflow: hidden; }
<style scoped>
.balance-rail {
min-width: 0;
min-height: 0;
overflow: hidden;
border-left: 1px solid var(--ui-border);
background: color-mix(in srgb, var(--ui-surface-strong) 72%, var(--ui-surface));
outline: none;
}
.balance-rail:focus-visible { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--ui-primary) 34%, transparent); }
@media (max-width: 1023px) {
.balance-rail { min-height: 560px; border-top: 1px solid var(--ui-border); border-left: 0; }
}
@media (prefers-reduced-motion: reduce) { .balance-rail { scroll-behavior: auto; } }
</style>
+20 -18
View File
@@ -1,33 +1,35 @@
<script setup lang="ts">
import type { BalanceQuery } from '../../types/commands'
import { balanceResultText, presentBalanceState } from '../../utils/commandPresentation'
import { Wallet24Regular } from '@vicons/fluent'
import {
balanceResultText,
balanceTransportLabel,
presentBalanceState
} from '../../utils/commandPresentation'
defineProps<{ query: BalanceQuery }>()
</script>
<template>
<div class="balance-message" :class="query.state">
<div class="balance-heading">
<span><el-icon><Wallet24Regular /></el-icon>运营商余额</span>
<span class="balance-state">{{ presentBalanceState(query).label }}</span>
<div class="balance-message" :class="`tone-${presentBalanceState(query).tone}`">
<div class="balance-result-row">
<strong>{{ balanceResultText(query) }}</strong>
<span>{{ presentBalanceState(query).label }}</span>
</div>
<strong>{{ balanceResultText(query) }}</strong>
<span class="balance-device">{{ query.device_id }}</span>
<span class="balance-device">{{ query.device_id }} · {{ balanceTransportLabel(query) }}</span>
<pre v-if="query.raw_response">{{ query.raw_response }}</pre>
<p v-if="query.error">{{ query.error }}</p>
</div>
</template>
<style scoped>
.balance-message { min-width: 0; padding-top: 4px; display: grid; gap: 7px; }
.balance-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; color: #0f766e; font-size: 12px; }
.balance-heading > span:first-child { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.balance-state { color: #64748b; font-size: 11px; }
.balance-message strong { font-size: 14px; overflow-wrap: anywhere; }
.balance-device { color: #64748b; font: 11px "v-mono", monospace; }
.balance-message pre { margin: 1px 0 0; padding-top: 8px; border-top: 1px solid var(--ui-border); font: 12px/1.5 "v-mono", monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
.balance-message p { margin: 0; color: #dc2626; font-size: 12px; overflow-wrap: anywhere; }
.balance-message.failed .balance-heading, .balance-message.timed_out .balance-heading { color: #dc2626; }
.balance-message { min-width: 0; padding-top: 5px; display: grid; gap: 6px; color: var(--ui-primary); }
.balance-result-row { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.balance-result-row strong { color: currentColor; font-size: 16px; overflow-wrap: anywhere; }
.balance-result-row span { color: currentColor; font-size: 10px; }
.balance-device { color: var(--ui-text-subtle); font: 10px "v-mono", monospace; }
.balance-message pre { margin: 1px 0 0; padding-top: 7px; border-top: 1px solid var(--ui-border); color: var(--ui-text-muted); font: 11px/1.5 "v-mono", monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
.balance-message p { margin: 0; color: var(--ui-danger); font-size: 11px; overflow-wrap: anywhere; }
.tone-waiting, .tone-running { color: var(--ui-warning); }
.tone-parsed { color: var(--ui-info); }
.tone-danger { color: var(--ui-danger); }
</style>
+162 -57
View File
@@ -1,14 +1,30 @@
<script setup lang="ts">
import type { BalanceQuery } from '../../types/commands'
import { computed } from 'vue'
import type { DeviceMgmtListItem } from '../../types/api'
import type { BalanceQuery, CarrierQueryRule } from '../../types/commands'
import { isControlOnline } from '../../utils/deviceLifecycle'
import { formatDeviceDateTime } from '../../utils/deviceTime'
import { balanceResultText, presentBalanceState } from '../../utils/commandPresentation'
import { Edit24Regular, Wallet24Regular } from '@vicons/fluent'
import {
balanceResultText,
balanceTransportLabel,
presentBalanceState
} from '../../utils/commandPresentation'
import {
CheckmarkCircle24Regular,
Clock24Regular,
Edit24Regular,
ErrorCircle24Regular,
Info24Regular,
Phone24Regular,
Wallet24Regular
} from '@vicons/fluent'
defineProps<{
const props = defineProps<{
devices: DeviceMgmtListItem[]
selectedDevice: string
queries: BalanceQuery[]
builtInRules: CarrierQueryRule[]
customRules: CarrierQueryRule[]
loading: boolean
querying: boolean
}>()
@@ -19,75 +35,164 @@ const emit = defineEmits<{
editRules: []
}>()
const selectedQueries = computed(() => [...props.queries
.filter((query) => !props.selectedDevice || query.device_id === props.selectedDevice)]
.sort((left, right) => Date.parse(right.updated_at) - Date.parse(left.updated_at)))
const latestQuery = computed(() => selectedQueries.value[0])
const visibleRules = computed(() => [...props.customRules, ...props.builtInRules]
.filter((rule) => rule.enabled)
.slice(0, 4))
function deviceLabel(device: DeviceMgmtListItem): string {
return `${device.name || device.id} · ${isControlOnline(device) ? '在线' : '离线'}`
}
function ruleRoute(rule: CarrierQueryRule): string {
if (rule.transport === 'unsupported') return rule.alternative || '不支持自动查询'
const payload = rule.payload || '未提供内容'
const destination = rule.destination || (rule.response_mode === 'direct' ? '直接返回' : '未提供目标')
return `${payload} → ${destination}`
}
</script>
<template>
<aside class="balance-panel" aria-label="运营商余额">
<div class="panel-heading">
<section class="balance-panel" aria-label="运营商余额">
<header class="panel-heading">
<span class="panel-icon" aria-hidden="true"><el-icon><Wallet24Regular /></el-icon></span>
<div>
<h3>运营商余额</h3>
<h2>余额查询</h2>
<span>真实运营商回复与解析记录</span>
</div>
<el-tooltip content="编辑运营商规则" placement="left">
<el-button text aria-label="编辑运营商规则" @click="emit('editRules')">
<el-icon><Edit24Regular /></el-icon>
</el-button>
</el-tooltip>
</div>
</header>
<div class="query-controls">
<el-select
:model-value="selectedDevice"
placeholder="选择设备"
class="device-select"
@update:model-value="emit('update:selectedDevice', String($event || ''))"
>
<el-option v-for="device in devices" :key="device.id" :label="device.name || device.id" :value="device.id" />
</el-select>
<el-button type="primary" :loading="querying" :disabled="!selectedDevice" @click="emit('query')">
查询
</el-button>
<label for="balance-device">选择设备</label>
<div class="query-row">
<el-select
id="balance-device"
:model-value="selectedDevice"
placeholder="选择设备"
class="device-select"
aria-label="余额查询设备"
@update:model-value="emit('update:selectedDevice', String($event || ''))"
>
<template #prefix><el-icon><Phone24Regular /></el-icon></template>
<el-option v-for="device in devices" :key="device.id" :label="deviceLabel(device)" :value="device.id" />
</el-select>
<el-button type="primary" :loading="querying" :disabled="!selectedDevice" @click="emit('query')">
查询
</el-button>
</div>
</div>
<div class="balance-list" aria-live="polite">
<div v-if="loading" class="balance-empty">正在读取余额记录</div>
<div v-else-if="!queries.length" class="balance-empty">
<el-icon><Wallet24Regular /></el-icon>
<span>暂无查询记录</span>
</div>
<article v-for="query in queries" :key="query.id" class="balance-item">
<div class="balance-row">
<strong>{{ balanceResultText(query) }}</strong>
<span class="query-state" :class="presentBalanceState(query).tone">
{{ presentBalanceState(query).label }}
<section class="latest-result" aria-label="最新余额结果">
<span class="section-label">最新结果</span>
<template v-if="latestQuery">
<strong>{{ balanceResultText(latestQuery) }}</strong>
<span class="latest-meta">
<el-icon><Clock24Regular /></el-icon>
{{ formatDeviceDateTime(latestQuery.updated_at) }} · 来源 {{ balanceTransportLabel(latestQuery) }}
</span>
</template>
<span v-else class="latest-empty">当前设备暂无余额记录</span>
</section>
<section class="history-section" aria-label="余额历史">
<h3>历史记录 <span>{{ selectedQueries.length }}</span></h3>
<div class="balance-history" aria-live="polite">
<div v-if="loading" class="balance-empty">正在读取余额记录</div>
<div v-else-if="!selectedQueries.length" class="balance-empty">暂无查询记录</div>
<article v-for="query in selectedQueries" :key="query.id" class="balance-item">
<span class="history-icon" :class="presentBalanceState(query).tone" aria-hidden="true">
<el-icon v-if="presentBalanceState(query).tone === 'danger'"><ErrorCircle24Regular /></el-icon>
<el-icon v-else-if="['running', 'waiting'].includes(presentBalanceState(query).tone)"><Clock24Regular /></el-icon>
<el-icon v-else><CheckmarkCircle24Regular /></el-icon>
</span>
</div>
<div class="balance-meta">
<span>{{ query.device_id }}</span>
<time>{{ formatDeviceDateTime(query.updated_at) }}</time>
</div>
<pre v-if="query.raw_response">{{ query.raw_response }}</pre>
<p v-if="query.error" class="query-error">{{ query.error }}</p>
</article>
</div>
</aside>
<div>
<b :class="`tone-${presentBalanceState(query).tone}`">{{ presentBalanceState(query).label }}</b>
<small>{{ query.device_id }} · {{ balanceTransportLabel(query) }}</small>
</div>
<span class="history-result">
{{ balanceResultText(query) }}
<small>{{ formatDeviceDateTime(query.updated_at) }}</small>
</span>
<pre v-if="query.raw_response">{{ query.raw_response }}</pre>
<p v-if="query.error" class="query-error">{{ query.error }}</p>
</article>
</div>
</section>
<section class="rules-section" aria-label="运营商规则">
<h3>运营商规则 <span>{{ builtInRules.length + customRules.length }}</span></h3>
<div v-if="visibleRules.length" class="rule-list">
<article v-for="rule in visibleRules" :key="rule.id">
<div>
<strong>{{ rule.operator || rule.id }}</strong>
<small>{{ rule.built_in ? '内置' : '自定义' }}</small>
</div>
<span>{{ ruleRoute(rule) }}</span>
<el-tooltip :content="`${rule.mcc}/${rule.mnc} · ${rule.id}`" placement="left">
<el-icon aria-label="规则详情"><Info24Regular /></el-icon>
</el-tooltip>
</article>
</div>
<button v-else type="button" class="rules-empty" @click="emit('editRules')">
暂无可用规则,打开规则编辑器
</button>
</section>
</section>
</template>
<style scoped>
.balance-panel { min-width: 0; min-height: 0; height: 100%; display: flex; flex-direction: column; }
.panel-heading { min-height: 58px; padding: 10px 12px 10px 14px; border-bottom: 1px solid var(--ui-border); display: flex; align-items: center; justify-content: space-between; }
.panel-heading h3 { margin: 0; font-size: 15px; letter-spacing: 0; }
.query-controls { padding: 12px; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; border-bottom: 1px solid var(--ui-border); }
.balance-panel { min-width: 0; min-height: 0; height: 100%; padding: 18px; overflow: auto; }
.panel-heading { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 10px; }
.panel-icon { width: 36px; height: 36px; border-radius: 6px; background: color-mix(in srgb, var(--ui-primary) 12%, transparent); color: var(--ui-primary); display: grid; place-items: center; }
.panel-icon .el-icon { font-size: 19px; }
.panel-heading h2 { margin: 0; color: var(--ui-text); font-size: 18px; }
.panel-heading div > span { color: var(--ui-text-subtle); font-size: 10px; }
.panel-heading :deep(.el-button) { width: 36px; height: 36px; padding: 0; }
.query-controls { display: grid; gap: 7px; margin: 18px 0 16px; }
.query-controls > label, .section-label { color: var(--ui-text-muted); font-size: 11px; }
.query-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 7px; }
.device-select { min-width: 0; }
.query-controls :deep(.el-input__wrapper), .query-controls :deep(.el-button) { min-height: 44px; }
.balance-list { min-height: 0; overflow: auto; }
.balance-item { padding: 13px 14px; border-bottom: 1px solid var(--ui-border); }
.balance-row, .balance-meta { display: flex; justify-content: space-between; gap: 8px; }
.balance-row strong { min-width: 0; font-size: 14px; overflow-wrap: anywhere; }
.balance-meta { margin-top: 5px; color: #94a3b8; font-size: 11px; }
.query-state { flex: 0 0 auto; color: #64748b; font-size: 11px; }
.query-state.completed { color: #15803d; }
.query-state.failed, .query-state.timed_out, .query-error { color: #dc2626; }
.balance-item pre { margin: 10px 0 0; padding: 8px; max-height: 130px; overflow: auto; border-radius: 6px; background: rgba(15, 23, 42, .05); font: 11px/1.5 "v-mono", monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
.query-error { margin: 7px 0 0; font-size: 11px; }
.balance-empty { min-height: 180px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: #94a3b8; font-size: 12px; }
.balance-empty .el-icon { font-size: 26px; color: #0d9488; }
.query-row :deep(.el-input__wrapper), .query-row :deep(.el-button) { min-height: 40px; border-radius: 4px; }
.latest-result { display: flex; flex-direction: column; gap: 6px; padding: 15px 0; border-block: 1px solid var(--ui-border); }
.latest-result > strong { color: var(--ui-primary); font-size: clamp(25px, 2.4vw, 36px); font-weight: 550; overflow-wrap: anywhere; }
.latest-meta { color: var(--ui-text-subtle); display: flex; align-items: center; gap: 5px; font-size: 10px; }
.latest-empty { min-height: 42px; color: var(--ui-text-subtle); display: flex; align-items: center; font-size: 11px; }
.balance-panel h3 { margin: 17px 0 7px; color: var(--ui-text); font-size: 13px; }
.balance-panel h3 span { margin-left: 4px; color: var(--ui-text-subtle); font-weight: 400; }
.balance-history, .rule-list { border-top: 1px solid var(--ui-border); }
.balance-item { display: grid; grid-template-columns: auto minmax(0, 1fr) minmax(90px, auto); gap: 8px; align-items: center; padding: 9px 0; border-bottom: 1px solid var(--ui-border); }
.history-icon { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; color: var(--ui-primary); background: color-mix(in srgb, currentColor 10%, transparent); }
.history-icon.waiting, .history-icon.running, .tone-waiting, .tone-running { color: var(--ui-warning); }
.history-icon.parsed, .tone-parsed { color: var(--ui-info); }
.history-icon.danger, .tone-danger, .query-error { color: var(--ui-danger); }
.balance-item b { font-size: 11px; }
.balance-item small { display: block; margin-top: 2px; color: var(--ui-text-subtle); font-size: 9px; }
.history-result { min-width: 0; color: var(--ui-text); font-size: 11px; text-align: right; overflow-wrap: anywhere; }
.balance-item pre, .balance-item .query-error { grid-column: 2 / -1; margin: 0; overflow-wrap: anywhere; }
.balance-item pre { max-height: 90px; overflow: auto; color: var(--ui-text-muted); font: 10px/1.5 "v-mono", monospace; white-space: pre-wrap; }
.query-error { font-size: 10px; }
.balance-empty { min-height: 90px; color: var(--ui-text-subtle); display: grid; place-items: center; font-size: 11px; }
.rule-list article { min-height: 48px; padding: 9px 8px; border: 1px solid var(--ui-border); border-top: 0; display: grid; grid-template-columns: minmax(90px, 1fr) minmax(0, auto) auto; align-items: center; gap: 8px; }
.rule-list article > div { min-width: 0; display: flex; align-items: center; gap: 5px; }
.rule-list strong { min-width: 0; color: var(--ui-text); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rule-list small { color: var(--ui-primary); font-size: 8px; }
.rule-list article > span { color: var(--ui-text-muted); font: 9px "v-mono", monospace; text-align: right; overflow-wrap: anywhere; }
.rule-list article > .el-icon { color: var(--ui-text-subtle); }
.rules-empty { width: 100%; min-height: 48px; border: 1px solid var(--ui-border); border-radius: 4px; background: transparent; color: var(--ui-text-muted); font-size: 11px; }
@media (max-width: 1023px) {
.balance-panel { min-height: 560px; }
.latest-result > strong { font-size: 34px; }
}
@media (max-width: 640px) {
.balance-panel { padding: 16px 12px 88px; }
.query-row :deep(.el-input__wrapper), .query-row :deep(.el-button) { min-height: 44px; }
}
</style>
+29 -15
View File
@@ -248,19 +248,20 @@ async function deleteRule(id: string) {
@submit="execute"
@dangerous="openDangerous"
/>
<BalanceDrawer
v-model="balanceOpen"
v-model:selected-device="selectedDevice"
:devices="devices"
:queries="balances"
:built-in-rules="builtInRules"
:custom-rules="customRules"
:loading="loading"
:querying="querying"
@query="startBalance"
@edit-rules="rulesOpen = true"
/>
</div>
<BalanceDrawer
v-model="balanceOpen"
v-model:selected-device="selectedDevice"
:devices="devices"
:queries="balances"
:loading="loading"
:querying="querying"
@query="startBalance"
@edit-rules="rulesOpen = true"
/>
<el-dialog
:model-value="!!dangerousDefinition"
:title="dangerousTitle"
@@ -300,12 +301,25 @@ async function deleteRule(id: string) {
</template>
<style scoped>
.commands-page { min-width: 0; }
.commands-layout { height: calc(100dvh - 112px); min-height: 520px; }
.commands-page { min-width: 0; min-height: 0; }
.commands-layout {
min-width: 0;
height: calc(100dvh - 112px);
min-height: 540px;
overflow: hidden;
border: 1px solid var(--ui-border);
border-radius: 8px;
background: var(--ui-surface);
display: grid;
grid-template-columns: minmax(0, 1fr) clamp(300px, 25vw, 360px);
}
.commands-layout :deep(.chat-shell) { border: 0; border-radius: 0; }
@media (max-width: 1023px) {
.commands-layout { height: 620px; min-height: 520px; }
.commands-layout { height: auto; min-height: 0; grid-template-columns: minmax(0, 1fr); overflow: visible; }
.commands-layout :deep(.chat-shell) { height: 690px; min-height: 690px; }
}
@media (max-width: 640px) {
.commands-layout { height: 620px; min-height: 480px; margin: 0 -4px; }
.commands-layout { margin: 0 -4px; }
.commands-layout :deep(.chat-shell) { height: 720px; min-height: 720px; }
}
</style>
+7 -4
View File
@@ -9,13 +9,16 @@ const commandTimeline = await readFile(new URL('../src/components/commands/Comma
const commandAudioPlayer = await readFile(new URL('../src/components/commands/CommandAudioPlayer.vue', import.meta.url), 'utf8')
const commandComposer = await readFile(new URL('../src/components/commands/CommandComposer.vue', import.meta.url), 'utf8')
test('command workspace keeps balance inside chat and a responsive history drawer', () => {
test('command workspace keeps an inline responsive balance rail beside the conversation', () => {
const chatIndex = commandsView.indexOf('<CommandChat')
const balanceIndex = commandsView.indexOf('<BalanceDrawer')
assert.ok(chatIndex >= 0, 'independent chat is present')
assert.match(commandsView, /<BalanceDrawer/)
assert.match(balanceDrawer, /<el-drawer[\s\S]*<BalancePanel/)
assert.match(balanceDrawer, /narrowViewport\.value \? 'btt' : 'rtl'/)
assert.ok(balanceIndex > chatIndex, 'balance rail follows chat inside the workspace')
assert.match(balanceDrawer, /class="balance-rail"/)
assert.match(balanceDrawer, /<BalancePanel/)
assert.doesNotMatch(balanceDrawer, /<el-drawer/)
assert.match(commandsView, /grid-template-columns:\s*minmax\(0, 1fr\) clamp\(300px, 25vw, 360px\)/)
assert.match(commandChat, /Wallet24Regular/)
assert.match(commandChat, /:events="visibleEvents"/)
assert.match(commandChat, /:balance-queries="visibleBalanceQueries"/)