拉取通道弹窗:一通道一行 + 打开即拉取

- 通道列表由三列网格改为单列(一行一个):通道号 / 通道名 / IP:端口 都完整显示,
  名字不再被 120px 的 max-width 截断(名字与地址都补了 title,窄窗时仍可悬停看全)
- 点表格里的「拉取通道」直接拉取:并行取「已导入通道 + 库里存的设备密码」,
  拿到密码立刻 pull 一次,不用再点第二次按钮;弹窗里的按钮改为「重新拉取」(换密码用)
- 没有存密码时给出提示并自动聚焦密码框,回车即拉
- 拉取中/关闭弹窗/切换 NVR 都有保护,不会把旧结果写进新窗口

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-10-21 14:39:50 +08:00
parent 000a45d6cb
commit 46c7d4f359

View File

@ -3,14 +3,14 @@
* NVR 录像机管理
*
* 两段式添加摄像头:
* 1. 填地址/账号 → 「拉取通道」:**页面直接调本机软件**(WebSocket 17301 的 `get_channels`),
* 走 8000 私有协议登录设备拿到通道列表;
* 1. 点「拉取通道」:**页面直接调本机软件**(WebSocket 17301 的 `get_channels`),
* 走 8000 私有协议登录设备拿到通道列表;已存过密码的直接就拉,不用再点一次按钮;
* 2. 勾选通道 → 提交到后端 `/api/web/nvrs/:id/channels` 落库成摄像头。
*
* 设备在用户内网,所以拉取只能由本机软件做;后端只存结果。
* 密码在后端只存密文、接口不返回,因此**每次拉取需要重新输入密码**。
* 密码在后端只存密文、接口不返回,因此**每次拉取需要重新输入密码**(库里有时自动带出)。
*/
import { onMounted, reactive, ref } from 'vue';
import { nextTick, onMounted, reactive, ref } from 'vue';
import AppLayout from '@/components/AppLayout.vue';
import DataPager from '@/components/DataPager.vue';
import ModalBox from '@/components/ModalBox.vue';
@ -127,6 +127,7 @@ const payload = ref<HikChannelsPayload | null>(null);
const channelError = ref('');
const selected = ref<Set<number>>(new Set());
const existingChannels = ref<Set<number>>(new Set());
const passwordInput = ref<HTMLInputElement | null>(null);
async function openChannels(nvr: Nvr) {
channelNvr.value = nvr;
@ -135,24 +136,33 @@ async function openChannels(nvr: Nvr) {
channelError.value = '';
selected.value = new Set();
existingChannels.value = new Set();
try {
const data = await fetchNvrCameras(nvr.id);
existingChannels.value = new Set(data.list.map(item => item.channelNo));
} catch {
// 已有通道读取失败不影响拉取
}
channelOpen.value = true;
// 库里已有密码时自动带出(密文存储 → 后端解密 → 直接给本机软件),
// 用户不用每次手输;想用其他密码也可以在输入框里覆盖。
try {
const cred = await fetchNvrCredential(nvr.id);
if (cred.hasPassword && cred.password) channelPassword.value = cred.password;
} catch {
// 取不到就保持为空,让用户手填
// 已导入的通道 + 库里存的密码一起取:拿到密码就直接拉,用户不用再点一次「拉取通道」
const [, cred] = await Promise.all([
fetchNvrCameras(nvr.id)
.then(data => {
existingChannels.value = new Set(data.list.map(item => item.channelNo));
})
.catch(() => {
// 已有通道读取失败不影响拉取
}),
// 密文存储 → 后端解密 → 直接给本机软件;想换密码可以在输入框里覆盖
fetchNvrCredential(nvr.id).catch(() => null),
]);
// 期间用户可能已经关窗/换了另一台,别把结果写乱
if (!channelOpen.value || channelNvr.value !== nvr) return;
if (cred?.hasPassword && cred.password) {
channelPassword.value = cred.password;
await pull();
return;
}
channelError.value = '这台录像机还没有保存密码,请填写设备密码后回车(或点「拉取通道」)。';
await nextTick();
passwordInput.value?.focus();
}
/** 拉一次通道并把返回值写进弹窗状态(失败直接抛,由 pull() 统一处理) */
@ -364,7 +374,7 @@ onMounted(() => load(1));
</ModalBox>
<!-- 拉取通道 -->
<ModalBox :open="channelOpen" title="拉取通道" width="720px" @close="channelOpen = false">
<ModalBox :open="channelOpen" title="拉取通道" width="820px" @close="channelOpen = false">
<div v-if="channelNvr" class="pull-head">
<div>
<strong>{{ channelNvr.name || channelNvr.host }}</strong>
@ -372,8 +382,9 @@ onMounted(() => load(1));
<span class="hint">账号 {{ channelNvr.username }}</span>
</div>
<div class="pull-form">
<input v-model="channelPassword" type="password" placeholder="设备密码(新增时已保存,可覆盖)" @keyup.enter="pull" />
<button class="btn" :disabled="pulling" @click="pull">{{ pulling ? '拉取中…' : '拉取通道' }}</button>
<input ref="passwordInput" v-model="channelPassword" type="password"
placeholder="设备密码(新增时已保存,可覆盖)" @keyup.enter="pull" />
<button class="btn" :disabled="pulling" @click="pull">{{ pulling ? '拉取中…' : '重新拉取' }}</button>
</div>
</div>
@ -393,7 +404,7 @@ onMounted(() => load(1));
<span class="hint">已选 {{ selected.size }} 个</span>
</div>
<div class="channel-grid">
<div class="channel-list">
<label v-for="item in payload.data.channels" :key="item.channel"
:class="['channel-item', { added: existingChannels.has(item.channel), off: !item.enabled }]">
<input type="checkbox"
@ -401,8 +412,9 @@ onMounted(() => load(1));
:disabled="existingChannels.has(item.channel)"
@change="toggle(item.channel)" />
<span class="ch-no">通道 {{ item.channel }}</span>
<span :class="['ch-name', { 'ch-name-empty': !item.name }]">{{ (item.name || '').trim() || '未命名' }}</span>
<span class="ch-ip">{{ cameraLabel(item) }}</span>
<span :class="['ch-name', { 'ch-name-empty': !item.name }]"
:title="(item.name || '').trim() || '未命名'">{{ (item.name || '').trim() || '未命名' }}</span>
<span class="ch-ip" :title="cameraLabel(item)">{{ cameraLabel(item) }}</span>
<span v-if="!item.enabled" class="badge badge-off">未配置</span>
<span v-else-if="existingChannels.has(item.channel)" class="badge">已添加</span>
</label>
@ -483,11 +495,12 @@ onMounted(() => load(1));
margin: 10px 0;
}
.channel-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
gap: 8px;
max-height: 320px;
/* 一个通道一行:名字/地址完整显示,不再挤成三列被省略 */
.channel-list {
display: flex;
flex-direction: column;
gap: 6px;
max-height: 360px;
overflow: auto;
padding: 2px;
}
@ -495,14 +508,19 @@ onMounted(() => load(1));
.channel-item {
display: flex;
align-items: center;
gap: 8px;
padding: 7px 10px;
gap: 10px;
padding: 8px 12px;
border: 1px solid #e5e7eb;
border-radius: 8px;
font-size: 13px;
cursor: pointer;
}
.channel-item:hover {
border-color: #cbd5e1;
background: #f8fafc;
}
.channel-item.added {
background: #f8fafc;
color: #94a3b8;
@ -515,19 +533,23 @@ onMounted(() => load(1));
.ch-no {
font-weight: 600;
flex: 0 0 72px;
font-variant-numeric: tabular-nums;
}
.ch-ip {
color: #64748b;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
flex: 0 0 auto;
margin-left: auto;
}
.ch-name {
flex: 1 1 auto;
min-width: 0;
color: #334155;
font-size: 12px;
max-width: 120px;
font-size: 13px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;