261 lines
11 KiB
Vue
261 lines
11 KiB
Vue
<template>
|
|
<!-- 契约表格范式(#p-acct 单卡一体 1:1):筛选区 border-b+原生 table+分页条;入口页,行内编辑接 /channel/edit -->
|
|
<div>
|
|
<div class="page-header flex items-end justify-between">
|
|
<div>
|
|
<p class="page-description">{{ total }} 条通道 · {{ runningHint }}</p>
|
|
</div>
|
|
<router-link to="/channel/edit" class="btn btn-primary">+ 新建通道</router-link>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<!-- 筛选区 -->
|
|
<div class="flex flex-wrap items-center gap-2 border-b border-gray-100 p-4 dark:border-dark-700">
|
|
<input v-model="filterName" class="input !w-56" placeholder="搜索通道名称 / 接入码" @keyup.enter="search" />
|
|
<Select v-model="filterProtocol" :options="PROTOCOL_OPTIONS" placeholder="协议:全部" clearable class="!w-32" />
|
|
<Select v-model="filterStatus" :options="STATUS_OPTIONS" placeholder="状态:全部" clearable class="!w-32" />
|
|
<button class="btn btn-primary" @click="search">搜索</button>
|
|
<button class="btn btn-secondary" @click="reset">重置</button>
|
|
<span class="ml-auto text-xs text-gray-400">已选 {{ selected.size }} 项</span>
|
|
</div>
|
|
|
|
<!-- 表格 -->
|
|
<div class="table-container m-4 mt-0 border-0">
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th><input type="checkbox" class="accent-primary-600" :checked="allChecked" @change="toggleAll" /></th>
|
|
<th>通道</th><th>协议</th><th>接入地址</th>
|
|
<th class="text-right">速率 / 连接</th><th>上家</th>
|
|
<th>状态</th><th>操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-for="c in rows" :key="c.id" :class="selected.has(c.id) ? 'bg-gray-50 dark:bg-dark-800/30' : ''">
|
|
<td><input type="checkbox" class="accent-primary-600" :checked="selected.has(c.id)" @change="toggle(c.id)" /></td>
|
|
<td>
|
|
<span class="flex items-center gap-2 font-medium" :class="c.status === 1 ? 'text-gray-900 dark:text-white' : 'text-gray-500'">
|
|
<span class="flex h-7 w-7 items-center justify-center rounded-lg text-xs font-bold" :class="badgeClass(c)">{{ protoShort(c.protocol) }}</span>
|
|
{{ c.name }} <span class="text-xs text-gray-400">{{ c.id }}</span>
|
|
</span>
|
|
</td>
|
|
<td><span class="badge badge-gray">{{ protoLabel(c) }}</span></td>
|
|
<td><span class="font-mono text-xs text-gray-500">{{ c.host }}:{{ c.port }}</span></td>
|
|
<td class="text-right font-medium">{{ c.speed }} 条/s <span class="text-xs text-gray-400">· {{ c.conn_count }} 连接</span></td>
|
|
<td class="text-gray-500">{{ c.upstream_name || '—' }}</td>
|
|
<td><span class="badge" :class="c.status === 1 ? 'badge-success' : 'badge-gray'">{{ c.status === 1 ? '在营' : '停用' }}</span></td>
|
|
<td class="text-right">
|
|
<router-link :to="`/channel/edit/${c.id}`" class="cursor-pointer text-sm text-primary-600 hover:underline">编辑</router-link>
|
|
<button class="cursor-pointer text-sm text-primary-600 hover:underline" @click="openTest(c)">测试</button>
|
|
<button class="cursor-pointer text-sm hover:underline" :class="c.status === 1 ? 'text-amber-600' : 'text-emerald-600'" @click="toggleStatus(c)">
|
|
{{ c.status === 1 ? '停用' : '启用' }}
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
<tr v-if="!loading && rows.length === 0">
|
|
<td colspan="8" class="py-10 text-center text-sm text-gray-400">无匹配通道</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
|
|
<!-- 分页条 -->
|
|
<div class="flex items-center justify-between border-t border-gray-100 px-4 py-3 text-sm text-gray-500 dark:border-dark-700">
|
|
<span>共 {{ total }} 条记录</span>
|
|
<div class="flex items-center gap-1">
|
|
<button class="rounded-xl px-3.5 py-2 text-sm text-gray-500 hover:bg-gray-100 dark:hover:bg-dark-800" :disabled="page <= 1" @click="goPage(page - 1)">◀</button>
|
|
<button v-for="p in pageButtons" :key="p" class="rounded-xl px-3.5 py-2 text-sm" :class="p === page ? 'bg-primary-600 font-medium text-white' : 'text-gray-500 hover:bg-gray-100 dark:hover:bg-dark-800'" @click="goPage(p)">{{ p }}</button>
|
|
<span v-if="showEllipsis" class="px-1 text-gray-400">…</span>
|
|
<button class="rounded-xl px-3.5 py-2 text-sm text-gray-500 hover:bg-gray-100 dark:hover:bg-dark-800" :disabled="page >= lastPage" @click="goPage(page + 1)">▶</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 测试发送弹窗(契约 dialog 范式:dialog-overlay/container/header/body/footer) -->
|
|
<div v-if="testTarget" class="dialog-overlay" @click.self="closeTest">
|
|
<div class="dialog-container max-w-md">
|
|
<div class="dialog-header">
|
|
<h3 class="text-lg font-semibold text-gray-900 dark:text-white">测试发送</h3>
|
|
<button class="btn-ghost btn-icon" @click="closeTest"><svg class="h-5 w-5"><use href="#i-x" /></svg></button>
|
|
</div>
|
|
<div class="dialog-body space-y-4">
|
|
<p class="text-xs text-gray-500">通道 {{ testTarget.name }} · {{ protoLabel(testTarget) }} · 真实下发一条测试短信</p>
|
|
<div>
|
|
<label class="input-label">接收手机号 <span class="text-red-500">*</span></label>
|
|
<input v-model="testMobile" class="input" placeholder="13800138000" />
|
|
</div>
|
|
<div>
|
|
<label class="input-label">短信内容 <span class="text-red-500">*</span></label>
|
|
<textarea v-model="testContent" rows="3" class="input" placeholder="【签名】测试内容"></textarea>
|
|
</div>
|
|
<div v-if="testResult" class="rounded-xl border-l-4 p-3 text-sm" :class="testResult.ok ? 'border-emerald-500 bg-emerald-50 text-emerald-700 dark:bg-emerald-950/40 dark:text-emerald-300' : 'border-red-500 bg-red-50 text-red-700 dark:bg-red-950/40 dark:text-red-300'">
|
|
{{ testResult.msg }}
|
|
</div>
|
|
</div>
|
|
<div class="dialog-footer">
|
|
<button class="btn btn-secondary" @click="closeTest">取消</button>
|
|
<button class="btn btn-primary" :disabled="testing" @click="sendTest">{{ testing ? '发送中…' : '发送' }}</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, onMounted, reactive, ref } from 'vue'
|
|
import Select from '@/components/common/Select.vue'
|
|
import { channelApi, type Channel } from '@/api/channel'
|
|
|
|
// Issue#2 keep-alive 锚点:组件名=路由名
|
|
defineOptions({ name: 'channel' })
|
|
|
|
const rows = ref<Channel[]>([])
|
|
const total = ref(0)
|
|
const page = ref(1)
|
|
const pageSize = 10
|
|
const loading = ref(false)
|
|
const selected = reactive(new Set<number>())
|
|
|
|
const PROTOCOL_OPTIONS = [
|
|
{ value: 'cmpp', label: 'CMPP' },
|
|
{ value: 'smpp', label: 'SMPP' },
|
|
{ value: 'sgip', label: 'SGIP' },
|
|
{ value: 'http', label: 'HTTP' },
|
|
]
|
|
const STATUS_OPTIONS = [
|
|
{ value: '1', label: '在营' },
|
|
{ value: '0', label: '停用' },
|
|
]
|
|
const filterName = ref('')
|
|
const filterProtocol = ref('')
|
|
const filterStatus = ref('')
|
|
const query = reactive({ name: '', protocol: '', status: '' })
|
|
|
|
const lastPage = computed(() => Math.max(1, Math.ceil(total.value / pageSize)))
|
|
const allChecked = computed(() => rows.value.length > 0 && rows.value.every((c) => selected.has(c.id)))
|
|
const showEllipsis = computed(() => lastPage.value > 5)
|
|
const pageButtons = computed(() => {
|
|
const last = lastPage.value
|
|
const btns: number[] = []
|
|
const from = Math.max(1, Math.min(page.value - 1, last - 4))
|
|
for (let p = from; p <= Math.min(last, from + 4); p++) btns.push(p)
|
|
return btns
|
|
})
|
|
const runningHint = computed(() => `${rows.value.filter((c) => c.status === 1).length} 在营(本页)`)
|
|
|
|
async function load() {
|
|
loading.value = true
|
|
try {
|
|
const res = await channelApi.list({
|
|
page: page.value,
|
|
page_size: pageSize,
|
|
...(query.name ? { name: query.name } : {}),
|
|
...(query.protocol ? { protocol: query.protocol } : {}),
|
|
...(query.status !== '' ? { status: query.status } : {}),
|
|
})
|
|
rows.value = res.list
|
|
total.value = res.total
|
|
} catch {
|
|
rows.value = []
|
|
total.value = 0
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
function search() {
|
|
query.name = filterName.value.trim()
|
|
query.protocol = filterProtocol.value
|
|
query.status = filterStatus.value
|
|
page.value = 1
|
|
void load()
|
|
}
|
|
|
|
function reset() {
|
|
filterName.value = ''
|
|
filterProtocol.value = ''
|
|
filterStatus.value = ''
|
|
search()
|
|
}
|
|
|
|
function goPage(p: number) {
|
|
if (p < 1 || p > lastPage.value) return
|
|
page.value = p
|
|
void load()
|
|
}
|
|
|
|
function toggle(id: number) {
|
|
if (selected.has(id)) selected.delete(id)
|
|
else selected.add(id)
|
|
}
|
|
function toggleAll() {
|
|
if (allChecked.value) rows.value.forEach((c) => selected.delete(c.id))
|
|
else rows.value.forEach((c) => selected.add(c.id))
|
|
}
|
|
|
|
async function toggleStatus(c: Channel) {
|
|
const next = c.status === 1 ? 0 : 1
|
|
try {
|
|
await channelApi.update(c.id, { status: next })
|
|
c.status = next
|
|
} catch (e) {
|
|
testResult.value = { ok: false, msg: e instanceof Error ? e.message : '操作失败' }
|
|
}
|
|
}
|
|
|
|
// ---- 测试发送弹窗 ----
|
|
const testTarget = ref<Channel | null>(null)
|
|
const testMobile = ref('')
|
|
const testContent = ref('')
|
|
const testing = ref(false)
|
|
const testResult = ref<{ ok: boolean; msg: string } | null>(null)
|
|
|
|
function openTest(c: Channel) {
|
|
testTarget.value = c
|
|
testMobile.value = ''
|
|
testContent.value = '【测试】通道连通性验证'
|
|
testResult.value = null
|
|
}
|
|
function closeTest() {
|
|
testTarget.value = null
|
|
}
|
|
async function sendTest() {
|
|
if (!testTarget.value || !/^1\d{10}$/.test(testMobile.value.trim())) {
|
|
testResult.value = { ok: false, msg: '请输入正确的 11 位手机号' }
|
|
return
|
|
}
|
|
if (!testContent.value.trim()) {
|
|
testResult.value = { ok: false, msg: '短信内容必填' }
|
|
return
|
|
}
|
|
testing.value = true
|
|
testResult.value = null
|
|
try {
|
|
const r = await channelApi.test(testTarget.value.id, { mobile: testMobile.value.trim(), content: testContent.value.trim() })
|
|
testResult.value = { ok: true, msg: `已提交 · MsgID ${r.message_id} · 协议 ${r.protocol} · 耗时 ${r.elapsed_ms}ms` }
|
|
} catch (e) {
|
|
testResult.value = { ok: false, msg: e instanceof Error ? e.message : '发送失败' }
|
|
} finally {
|
|
testing.value = false
|
|
}
|
|
}
|
|
|
|
// ---- 展示工具 ----
|
|
function protoShort(p: string): string {
|
|
return ({ cmpp: 'CM', smpp: 'SM', http: 'HT', sgip: 'SG' } as Record<string, string>)[p] ?? p.slice(0, 2).toUpperCase()
|
|
}
|
|
function protoLabel(c: Channel): string {
|
|
const ver = c.protocol === 'cmpp' ? (c.protocol_version === 30 ? ' 3.0' : ' 2.0') : c.protocol === 'sgip' ? ' 1.2' : ''
|
|
return (c.protocol + ver).toUpperCase()
|
|
}
|
|
const BADGE = ['bg-primary-100 text-primary-700', 'bg-emerald-100 text-emerald-700', 'bg-amber-100 text-amber-700', 'bg-sky-100 text-sky-700']
|
|
function badgeClass(c: Channel): string {
|
|
if (c.status !== 1) return 'bg-gray-100 text-gray-500'
|
|
return BADGE[c.id % BADGE.length] ?? BADGE[0]!
|
|
}
|
|
|
|
onMounted(() => void load())
|
|
</script>
|
|
|