OperationLog.vue 9.45 KB
<template>
    <div ref="scrollBoxRef" class="logBox" @scroll="handleScroll">
        <div v-for="(item, index) in logList" :key="item.id || index" class="log-list">
            <div class="progressBox">
                <div class="progressBox-point" :class="{ 'bg-1956fe': index > 0 }"></div>
                <div v-if="logList.length !== index + 1 || hasMore" class="progressBox-line"></div>
            </div>
            <div class="log-title">
                <template v-if="item.description">
                    <a-popover
                        v-if="isChangeLog(item)"
                        trigger="hover"
                        placement="topLeft"
                        :overlay-class-name="'op-log-detail-popover'"
                        :overlay-style="{ maxWidth: '820px' }"
                    >
                        <template #content>
                            <a-table
                                :columns="detailColumns"
                                :data-source="item.detailItems"
                                :pagination="false"
                                :scroll="{ y: 280 }"
                                :row-key="(row) => row.logItemId"
                                size="small"
                                bordered
                                class="op-log-detail-table"
                            />
                        </template>
                        <div :title="item.description" class="logBox-content with-detail">
                            {{ item.description }}
                        </div>
                    </a-popover>
                    <div v-else :title="item.description" class="logBox-content">
                        {{ item.description }}
                    </div>
                </template>
                <div class="logBox-time">
                    <div v-if="getFirstChar(item.operatorName)" class="logBox-point">
                        {{ getFirstChar(item.operatorName) }}
                    </div>
                    <span class="p-l-4">{{ item.operatorName }}</span>
                    <span class="p-l-8">{{ item.operationTime }}</span>
                </div>
            </div>
        </div>
        <div v-if="loading" class="statusBox">加载中...</div>
        <div v-else-if="!hasMore && logList.length > 0" class="statusBox">已加载全部</div>
        <div v-else-if="!loading && logList.length === 0" class="statusBox">暂无操作日志</div>
    </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-facing-decorator";
import { DetectionController } from "@/api/DetectionController";
import { CopyOutlined } from "@ant-design/icons-vue";
import { copyText } from "@/utils/copyText";
import { message } from "ant-design-vue";
import { h, type VNode } from "vue";

function formatLogValue(value: unknown): string {
    if (value == null || value === "") {
        return "-";
    }
    return String(value);
}

function hasCopyableValue(value: unknown): boolean {
    return value != null && String(value).trim() !== "";
}

@Component({ components: { CopyOutlined } })
export default class OperationLog extends Vue {
    workOrderId: any = "";
    logList: any[] = [];
    offset = 0;
    limit = 20;
    totalElements = 0;
    loading = false;
    hasMore = true;

    detailColumns: any[] = [];

    created() {
        this.detailColumns = this.buildDetailColumns();
    }

    buildDetailColumns() {
        const renderCell = (text: unknown) => this.renderValueCell(text);
        const valueColumn = (title: string, dataIndex: string, key: string) => ({
            title,
            dataIndex,
            key,
            width: 280,
            customRender: ({ text }: { text: unknown }) => renderCell(text),
        });
        return [
            {
                title: "变更项",
                dataIndex: "fieldLabel",
                key: "fieldLabel",
                width: 200,
                ellipsis: true,
            },
            valueColumn("变更前的值", "beforeValue", "beforeValue"),
            valueColumn("变更后的值", "afterValue", "afterValue"),
        ];
    }

    renderValueCell = (value: unknown): VNode => {
        const display = formatLogValue(value);
        const children: VNode[] = [
            h("span", { class: "op-log-value-text", title: display }, display),
        ];
        if (hasCopyableValue(value)) {
            children.push(
                h(
                    "a",
                    {
                        class: "op-log-copy-btn",
                        onClick: (e: Event) => {
                            e.stopPropagation();
                            e.preventDefault();
                            void this.copyLogValue(value);
                        },
                    },
                    [h(CopyOutlined), " 复制"],
                ),
            );
        }
        return h("div", { class: "op-log-value-cell" }, children);
    };

    isChangeLog(item: any) {
        const type = Number(item?.operationType);
        return (
            (type === 11 || type === 12) &&
            Array.isArray(item?.detailItems) &&
            item.detailItems.length > 0
        );
    }

    init(workOrderId: any) {
        this.workOrderId = workOrderId;
        this.logList = [];
        this.offset = 0;
        this.totalElements = 0;
        this.hasMore = true;
        this.loadNextPage();
    }

