Files
one-pipe-system/src/views/system/carrier-traffic-threshold-hits/index.vue
luo f896cad34f
All checks were successful
构建并部署前端到测试环境 / deploy (push) Successful in 55s
fix: 选择今天00:00:00-23:59:59
2026-09-29 11:36:41 +08:00

422 lines
13 KiB
Vue

<template>
<ArtTableFullScreen>
<div class="carrier-threshold-hit-page" id="table-full-screen">
<ElResult
v-if="!canAccess"
icon="warning"
title="无访问权限"
sub-title="仅超级管理员和平台账号可查看通道阈值命中记录"
/>
<template v-else>
<ArtSearchBar
v-model:filter="searchForm"
:items="searchFormItems"
label-width="100"
show-expand
@reset="handleReset"
@search="handleSearch"
/>
<ElCard shadow="never" class="art-table-card">
<ArtTableHeader
:columnList="columnOptions"
v-model:columns="columnChecks"
@refresh="handleRefresh"
/>
<ArtTable
:loading="loading"
:data="hitList"
:currentPage="pagination.page"
:pageSize="pagination.page_size"
:total="pagination.total"
:marginTop="10"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
>
<template #default>
<ElTableColumn v-for="col in columns" :key="col.prop || col.type" v-bind="col" />
</template>
</ArtTable>
</ElCard>
</template>
</div>
</ArtTableFullScreen>
</template>
<script setup lang="ts">
import { computed, h, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElTag } from 'element-plus'
import { CarrierService, CardService, CarrierTrafficThresholdHitService } from '@/api/modules'
import { usePermission } from '@/composables/usePermission'
import { useCheckedColumns } from '@/composables/useCheckedColumns'
import { formatDateTime } from '@/utils/business/format'
import type { SearchFormItem } from '@/types'
import type {
Carrier,
CarrierTrafficThresholdHitItem,
CarrierTrafficThresholdHitQueryParams,
StandaloneCardQueryParams,
StandaloneIotCard
} from '@/types/api'
defineOptions({ name: 'CarrierTrafficThresholdHits' })
const { isPlatformAccount } = usePermission()
const canAccess = computed(() => isPlatformAccount.value)
const loading = ref(false)
const carrierOptionsLoading = ref(false)
const cardOptionsLoading = ref(false)
const carrierOptions = ref<Carrier[]>([])
const cardOptions = ref<StandaloneIotCard[]>([])
const hitList = ref<CarrierTrafficThresholdHitItem[]>([])
const pagination = reactive({
page: 1,
page_size: 20,
total: 0
})
const initialSearchState = {
carrier_id: undefined as number | undefined,
card_id: undefined as number | undefined,
period_start: '',
judged_at_range: [] as string[],
start_time: '',
end_time: ''
}
const searchForm = ref({ ...initialSearchState })
const preserveSelectedOption = <T extends { id: number }>(
results: T[],
currentOptions: T[],
selectedId?: number
): T[] => {
if (!selectedId || results.some((item) => item.id === selectedId)) return results
const selectedOption = currentOptions.find((item) => item.id === selectedId)
return selectedOption ? [selectedOption, ...results.slice(0, 19)] : results
}
const searchCarriers = async (keyword = '') => {
if (!canAccess.value) return
carrierOptionsLoading.value = true
try {
const res = await CarrierService.getCarriers({
page: 1,
page_size: 20,
carrier_name: keyword.trim() || undefined
})
if (res.code === 0) {
carrierOptions.value = preserveSelectedOption(
res.data.items || [],
carrierOptions.value,
searchForm.value.carrier_id
)
}
} catch (error) {
console.error('搜索运营商通道失败:', error)
} finally {
carrierOptionsLoading.value = false
}
}
const searchCards = async (keyword = '') => {
if (!canAccess.value) return
cardOptionsLoading.value = true
try {
const params: StandaloneCardQueryParams = {
page: 1,
page_size: 20,
iccid: keyword.trim() || undefined
}
const res = await CardService.getStandaloneIotCards(params)
if (res.code === 0) {
cardOptions.value = preserveSelectedOption(
res.data.items || [],
cardOptions.value,
searchForm.value.card_id
)
}
} catch (error) {
console.error('搜索IoT卡失败:', error)
} finally {
cardOptionsLoading.value = false
}
}
const searchFormItems = computed<SearchFormItem[]>(() => [
{
label: '运营商通道',
prop: 'carrier_id',
type: 'select',
config: {
clearable: true,
filterable: true,
remote: true,
reserveKeyword: true,
remoteMethod: searchCarriers,
loading: carrierOptionsLoading.value,
placeholder: '输入运营商名称搜索'
},
options: () =>
carrierOptions.value.map((carrier) => ({
label: carrier.carrier_name,
value: carrier.id
}))
},
{
label: 'IoT卡',
prop: 'card_id',
type: 'select',
config: {
clearable: true,
filterable: true,
remote: true,
reserveKeyword: true,
remoteMethod: searchCards,
loading: cardOptionsLoading.value,
placeholder: '输入ICCID搜索'
},
options: () =>
cardOptions.value.map((card) => ({
label: card.iccid,
value: card.id
}))
},
{
label: '计费周期起点',
prop: 'period_start',
type: 'datetime',
config: {
type: 'datetime',
format: 'YYYY-MM-DD HH:mm:ss',
valueFormat: 'YYYY-MM-DDTHH:mm:ssZ',
placeholder: '请选择周期起点'
}
},
{
label: '判定时间',
prop: 'judged_at_range',
type: 'datetimerange',
config: {
type: 'datetimerange',
format: 'YYYY-MM-DD HH:mm:ss',
valueFormat: 'YYYY-MM-DDTHH:mm:ssZ',
rangeSeparator: '至',
startPlaceholder: '开始时间',
endPlaceholder: '结束时间'
}
}
])
const formatNullableNumber = (value: number | null | undefined, unit = '') => {
if (value === null || value === undefined) return '无'
const formatted = Number(value).toLocaleString('zh-CN', { maximumFractionDigits: 2 })
return unit ? `${formatted} ${unit}` : formatted
}
const formatThresholdValue = (row: CarrierTrafficThresholdHitItem) => {
if (row.hit_threshold_value === null || row.hit_threshold_value === undefined) return '无'
const value = formatNullableNumber(row.hit_threshold_value)
return row.hit_threshold_unit ? `${value} ${row.hit_threshold_unit}` : `${value}(单位:无)`
}
const statusMap: Record<string, { label: string; type: 'warning' | 'success' | 'info' }> = {
locked: { label: '持锁中', type: 'warning' },
unlocked: { label: '已跨期解锁', type: 'success' }
}
const triggerSourceMap: Record<string, string> = {
polling: '周期轮询',
manual_sync: '手动同步',
manual_override: '人工纠偏',
carrier_callback: '运营商回调',
business_event: '业务成功后的观测'
}
const resumeResultMap: Record<string, string> = {
resumed: '已发起复机',
skipped: '仅解锁未复机',
manual: '转人工核对'
}
const { columnChecks, columns } = useCheckedColumns(() => [
{ prop: 'id', label: '记录ID', width: 100 },
{ prop: 'card_id', label: 'IoT卡ID', width: 110 },
{ prop: 'carrier_id', label: '运营商通道ID', width: 140 },
{
prop: 'hit_traffic_mb',
label: '命中流量',
width: 140,
formatter: (row: CarrierTrafficThresholdHitItem) =>
formatNullableNumber(row.hit_traffic_mb, 'MB')
},
{
prop: 'hit_threshold_value',
label: '命中阈值',
width: 140,
formatter: formatThresholdValue
},
{
prop: 'period_start',
label: '计费周期起点',
width: 180,
formatter: (row: CarrierTrafficThresholdHitItem) => formatDateTime(row.period_start)
},
{
prop: 'judged_at',
label: '判定时间',
width: 180,
formatter: (row: CarrierTrafficThresholdHitItem) => formatDateTime(row.judged_at)
},
{
prop: 'trigger_source',
label: '触发来源',
minWidth: 160,
formatter: (row: CarrierTrafficThresholdHitItem) =>
row.trigger_source ? triggerSourceMap[row.trigger_source] || row.trigger_source : '无'
},
{
prop: 'status',
label: '锁状态',
width: 120,
formatter: (row: CarrierTrafficThresholdHitItem) => {
const status = statusMap[row.status]
return h(ElTag, { type: status?.type || 'info' }, () => status?.label || row.status || '无')
}
},
{
prop: 'unlocked_at',
label: '解锁时间',
width: 180,
formatter: (row: CarrierTrafficThresholdHitItem) =>
row.unlocked_at ? formatDateTime(row.unlocked_at) : '无'
},
{
prop: 'resume_result',
label: '复机结果',
minWidth: 150,
formatter: (row: CarrierTrafficThresholdHitItem) =>
row.resume_result
? resumeResultMap[row.resume_result] || row.resume_result
: '未处理或历史记录'
},
{
prop: 'failure_reason',
label: '安全原因',
minWidth: 180,
showOverflowTooltip: true,
formatter: (row: CarrierTrafficThresholdHitItem) => row.failure_reason || '无'
},
{
prop: 'created_at',
label: '记录创建时间',
width: 180,
formatter: (row: CarrierTrafficThresholdHitItem) => formatDateTime(row.created_at)
}
])
const columnOptions = [
{ label: '记录ID', prop: 'id' },
{ label: 'IoT卡ID', prop: 'card_id' },
{ label: '运营商通道ID', prop: 'carrier_id' },
{ label: '命中流量', prop: 'hit_traffic_mb' },
{ label: '命中阈值', prop: 'hit_threshold_value' },
{ label: '计费周期起点', prop: 'period_start' },
{ label: '判定时间', prop: 'judged_at' },
{ label: '触发来源', prop: 'trigger_source' },
{ label: '锁状态', prop: 'status' },
{ label: '解锁时间', prop: 'unlocked_at' },
{ label: '复机结果', prop: 'resume_result' },
{ label: '安全原因', prop: 'failure_reason' },
{ label: '记录创建时间', prop: 'created_at' }
]
const syncJudgedAtRange = () => {
if (
Array.isArray(searchForm.value.judged_at_range) &&
searchForm.value.judged_at_range.length === 2
) {
searchForm.value.start_time = searchForm.value.judged_at_range[0]
searchForm.value.end_time = searchForm.value.judged_at_range[1]
} else {
searchForm.value.start_time = ''
searchForm.value.end_time = ''
}
}
const getTableData = async () => {
if (!canAccess.value) return
const params: CarrierTrafficThresholdHitQueryParams = {
carrier_id: searchForm.value.carrier_id ?? undefined,
card_id: searchForm.value.card_id ?? undefined,
period_start: searchForm.value.period_start || undefined,
start_time: searchForm.value.start_time || undefined,
end_time: searchForm.value.end_time || undefined,
page: pagination.page,
page_size: pagination.page_size
}
loading.value = true
try {
const res = await CarrierTrafficThresholdHitService.getHits(params)
if (res.code === 0 && res.data) {
hitList.value = res.data.items || []
pagination.page = res.data.page || pagination.page
pagination.page_size = res.data.size || pagination.page_size
pagination.total = res.data.total || 0
} else {
ElMessage.error(res.msg || '查询通道阈值命中记录失败')
}
} catch (error: any) {
if (error?.status === 403 || error?.response?.status === 403) {
ElMessage.error('当前账号无权查看通道阈值命中记录')
} else {
ElMessage.error('查询通道阈值命中记录失败,请稍后重试')
}
} finally {
loading.value = false
}
}
const handleSearch = () => {
syncJudgedAtRange()
pagination.page = 1
void getTableData()
}
const handleReset = () => {
searchForm.value = { ...initialSearchState, judged_at_range: [] }
pagination.page = 1
void getTableData()
}
const handleRefresh = () => void getTableData()
const handleSizeChange = (pageSize: number) => {
pagination.page_size = Math.min(pageSize, 100)
pagination.page = 1
void getTableData()
}
const handleCurrentChange = (page: number) => {
pagination.page = page
void getTableData()
}
onMounted(() => {
if (canAccess.value) {
void getTableData()
void searchCarriers()
void searchCards()
}
})
</script>
<style scoped lang="scss">
.carrier-threshold-hit-page {
height: 100%;
}
</style>