WEB/src/views/system/setting/encoding/components/PlatformCodeManagement.vue
2026-06-26 16:11:03 +08:00

364 lines
9.9 KiB
Vue

<template>
<div class="encoding-sub-container">
<!-- <scan-box
ref="scanBox"
cache-key="encoding:platformCode"
width="250px"
:tree-props="{ children: 'children', label: 'label' }"
:auto-submit="false"
:preset-tree="presetTree"
:available-fields="availableFieldsRef"
@condition-change="handleConditionChange"
@reset="handleConditionReset"
@condition-clear="handleConditionClear"
/> -->
<div class="main-content">
<div class="toolbar">
<div class="toolbar-left">
<el-button size="small" :loading="loading" @click="loadList">刷新</el-button>
<el-button type="success" size="small" @click="openCreate">新增平台编码</el-button>
</div>
</div>
<div class="upper-wrapper">
<upper-table
ref="upperTable"
title="平台编码列表"
default-align="center"
table-id="encoding-platform-code"
:data="tableData"
:columns="columns"
:table-size="tableSize"
:pagination="pagination"
:show-index="true"
:show-selection="false"
:loading="loading"
:query-time="queryTime"
@page-change="handlePageChange"
@page-size-change="handlePageSizeChange"
@refresh="loadList"
>
<template #column-actions="{ row }">
<el-button link type="primary" @click="openEdit(row)">编辑</el-button>
<el-button link type="danger" @click="handleDelete(row)">删除</el-button>
</template>
<template #empty>
<div class="empty-tip">暂无平台编码数据</div>
</template>
</upper-table>
</div>
</div>
<el-dialog
v-model="dialogVisible"
:title="dialogMode === 'create' ? '新增平台编码' : '编辑平台编码'"
width="560px"
align-center
@close="resetForm"
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="110px">
<el-form-item label="业务编码" prop="code">
<el-input v-model="formData.code" placeholder="如 DY" />
</el-form-item>
<el-form-item label="业务名称" prop="name">
<el-input v-model="formData.name" placeholder="如 抖音(仓管家映射)" />
</el-form-item>
<el-form-item label="平台" prop="platform_code">
<el-select
v-model="formData.platform_code"
placeholder="请选择平台"
filterable
style="width: 100%"
@change="handlePlatformChange"
>
<el-option
v-for="item in platformOptions"
:key="item.code"
:label="item.label || item.name"
:value="item.code"
/>
</el-select>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="submitForm">保存</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import ScanBox from '@/components/ScanBox/index.vue'
import UpperTable from '@/components/UpperTable/index.vue'
import { getPlatformList } from '@/api/index/index'
import {
settingPlatformCreate,
settingPlatformDelete,
settingPlatformList,
settingPlatformUpdate
} from '@/api/setting.platform/platform'
import {
buildWhereConditions,
platformAvailableFields,
platformColumns
} from '../encoding-config'
type PlatformOption = {
code: string
name: string
label?: string
}
type PlatformRecord = {
id?: number
code: string
name: string
platform_code: string
platform_name?: string
label?: string
}
const scanBox = ref<any>(null)
const presetTree = ref<any[]>([])
const availableFieldsRef = reactive(JSON.parse(JSON.stringify(platformAvailableFields)))
const columns = platformColumns
const loading = ref(false)
const saving = ref(false)
const tableData = ref<PlatformRecord[]>([])
const platformOptions = ref<PlatformOption[]>([])
const queryTime = ref(0)
const tableSize = ref('small')
const pagination = reactive({ currentPage: 1, pageSize: 20, total: 0 })
const dialogVisible = ref(false)
const dialogMode = ref<'create' | 'edit'>('create')
const formRef = ref<FormInstance>()
const formData = reactive<PlatformRecord>({
id: undefined,
code: '',
name: '',
platform_code: ''
})
const rules: FormRules = {
code: [{ required: true, message: '请输入业务编码', trigger: 'blur' }],
name: [{ required: true, message: '请输入业务名称', trigger: 'blur' }],
platform_code: [{ required: true, message: '请选择平台', trigger: 'change' }]
}
const isSuccess = (res: any) => res && [0, 1, 200].includes(res.code)
function buildListPayload() {
const raw = scanBox.value?.getConditions?.() || {}
const where = buildWhereConditions(raw)
const keyword = String(where.keyword?.value ?? raw.keyword ?? '').trim()
delete where.keyword
const payload: Record<string, any> = {
pagination: {
page: pagination.currentPage,
pageSize: pagination.pageSize
}
}
if (keyword) {
payload.keyword = keyword
}
if (Object.keys(where).length) {
payload.where = where
}
return payload
}
function syncPresetTree(list: PlatformRecord[]) {
presetTree.value = list.map((item) => ({
label: item.name || item.code,
id: item.id,
code: item.code,
children: []
}))
}
function fillPlatformScanOptions(raw: any[]) {
const field = availableFieldsRef.find((f: any) => f.value === 'platform_code')
if (!field) return
field.options = (raw || []).map((item: any) => ({
label: item.label || item.platform_name || item.name,
value: item.platform_code || item.code
}))
}
async function loadList() {
loading.value = true
const start = Date.now()
try {
const res: any = await settingPlatformList(buildListPayload())
if (!isSuccess(res)) {
ElMessage.error(res?.message || '获取平台编码失败')
tableData.value = []
pagination.total = 0
presetTree.value = []
return
}
const data = res?.data || {}
tableData.value = data.list || []
pagination.total = Number(data.pagination?.total ?? data.total ?? 0)
syncPresetTree(tableData.value)
} catch {
tableData.value = []
pagination.total = 0
presetTree.value = []
ElMessage.error('获取平台编码失败')
} finally {
queryTime.value = Date.now() - start
loading.value = false
}
}
async function loadPlatformOptions() {
try {
const res: any = await getPlatformList()
if (!isSuccess(res)) {
platformOptions.value = []
return
}
const raw = res?.data || []
platformOptions.value = raw.map((item: any) => ({
code: item.platform_code || item.code,
name: item.platform_name || item.name,
label: item.label || item.platform_name || item.name
}))
fillPlatformScanOptions(raw)
} catch {
platformOptions.value = []
}
}
function handlePageChange(page: number) {
pagination.currentPage = page
loadList()
}
function handlePageSizeChange(size: number) {
pagination.pageSize = size
pagination.currentPage = 1
loadList()
}
function handleConditionChange() {
pagination.currentPage = 1
loadList()
}
function handleConditionClear() {
pagination.currentPage = 1
loadList()
}
function handleConditionReset() {
pagination.currentPage = 1
scanBox.value?.reset?.()
loadList()
}
function handlePlatformChange(code: string) {
const target = platformOptions.value.find((item) => item.code === code)
if (!target) return
if (!formData.name) {
formData.name = target.name || target.label || ''
}
}
function resetForm() {
formData.id = undefined
formData.code = ''
formData.name = ''
formData.platform_code = ''
formRef.value?.clearValidate()
}
function openCreate() {
dialogMode.value = 'create'
dialogVisible.value = true
resetForm()
}
function openEdit(row: PlatformRecord) {
dialogMode.value = 'edit'
dialogVisible.value = true
formData.id = row.id
formData.code = row.code
formData.name = row.name
formData.platform_code = row.platform_code
formRef.value?.clearValidate()
}
async function submitForm() {
if (!formRef.value || saving.value) return
const valid = await formRef.value.validate().catch(() => false)
if (!valid) return
saving.value = true
try {
const payload = {
id: formData.id as number,
code: formData.code,
name: formData.name,
platform_code: formData.platform_code
}
const res: any = dialogMode.value === 'create'
? await settingPlatformCreate({
code: payload.code,
name: payload.name,
platform_code: payload.platform_code
})
: await settingPlatformUpdate(payload)
if (!isSuccess(res)) {
ElMessage.error(res?.message || '保存失败')
return
}
ElMessage.success(dialogMode.value === 'create' ? '新增成功' : '更新成功')
dialogVisible.value = false
loadList()
} catch {
ElMessage.error('保存失败')
} finally {
saving.value = false
}
}
function handleDelete(row: PlatformRecord) {
if (!row.id) return
ElMessageBox.confirm(`确认删除平台编码「${row.code}」吗?`, '提示', { type: 'warning' })
.then(async () => {
const res: any = await settingPlatformDelete({ id: row.id as number })
if (!isSuccess(res)) {
ElMessage.error(res?.message || '删除失败')
return
}
ElMessage.success('删除成功')
if (tableData.value.length === 1 && pagination.currentPage > 1) {
pagination.currentPage -= 1
}
loadList()
})
.catch(() => undefined)
}
onMounted(async () => {
await loadPlatformOptions()
await loadList()
})
</script>
<style lang="scss" scoped>
@use '../styles/encoding-sub.scss';
</style>