    loadNextPage() {
        if (this.loading || !this.hasMore || !this.workOrderId) return;
        this.loading = true;
        DetectionController.inspectWoOpLogPage(this.axios, {
            workOrderId: this.workOrderId,
            offset: this.offset,
            limit: this.limit,
        })
            .then((response) => {
                const data = response?.data?.data || {};
                const content = data.content || [];
                this.logList = [...this.logList, ...content];
                this.totalElements = data.totalElements || this.logList.length;
                this.offset += this.limit;
                this.hasMore =
                    this.logList.length < this.totalElements &&
                    content.length > 0;
            })
            .finally(() => {
                this.loading = false;
            });
    }

    handleScroll(e: Event) {
        const target = e.target as HTMLElement;
        if (
            target.scrollTop + target.clientHeight >=
            target.scrollHeight - 20
        ) {
            this.loadNextPage();
        }
    }

    getFirstChar(str: string) {
        if (!str) return "";
        return String(str).charAt(0);
    }

    async copyLogValue(value: unknown) {
        const text = formatLogValue(value);
        if (text === "-") {
            return;
        }
        try {
            await copyText(text);
            message.success("复制成功");
        } catch {
            message.error("复制失败");
        }
    }
}
</script>

<style lang="less" scoped>
.logBox {
    float: left;
    width: 100%;
    max-height: 480px;
    margin-top: 16px;
    overflow-y: auto;
}

.log-list {
    float: left;
    width: -webkit-calc(100% - 32px);
    width: -moz-calc(100% - 32px);
    width: calc(100% - 32px);
    margin-bottom: 16px;
    margin-left: 32px;
}

.progressBox {
    float: left;
    width: 24px;
}

.progressBox-point {
    width: 8px;
    height: 8px;
    border: 2px solid #1956fe;
    border-radius: 8px;
}

.bg-1956fe {
    background: #1956fe;
}

.progressBox-line {
    width: 2px;
    height: 40px;
    margin-top: 3px;
    margin-left: 2.5px;
    background: #1956fe;
}

.log-title {
    float: left;
    width: -webkit-calc(100% - 24px);
    width: -moz-calc(100% - 24px);
    width: calc(100% - 24px);
}

.logBox-content {
    float: left;
    max-width: 100%;
    font-size: 14px;
    font-weight: 400;
    line-height: 20px;
    color: #2f3133;
    word-wrap: break-word;
    word-break: break-word;
}

.logBox-content.with-detail {
    color: #1956fe;
    cursor: help;
    text-decoration: underline;
    text-decoration-color: rgba(25, 86, 254, 0.4);
    text-decoration-style: dashed;
    text-underline-offset: 3px;
}

.logBox-content span {
    color: #2f3133;
}

.logBox-time {
    float: left;
    width: 100%;
    height: 20px;
    margin-top: 6px;
    font-size: 12px;
    font-weight: 400;
    line-height: 20px;
    color: #777c85;
}

.logBox-point {
    float: left;
    width: 14px;
    height: 14px;
    margin: 3px 0;
    font-size: 8px;
    font-weight: 700;
    line-height: 14px;
    color: #1956fe;
    text-align: center;
    background: #e6efff;
    border-radius: 8px;
}

.statusBox {
    float: left;
    width: 100%;
    padding: 8px 0 16px;
    font-size: 13px;
    font-weight: 400;
    color: #999999;
    text-align: center;
}

.p-l-4 {
    padding-left: 4px;
}

.p-l-8 {
    padding-left: 8px;
}

</style>

<style lang="less">
.op-log-detail-popover .op-log-detail-table .ant-table-cell {
    vertical-align: middle;
}

.op-log-detail-popover .op-log-value-cell {
    display: flex;
    align-items: center;
    gap: 16px;
    width: 100%;
    padding-right: 4px;
}

.op-log-detail-popover .op-log-value-text {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding-right: 4px;
}

.op-log-detail-popover .op-log-copy-btn {
    flex-shrink: 0;
    margin-left: 4px;
    padding-left: 12px;
    font-size: 12px;
    color: #1956fe;
    white-space: nowrap;
    cursor: pointer;
    border-left: 1px solid #e8eaed;
}

.op-log-detail-popover .op-log-copy-btn:hover {
    color: #0d3fd4;
}
</style>