Files
one-pipe-system/src/views/system/carrier-management/index.vue
luo 22e796ba23
All checks were successful
构建并部署前端到测试环境 / deploy (push) Successful in 55s
fix:some
2026-10-20 17:35:12 +08:00

852 lines
26 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<ArtTableFullScreen>
<div class="carrier-page" id="table-full-screen">
<!-- 搜索栏 -->
<ArtSearchBar
v-model:filter="searchForm"
:items="searchFormItems"
label-width="85"
:show-expand="false"
@reset="handleReset"
@search="handleSearch"
></ArtSearchBar>
<ElCard shadow="never" class="art-table-card">
<!-- 表格头部 -->
<ArtTableHeader
:columnList="columnOptions"
v-model:columns="columnChecks"
@refresh="handleRefresh"
>
<template #left>
<ElButton @click="showDialog('add')" v-permission="'carrier:add'">新增运营商</ElButton>
</template>
</ArtTableHeader>
<!-- 表格 -->
<ArtTable
ref="tableRef"
row-key="id"
:loading="loading"
:data="carrierList"
:currentPage="pagination.page"
:pageSize="pagination.pageSize"
: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>
<!-- 新增/编辑对话框 -->
<ElDialog
v-model="dialogVisible"
:title="dialogType === 'add' ? '新增运营商' : '编辑运营商'"
width="min(94vw, 760px)"
>
<ElForm
ref="formRef"
:model="form"
:rules="rules"
label-width="120px"
class="carrier-form-grid"
>
<ElFormItem label="运营商编码" prop="carrier_code">
<ElInput
v-model="form.carrier_code"
placeholder="请输入运营商编码或点击生成"
:disabled="dialogType === 'edit'"
clearable
>
<template #append>
<ElButton v-if="dialogType === 'add'" @click="handleGenerateCarrierCode"
>生成编码</ElButton
>
</template>
</ElInput>
</ElFormItem>
<ElFormItem label="运营商名称" prop="carrier_name">
<ElInput v-model="form.carrier_name" placeholder="请输入运营商名称" />
</ElFormItem>
<ElFormItem label="运营商类型" prop="carrier_type">
<ElSelect
v-model="form.carrier_type"
placeholder="请选择运营商类型"
style="width: 100%"
:disabled="dialogType === 'edit'"
>
<ElOption
v-for="item in CARRIER_TYPE_OPTIONS"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</ElSelect>
</ElFormItem>
<ElFormItem label="运营商描述" prop="description" class="carrier-form-span">
<ElInput
v-model="form.description"
type="textarea"
:rows="3"
placeholder="请输入运营商描述"
/>
</ElFormItem>
<ElFormItem label="流量重置日" prop="data_reset_day">
<ElInputNumber
v-model="form.data_reset_day"
:min="1"
:max="28"
:step="1"
controls-position="right"
placeholder="请输入流量重置日"
style="width: 100%"
/>
<div style="margin-top: 5px; font-size: 12px; color: #909399">
运营商每月清零网关计数器的日期(1-28日),默认为1日
</div>
</ElFormItem>
<ElFormItem v-if="canConfigureTrafficThreshold" label="通道阈值状态">
<ElSelect
v-model="form.traffic_threshold_mode"
placeholder="请选择,留空表示未配置"
style="width: 100%"
@change="handleTrafficThresholdModeChange"
>
<ElOption label="未配置" value="unconfigured" />
<ElOption label="停用" value="disabled" />
<ElOption label="启用" value="enabled" />
</ElSelect>
</ElFormItem>
<template v-if="canConfigureTrafficThreshold">
<ElFormItem label="通道阈值单位" prop="traffic_threshold_unit">
<ElSelect
v-model="form.traffic_threshold_unit"
placeholder="请选择阈值单位"
style="width: 100%"
:disabled="form.traffic_threshold_mode !== 'enabled'"
>
<ElOption label="MB" value="MB" />
<ElOption label="GB" value="GB" />
</ElSelect>
</ElFormItem>
<ElFormItem label="通道阈值数值" prop="traffic_threshold_value">
<ElInput
v-model="form.traffic_threshold_value"
type="number"
min="0"
step="any"
:disabled="form.traffic_threshold_mode !== 'enabled'"
placeholder="请输入大于0的阈值数值"
style="width: 100%"
/>
</ElFormItem>
</template>
<ElFormItem
v-if="canConfigureRealnameControl"
label="通道实名管控"
prop="realname_control_enabled"
>
<ElSwitch
v-model="form.realname_control_enabled"
:active-value="1"
:inactive-value="0"
active-text="管控"
inactive-text="不管控"
inline-prompt
/>
</ElFormItem>
<ElFormItem label="实名链接类型" prop="realname_link_type">
<ElSelect
v-model="form.realname_link_type"
placeholder="请选择实名链接类型"
style="width: 100%"
>
<ElOption label="无需实名" value="none" />
<ElOption label="模板实名" value="template" />
<ElOption label="网关实名" value="gateway" />
</ElSelect>
</ElFormItem>
<ElFormItem
v-if="form.realname_link_type === 'template'"
label="实名链接模板"
prop="realname_link_template"
class="carrier-form-span"
>
<ElInput
v-model="form.realname_link_template"
type="textarea"
:rows="2"
placeholder="请输入实名链接模板"
/>
<div style="margin-top: 5px; font-size: 12px; color: #909399">
支持变量:{iccid}、{msisdn}等
</div>
</ElFormItem>
</ElForm>
<template #footer>
<div class="dialog-footer">
<ElButton @click="dialogVisible = false">取消</ElButton>
<ElButton type="primary" @click="handleSubmit(formRef)" :loading="submitLoading">
提交
</ElButton>
</div>
</template>
</ElDialog>
</ElCard>
</div>
</ArtTableFullScreen>
</template>
<script setup lang="ts">
import { h, watch, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { CarrierService } from '@/api/modules'
import { ElMessage, ElMessageBox, ElTag, ElSwitch, ElButton } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import type {
Carrier,
CarrierType,
CarrierRealnameControlEnabled,
CreateCarrierParams,
UpdateCarrierParams
} from '@/types/api'
import type { SearchFormItem } from '@/types'
import { useCheckedColumns } from '@/composables/useCheckedColumns'
import { useAuth } from '@/composables/useAuth'
import { usePermission } from '@/composables/usePermission'
import { RoutesAlias } from '@/router/routesAlias'
import { generateCarrierCode } from '@/utils/codeGenerator'
import { formatDateTime } from '@/utils/business/format'
import {
CommonStatus,
getStatusText,
CARRIER_TYPE_OPTIONS,
getCarrierTypeLabel,
getCarrierTypeColor,
getCarrierRealnameControlLabel
} from '@/config/constants'
defineOptions({ name: 'CarrierManagement' })
const router = useRouter()
const { hasAuth } = useAuth()
const { isPlatformAccount } = usePermission()
const canConfigureTrafficThreshold = computed(() => isPlatformAccount.value)
const canConfigureRealnameControl = computed(() => isPlatformAccount.value)
const canUpdateCarrierStatus = hasAuth('carrier:update_status')
const dialogVisible = ref(false)
const loading = ref(false)
const submitLoading = ref(false)
const tableRef = ref()
// 搜索表单初始值
const initialSearchState = {
carrier_name: '',
carrier_type: null,
status: null
}
// 搜索表单
const searchForm = reactive({ ...initialSearchState })
// 搜索表单配置
const searchFormItems: SearchFormItem[] = [
{
label: '运营商名称',
prop: 'carrier_name',
type: 'input',
config: {
clearable: true,
placeholder: '请输入运营商名称'
}
},
{
label: '运营商类型',
prop: 'carrier_type',
type: 'select',
config: {
clearable: true,
placeholder: '请选择运营商类型'
},
options: () => [
{ label: '中国移动', value: 'CMCC' },
{ label: '中国联通', value: 'CUCC' },
{ label: '中国电信', value: 'CTCC' },
{ label: '中国广电', value: 'CBN' }
]
},
{
label: '状态',
prop: 'status',
type: 'select',
config: {
clearable: true,
placeholder: '请选择状态'
},
options: () => [
{ label: '启用', value: CommonStatus.ENABLED },
{ label: '禁用', value: CommonStatus.DISABLED }
]
}
]
// 分页
const pagination = reactive({
page: 1,
pageSize: 20,
total: 0
})
// 列配置
const columnOptions = [
{ label: '运营商编码', prop: 'carrier_code' },
{ label: '运营商名称', prop: 'carrier_name' },
{ label: '运营商类型', prop: 'carrier_type' },
{ label: '运营商描述', prop: 'description' },
...(canConfigureRealnameControl.value
? [{ label: '通道实名管控', prop: 'realname_control_enabled' }]
: []),
...(canConfigureTrafficThreshold.value
? [
{ label: '通道阈值状态', prop: 'traffic_threshold_enabled' },
{ label: '通道阈值单位', prop: 'traffic_threshold_unit' },
{ label: '通道阈值数值', prop: 'traffic_threshold_value' }
]
: []),
...(canUpdateCarrierStatus ? [{ label: '状态', prop: 'status' }] : []),
{ label: '创建时间', prop: 'created_at' }
]
const formRef = ref<FormInstance>()
const rules = reactive<FormRules>({
carrier_code: [
{ required: true, message: '请输入运营商编码', trigger: 'blur' },
{ min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
],
carrier_name: [
{ required: true, message: '请输入运营商名称', trigger: 'blur' },
{ min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
],
carrier_type: [{ required: true, message: '请选择运营商类型', trigger: 'change' }],
description: [{ max: 500, message: '描述不能超过500个字符', trigger: 'blur' }],
data_reset_day: [
{
validator: (_rule: any, value: any, callback: any) => {
if (value === null || value === undefined || value === '') {
callback() // 允许为空
} else {
const numValue = Number(value)
if (isNaN(numValue)) {
callback(new Error('请输入有效的数字'))
} else if (!Number.isInteger(numValue)) {
callback(new Error('请输入整数'))
} else if (numValue < 1 || numValue > 28) {
callback(new Error('流量重置日必须在1-28之间'))
} else {
callback()
}
}
},
trigger: 'blur'
}
],
realname_link_type: [{ required: true, message: '请选择实名链接类型', trigger: 'change' }],
realname_link_template: [
{
required: true,
validator: (_rule: any, value: any, callback: any) => {
if (form.realname_link_type === 'template' && !value) {
callback(new Error('请输入实名链接模板'))
} else {
callback()
}
},
trigger: 'blur'
}
]
})
const form = reactive<any>({
id: 0,
carrier_code: '',
carrier_name: '',
carrier_type: null,
description: '',
data_reset_day: 1,
realname_control_enabled: 0,
traffic_threshold_mode: 'unconfigured',
traffic_threshold_unit: null,
traffic_threshold_value: null,
realname_link_type: 'none',
realname_link_template: ''
})
const carrierList = ref<Carrier[]>([])
// 动态列配置
const { columnChecks, columns } = useCheckedColumns(() => [
{
prop: 'carrier_code',
label: '运营商编码',
minWidth: 160,
showOverflowTooltip: true
},
{
prop: 'carrier_name',
label: '运营商名称',
minWidth: 150,
showOverflowTooltip: true,
formatter: (row: Carrier) =>
h(
ElButton,
{
type: 'primary',
link: true,
style: { textDecoration: 'underline' },
onClick: () => void router.push(`${RoutesAlias.CarrierDetail}/${row.id}`)
},
() => row.carrier_name || '-'
)
},
{
prop: 'carrier_type',
label: '运营商类型',
width: 120,
formatter: (row: any) => {
const color = getCarrierTypeColor(row.carrier_type)
return h(
ElTag,
{ style: `background-color: ${color}; border-color: ${color}; color: #fff;` },
() => getCarrierTypeLabel(row.carrier_type)
)
}
},
{
prop: 'description',
label: '运营商描述',
minWidth: 200,
showOverflowTooltip: true
},
...(canConfigureRealnameControl.value
? [
{
prop: 'realname_control_enabled',
label: '通道实名管控',
width: 140,
formatter: (row: Carrier) =>
getCarrierRealnameControlLabel(row.realname_control_enabled)
}
]
: []),
...(canConfigureTrafficThreshold.value
? [
{
prop: 'traffic_threshold_enabled',
label: '通道阈值状态',
width: 130,
formatter: (row: Carrier) => getThresholdEnabledLabel(row.traffic_threshold_enabled)
},
{
prop: 'traffic_threshold_unit',
label: '通道阈值单位',
width: 120,
formatter: (row: Carrier) => row.traffic_threshold_unit || '未配置'
},
{
prop: 'traffic_threshold_value',
label: '通道阈值数值',
width: 140,
formatter: (row: Carrier) => formatThresholdValue(row.traffic_threshold_value)
}
]
: []),
...(canUpdateCarrierStatus
? [
{
prop: 'status',
label: '状态',
width: 100,
formatter: (row: any) => {
return h(ElSwitch, {
modelValue: row.status,
activeValue: CommonStatus.ENABLED,
inactiveValue: CommonStatus.DISABLED,
activeText: getStatusText(CommonStatus.ENABLED),
inactiveText: getStatusText(CommonStatus.DISABLED),
inlinePrompt: true,
'onUpdate:modelValue': (val: string | number | boolean) =>
handleStatusChange(row, val as number)
})
}
}
]
: []),
{
prop: 'created_at',
label: '创建时间',
width: 180,
formatter: (row: any) => formatDateTime(row.created_at)
},
{
prop: 'actions',
label: '操作',
width: 160,
fixed: 'right',
formatter: (row: any) => {
const buttons: any[] = []
if (hasAuth('carrier:edit')) {
buttons.push(
h(
ElButton,
{
type: 'primary',
link: true,
onClick: () => showDialog('edit', row)
},
() => '编辑'
)
)
}
if (hasAuth('carrier:delete')) {
buttons.push(
h(
ElButton,
{
type: 'danger',
link: true,
onClick: () => deleteCarrier(row)
},
() => '删除'
)
)
}
return h('div', { style: 'display: flex; gap: 8px;' }, buttons)
}
}
])
onMounted(() => {
getTableData()
})
// 监听对话框关闭,清除验证状态
watch(dialogVisible, (val) => {
if (!val) {
nextTick(() => {
formRef.value?.clearValidate()
})
}
})
// 获取运营商列表
const getTableData = async () => {
loading.value = true
try {
const params = {
page: pagination.page,
page_size: pagination.pageSize,
carrier_name: searchForm.carrier_name || undefined,
carrier_type: searchForm.carrier_type || undefined,
status: searchForm.status !== null ? searchForm.status : undefined
}
const res = await CarrierService.getCarriers(params)
if (res.code === 0) {
carrierList.value = res.data.items || []
pagination.total = res.data.total || 0
}
} catch (error) {
console.error(error)
} finally {
loading.value = false
}
}
// 重置搜索
const handleReset = () => {
Object.assign(searchForm, { ...initialSearchState })
pagination.page = 1
getTableData()
}
// 搜索
const handleSearch = () => {
pagination.page = 1
getTableData()
}
// 刷新表格
const handleRefresh = () => {
getTableData()
}
// 处理表格分页变化
const handleSizeChange = (newPageSize: number) => {
pagination.pageSize = newPageSize
getTableData()
}
const handleCurrentChange = (newCurrentPage: number) => {
pagination.page = newCurrentPage
getTableData()
}
const dialogType = ref('add')
const originalRealnameControlEnabled = ref<CarrierRealnameControlEnabled>(0)
// 显示新增/编辑对话框
const showDialog = (type: string, row?: any) => {
dialogVisible.value = true
dialogType.value = type
if (type === 'edit' && row) {
form.id = row.id
form.carrier_code = row.carrier_code
form.carrier_name = row.carrier_name
form.carrier_type = row.carrier_type
form.description = row.description
form.data_reset_day = row.data_reset_day || 1
form.realname_control_enabled = row.realname_control_enabled ?? 0
originalRealnameControlEnabled.value = form.realname_control_enabled
form.traffic_threshold_mode = getThresholdMode(row.traffic_threshold_enabled)
form.traffic_threshold_unit = row.traffic_threshold_unit ?? null
form.traffic_threshold_value = row.traffic_threshold_value ?? null
form.realname_link_type = row.realname_link_type || 'none'
form.realname_link_template = row.realname_link_template || ''
} else {
form.id = 0
form.carrier_code = ''
form.carrier_name = ''
form.carrier_type = null
form.description = ''
form.data_reset_day = 1
form.realname_control_enabled = 0
originalRealnameControlEnabled.value = 0
form.traffic_threshold_mode = 'unconfigured'
form.traffic_threshold_unit = null
form.traffic_threshold_value = null
form.realname_link_type = 'none'
form.realname_link_template = ''
}
// 清除表单验证状态
nextTick(() => {
formRef.value?.clearValidate()
})
}
const getThresholdEnabledLabel = (enabled?: 0 | 1 | null) => {
if (enabled === 1) return '启用'
if (enabled === 0) return '停用'
return '未配置'
}
const getThresholdMode = (enabled?: 0 | 1 | null) => {
if (enabled === 1) return 'enabled'
if (enabled === 0) return 'disabled'
return 'unconfigured'
}
const handleTrafficThresholdModeChange = (mode: string) => {
if (mode === 'unconfigured') {
form.traffic_threshold_unit = null
form.traffic_threshold_value = null
}
}
const formatThresholdValue = (value?: number | null) =>
value === null || value === undefined ? '未配置' : String(value)
const getRealnameControlPayload = (): Pick<CreateCarrierParams, 'realname_control_enabled'> =>
canConfigureRealnameControl.value
? { realname_control_enabled: form.realname_control_enabled }
: {}
const getTrafficThresholdPayload = () => {
if (!canConfigureTrafficThreshold.value) return {}
if (form.traffic_threshold_mode === 'unconfigured') {
return {
traffic_threshold_enabled: null,
traffic_threshold_unit: null,
traffic_threshold_value: null
}
}
if (form.traffic_threshold_mode === 'disabled') {
return { traffic_threshold_enabled: 0 as const }
}
return {
traffic_threshold_enabled: 1 as const,
traffic_threshold_unit: form.traffic_threshold_unit,
traffic_threshold_value: Number(form.traffic_threshold_value)
}
}
// 处理编码生成
const handleGenerateCarrierCode = () => {
form.carrier_code = generateCarrierCode()
// 清除该字段的验证错误提示
nextTick(() => {
formRef.value?.clearValidate('carrier_code')
})
ElMessage.success('编码生成成功')
}
// 删除运营商
const deleteCarrier = (row: any) => {
ElMessageBox.confirm(`确定删除运营商 ${row.carrier_name} 吗?`, '删除确认', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'error'
})
.then(async () => {
try {
await CarrierService.deleteCarrier(row.id)
ElMessage.success('删除成功')
await getTableData()
} catch (error) {
console.error(error)
}
})
.catch(() => {
// 用户取消删除
})
}
// 提交表单
const handleSubmit = async (formEl: FormInstance | undefined) => {
if (!formEl || submitLoading.value) return
if (
canConfigureTrafficThreshold.value &&
form.traffic_threshold_mode === 'enabled' &&
(!form.traffic_threshold_unit ||
!Number.isFinite(Number(form.traffic_threshold_value)) ||
Number(form.traffic_threshold_value) <= 0)
) {
ElMessage.warning('启用通道阈值时,请填写阈值单位和大于0的阈值数值')
return
}
await formEl.validate(async (valid) => {
if (valid && !submitLoading.value) {
submitLoading.value = true
try {
const data: CreateCarrierParams = {
carrier_code: form.carrier_code,
carrier_name: form.carrier_name,
carrier_type: form.carrier_type as CarrierType,
description: form.description || undefined,
data_reset_day: form.data_reset_day || null,
realname_link_type: form.realname_link_type,
realname_link_template:
form.realname_link_type === 'template' ? form.realname_link_template : undefined,
...getTrafficThresholdPayload(),
...getRealnameControlPayload()
}
if (dialogType.value === 'add') {
await CarrierService.createCarrier(data)
ElMessage.success('新增成功')
} else {
const updateData: UpdateCarrierParams = {
carrier_name: form.carrier_name,
description: form.description || undefined,
data_reset_day: form.data_reset_day || null,
realname_link_type: form.realname_link_type,
realname_link_template:
form.realname_link_type === 'template' ? form.realname_link_template : undefined,
...getTrafficThresholdPayload(),
...getRealnameControlPayload()
}
if (
canConfigureRealnameControl.value &&
originalRealnameControlEnabled.value === 0 &&
updateData.realname_control_enabled === 1
) {
try {
await ElMessageBox.confirm(
'开启通道实名管控后,该通道未实名的开机卡会在下一次评估时被停机。确认开启吗?',
'确认开启通道实名管控',
{
type: 'warning',
confirmButtonText: '确认开启',
cancelButtonText: '取消',
closeOnClickModal: false
}
)
} catch {
return
}
updateData.realname_control_confirm = true
}
await CarrierService.updateCarrier(form.id, updateData)
ElMessage.success('修改成功')
}
dialogVisible.value = false
formEl.resetFields()
await getTableData()
} catch (error) {
console.error(error)
} finally {
submitLoading.value = false
}
}
})
}
// 状态切换
const handleStatusChange = async (row: any, newStatus: number) => {
const oldStatus = row.status
// 先更新UI
row.status = newStatus
try {
await CarrierService.updateCarrierStatus(row.id, newStatus)
ElMessage.success('状态切换成功')
} catch (error) {
// 切换失败,恢复原状态
row.status = oldStatus
console.error(error)
}
}
</script>
<style scoped lang="scss">
.carrier-page {
height: 100%;
}
.carrier-form-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
column-gap: 24px;
align-items: start;
:deep(.el-form-item) {
margin-right: 0;
}
}
.carrier-form-span {
grid-column: 1 / -1;
}
@media (width <= 640px) {
.carrier-form-grid {
grid-template-columns: minmax(0, 1fr);
}
.carrier-form-span {
grid-column: auto;
}
}
</style>