front/src/views/track/trackState/index.vue
2026-03-23 10:00:44 +08:00

468 lines
13 KiB
Vue

<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import { inputEnNum } from '@/utils/formatter';
import { $baseMessage } from '@/utils';
import { useUserStore } from '@/store';
import {
postEdit,
getGetDetail,
postGetList,
} from '@/api/TrackState/trackStatus';
import { getAppGetList } from '@/api/ApplicationServices/apps';
import PaginationPro from '@/components/PaginationPro/index.vue';
import HandsontablePro from '@/components/HandsontablePro/index.vue';
import { useTableContainerHeight } from '@/composables/useTableContainerHeight';
import { IconSearch } from '@arco-design/web-vue/es/icon';
const { t } = useI18n();
const { tableContainerRef, tableHeight } = useTableContainerHeight();
const handsontableHeight = computed(() => Math.max(tableHeight.value || 0, 400));
type IDialogConfig = {
show: boolean;
type: 'add' | 'edit';
title: string;
};
defineOptions({ name: 'TrackState' });
const userStore = useUserStore();
const modalFormRef = ref();
const editFormParams = reactive<any>({
options: 1,
id: undefined,
appKey: userStore.appKey,
app: userStore.appKey,
});
const searchKeyword = ref('');
const tableData = ref<any[]>([]);
const totalCount = ref(0);
const currentPage = ref(1);
const pageSize = ref(50);
const appMap = ref<Record<string, string>>({});
const loadAppMap = async () => {
try {
const res = await getAppGetList({
PageIndex: 1,
PageSize: 1000,
}) as any;
const apps = res?.data?.items || res?.items || res?.data || [];
const map: Record<string, string> = {};
apps.forEach((app: any) => {
if (app.appKey && app.name) {
map[app.appKey] = app.name;
}
});
appMap.value = map;
} catch (error) {
console.error('????????', error);
}
};
const columns = computed(() => [
{ key: 'code', label: t('track.state.table.statusCode'), width: 120 },
{ key: 'trackStatus', label: t('track.state.table.status'), width: 120 },
{ key: 'enTrackStatus', label: t('track.state.table.statusEn'), width: 120 },
{ key: 'isCompleted', label: t('track.state.table.isComplete'), width: 100 },
{ key: 'isToMq', label: t('track.state.table.isBroadcast'), width: 100 },
{ key: 'createTime', label: t('track.state.table.createTime'), width: 150 },
{ key: 'createUser', label: t('track.state.table.creator'), width: 100 },
{ key: 'updateTime', label: t('track.state.table.modifyTime'), width: 150 },
{ key: 'updateUser', label: t('track.state.table.modifier'), width: 100 },
{ key: 'app', label: t('track.state.table.application'), width: 120 },
]);
function customCellRenderer(row: number, col: number, value: any, rowData: any) {
const dataRow = tableData.value[row];
if (!dataRow) return null;
// isCompleted (col 4)
if (col === 4) {
const yes = t('track.state.common.yes');
const no = t('track.state.common.no');
const isCompleted = dataRow.isCompleted;
const color = isCompleted ? 'green' : 'red';
const text = isCompleted ? yes : no;
return `<span style="color: ${color === 'green' ? '#00b42a' : '#f53f3f'};">${text}</span>`;
}
// isToMq (col 5)
if (col === 5) {
const yes = t('track.state.common.yes');
const no = t('track.state.common.no');
const isToMq = dataRow.isToMq;
const text = isToMq ? yes : no;
return `<span style="color: ${isToMq ? '#00b42a' : '#f53f3f'};">${text}</span>`;
}
return null;
}
function actionRenderer(row: any, rowIndex: number) {
const editText = t('track.state.actions.edit');
const deleteText = t('track.state.actions.delete');
return `
<div class="action-buttons" style="display: flex; align-items: center; justify-content: center; gap: 8px;">
<span data-action="edit" data-row-index="${rowIndex}" style="cursor: pointer; color: var(--color-text-2); padding: 4px; border-radius: 4px;" title="${editText}">
<svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor"><path d="M11.7 2.3a1 1 0 0 1 1.4 0l1 1a1 1 0 0 1 0 1.4l-8 8a1 1 0 0 1-.7.3H4a1 1 0 0 1-1-1v-1.4a1 1 0 0 1 .3-.7l8-8z"/></svg>
</span>
<span data-action="delete" data-row-index="${rowIndex}" style="cursor: pointer; color: #ff4d4f; padding: 4px; border-radius: 4px;" title="${deleteText}">
<svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor"><path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/><path d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1z"/></svg>
</span>
</div>
`;
}
function handleActionClick(row: any, _rowIndex: number, action?: string) {
if (action === 'edit') {
handleOpenModal('edit', row);
} else if (action === 'delete') {
handleDelData(row.id);
}
}
const loading = ref(false);
const loadTableData = async () => {
loading.value = true;
try {
const res = await postGetList({
appKey: userStore.appKey,
keywords: searchKeyword.value,
pageIndex: currentPage.value,
pageSize: pageSize.value,
}) as API.GetTrackStatusListDtoPagedResult;
totalCount.value = Number(res?.totalCount ?? 0);
const items = res?.items || [];
tableData.value = items.map((item: any) => ({
...item,
app: item.appKey
? (appMap.value[item.appKey] || item.appKey)
: (typeof item.app === 'object' && item.app?.name ? item.app.name : item.app),
}));
} finally {
loading.value = false;
}
};
const handlePageChange = (page: number) => {
currentPage.value = page;
loadTableData();
};
const handlePageSizeChange = (size: number) => {
pageSize.value = size;
currentPage.value = 1;
loadTableData();
};
const formProps = computed(() => ({
formLayout: 'vertical',
defaultSpan: 12,
itemList: [
{
label: t('track.state.form.statusCode'),
field: 'code',
comp: 'a-input',
rules: [
{
required: true,
message: t('track.state.form.statusCodeRequired'),
},
],
compProps: ({ formModel }: any) => {
return {
disabled: formModel?.id,
placeholder: t('track.state.form.statusCodePlaceholder'),
size: 'large',
formatter: (e: string) => inputEnNum(e, true),
parser: (e: string) => inputEnNum(e, true),
};
},
span: 12,
},
{
field: 'trackStatus',
label: t('track.state.form.status'),
rules: [
{
required: true,
message: t('track.state.form.statusRequired'),
},
],
comp: 'a-input',
compProps: {
placeholder: t('track.state.form.statusPlaceholder'),
size: 'large',
},
span: 12,
},
{
field: 'enTrackStatus',
label: t('track.state.form.statusEn'),
rules: [
{
required: true,
message: t('track.state.form.statusEnRequired'),
},
{
required: true,
pattern: /^[a-zA-Z0-9_]{0,3000}$/,
message: t('track.state.form.statusEnOnlyAlphanumeric'),
trigger: 'change',
},
],
comp: 'a-input',
compProps: {
placeholder: t('track.state.form.statusEnPlaceholder'),
size: 'large',
},
span: 12,
},
{
field: 'isCompleted',
label: t('track.state.form.isComplete'),
comp: 'a-select',
compProps: {
placeholder: t('track.state.form.isCompletePlaceholder'),
size: 'large',
defaultValue: false,
options: [
{ label: t('track.state.common.yes'), value: true },
{ label: t('track.state.common.no'), value: false },
],
},
span: 12,
},
{
field: 'isToMq',
label: t('track.state.form.isBroadcast'),
comp: 'a-select',
compProps: {
placeholder: t('track.state.form.isBroadcastPlaceholder'),
size: 'large',
defaultValue: false,
options: [
{ label: t('track.state.common.yes'), value: true },
{ label: t('track.state.common.no'), value: false },
],
},
span: 12,
},
{
field: 'appKey',
label: t('track.state.form.application'),
comp: 'SelectPro',
rules: [
{
required: true,
message: t('track.state.form.applicationRequired'),
},
],
compProps: {
placeholder: t('track.state.form.applicationPlaceholder'),
size: 'large',
api: getAppGetList,
labelKeys: ['name', 'appKey'],
separator: '+',
idKey: 'appKey',
isPage: true,
immediate: true,
onChange: handleAppChange,
},
span: 12,
},
],
}));
function handleAppChange(appKey: string) {
editFormParams.appKey = appKey;
editFormParams.app = appKey;
}
function handleOpenModal(type: IDialogConfig['type'], row?: any) {
const titleObj: { [x in IDialogConfig['type']]: string } = {
add: t('track.state.modal.add'),
edit: t('track.state.modal.edit'),
};
editFormParams.options = 1;
editFormParams.id = undefined;
if (type === 'edit') {
unref(modalFormRef)?.openModalFn({
id: row?.id,
title: titleObj[type],
});
editFormParams.options = 3;
editFormParams.id = row?.id;
return;
}
modalFormRef.value?.openModalFn({
id: editFormParams.id,
title: titleObj[type],
});
}
function onSubmit() {
loadTableData();
}
function handleDelData(id: number) {
$baseConfirm({
content: t('track.state.modal.deleteConfirm'),
title: t('track.state.modal.warningTitle'),
onOk: async () => {
await postEdit({
id,
options: 2,
appKey: userStore.appKey,
});
await loadTableData();
$baseMessage(t('track.state.message.deleteSuccess'), 'success');
},
});
}
function handleSearch() {
currentPage.value = 1;
loadTableData();
}
onMounted(async () => {
await loadAppMap();
loadTableData();
});
</script>
<template>
<div class="track-state-page">
<div class="track-state-page-card">
<a-input
v-model="searchKeyword"
:placeholder="$t('track.state.search.placeholder')"
allow-clear
size="small"
class="search-input"
@change="handleSearch"
@clear="handleSearch"
@keyup.enter="handleSearch"
>
<template #prefix>
<icon-search />
</template>
</a-input>
<div class="table-toolbar">
<div class="toolbar-left">
<TableButtons
:actions="[
{
label: $t('track.state.actions.add'),
type: 'primary',
onClick: () => handleOpenModal('add'),
},
]"
/>
</div>
<div class="toolbar-right">
<PaginationPro
:total-count="totalCount"
v-model:current-page="currentPage"
v-model:page-size="pageSize"
@page-change="handlePageChange"
@page-size-change="handlePageSizeChange"
/>
</div>
</div>
<div ref="tableContainerRef" class="list-wrap">
<HandsontablePro
:columns="columns"
:data="tableData"
:total="totalCount"
:current-page="currentPage"
:page-size="pageSize"
:search-params="{}"
:show-action-column="true"
:action-column-position="'end'"
:action-column-width="110"
:custom-cell-renderer="customCellRenderer"
:action-renderer="actionRenderer"
:height="handsontableHeight"
:read-only="true"
:stretch-columns="true"
@action-click="handleActionClick"
/>
</div>
</div>
<ModalForm
ref="modalFormRef"
:edit-api="postEdit"
:add-api="postEdit"
:info-api="getGetDetail"
:form-props="formProps"
:api-params="editFormParams"
@on-submit="onSubmit"
/>
</div>
</template>
<style scoped lang="less">
.track-state-page {
width: 100%;
box-sizing: border-box;
padding: 16px;
background-color: #f0f3f4;
min-height: 100vh;
height: auto;
overflow-x: hidden;
overflow-y: auto;
}
.track-state-page-card {
background: #fff;
border-radius: 8px;
padding: 16px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
box-sizing: border-box;
display: flex;
flex-direction: column;
overflow: hidden;
}
.search-input {
width: 20%;
max-width: 320px;
margin-bottom: 16px;
}
.table-toolbar {
margin-bottom: 8px;
display: flex;
justify-content: space-between;
align-items: center;
}
.list-wrap {
width: 100%;
flex: 0 0 auto;
height: 1200px;
min-height: 0;
overflow: hidden;
display: flex;
flex-direction: column;
overscroll-behavior: contain;
touch-action: pan-x pan-y;
}
.list-wrap :deep(.handsontable-pro-container) {
flex: 1;
min-height: 0;
width: 100%;
display: flex;
flex-direction: column;
}
</style>