实现页面为结算单,也页面由固定头部,订单信息,产品列表,金额汇总,订单明细
其中产品列表需要动态计算分压,订单明细由于存在多个子单,订单收费项目可配置化,导致也存在多个分页需要处理

效果预览在这里插入图片描述

存在的难点

  1. 分页,为了方便计算,产品列表的行高进行了固定
  2. 字体大小,由于web字体最小只能设置10像素,所以结算页面统一为12像素,打印模块使用缩放,进行缩小字体元素
  3. 背景,由于实用的printjs在生成pdf时会将background丢失,唯一能渲染出来的是边框颜色,所以背景都使用定位,叠加的方式实现

插件依赖

使用了printjs, printjs官方介绍: A tiny javascript library to help printing from the web.

页面开发及思路

  1. 对页面组件惊醒细化拆分
  2. 对拆分后的组件尽心分组,渲染
# 页面模块结构如下

invoice
|-    index.vue #页面入口文件
|-    hooks.ts # 抽离函数以及数据状态管理
|_    component # 页面组件
    |- ContentRender.vue # 渲染首页头部以及订单信息
    |- InvoiceHeader.vue # 首页头部
    |- OderInvoiceInfo.vue # 订单编号客户信息等
    |- ProductInfoRender.vue # 渲染产品列表
    |- ProductsTable.vue # 产品列表表格
    |- ProductTableTitle.vue # 表单自定义表头
    |- ProductInfoColumn.vue # 产品信息模块
    |- product-info-data.ts # 产品信息
    |- Summation.vue # 产品合计
    |- FeeDtailRender.vue # 产品明细渲染
    |- MergeOrderFeeDetail.vue # 遍历渲染订单明细
    |- OriginOrderFeeDetail.vue # 订单明细组件
    |- PageFooter.vue # 页面通用底部


代码模块书写

依赖数据 product-info-data.ts 呢日用

// table column solts
interface TableColumnSolts {
    title?: string;
    customRender: string;
}
// table column
export interface TableColumnItem {
    dataIndex: string;
    key: string;
    slots?: TableColumnSolts;
    title?: string | Function;
    titleStr?: string;
    tipContent?: string;
    [key: string]: any;
}
// 商品

export const customeTitleColumn: TableColumnItem[] = [
    {
        dataIndex: 'originQuantity',
        key: 'originQuantity',
        titleStr: '原单数量',
        tipContent: '订单第一次创建的数量',
        align: 'center',
        slots: {
            title: 'yuandan',
            customRender: 'quantity',
        },
    },
    {
        dataIndex: 'quantity',
        key: 'quantity',
        titleStr: '订单数量',
        tipContent: '主单+子单数量',
        align: 'center',
        slots: {
            title: 'dingdan',
            customRender: 'orderQuantity',
        },
    },
    {
        dataIndex: 'returnQuantity',
        key: 'returnQuantity',
        titleStr: '退货数量',
        align: 'center',
        tipContent: '主单+子单退货单据状态为[已完成]',
        slots: {
            title: 'tuihuo',
            customRender: 'quantity',
        },
    },
    {
        dataIndex: 'saleQuantity',
        key: 'saleQuantity',
        titleStr: '结算数量',
        align: 'center',
        tipContent: '订单数量-退货数量',
        slots: {
            title: 'jiesuan',
            customRender: 'quantity',
        },
    },
    {
        dataIndex: 'installQuantity',
        key: 'installQuantity',
        titleStr: '安装数量',
        align: 'center',
        tipContent: '安装数量-退货数量',
        slots: {
            title: 'anzhuang',
            customRender: 'quantity',
        },
    },
    {
        dataIndex: 'salesPrice',
        key: 'salesPrice',
        title: '单价(¥)',
        align: 'center',
        slots: {
            customRender: 'Price',
        },
    },
    {
        dataIndex: 'serviceFeeRatio',
        key: 'serviceFeeRatio',
        title: '服务费率',
        align: 'center',
        slots: {
            customRender: 'serviceFeeRatio',
        },
    },
    {
        dataIndex: 'unReturnNum',
        key: 'unReturnNum',
        titleStr: '退货未入库数量',
        tipContent: '产生退货,未进行入库',
        align: 'center',
        width: 85,
        slots: {
            title: 'noInWarehose',
            customRender: 'quantity',
        },
    },
    {
        dataIndex: 'unOrderNum',
        key: 'unOrderNum',
        titleStr: '安装未补单数量',
        tipContent: '已经安装,未进行补单',
        width: 85,
        align: 'center',
        slots: {
            title: 'noCreateOrder',
            customRender: 'quantity',
        },
    },
];
export const columns: TableColumnItem[] = [
    {
        dataIndex: 'index',
        key: 'index',
        title: '序号',
        align: 'center',
        slots: {
            customRender: 'Index',
        },
    },
    {
        dataIndex: 'productTypeName',
        key: 'productTypeName',
        title: '类别',
        width: 100,
        slots: {
            customRender: 'ProductType',
        },
    },
    {
        dataIndex: 'name',
        key: 'name',
        title: '产品名称',
        width: 204,
        slots: {
            customRender: 'ProductInfo',
        },
    },
    ...customeTitleColumn,
];

// 测试 多条数据拓展,自己可以循环多创建一些
export const testFeeList = [
    {
        id: 14,
        code: 'PRODUCT_FEE',
        name: '产品费',
        type: 1,
        value: 1,
        amount: 5664711.2,
        itemNum: 0,
        version: 235,
        required: 1,
        enableState: 1,
        allowDiscount: 1,
        lowestDiscount: 0.8,
        originalAmount: 7080889,
    },
];

export const resData = {
    id: 316,
    orderId: 'SO2022050156496',
    storeId: '8',
    storeName: '凉山门店',
    houseId: '253',
    projectNo: 'PM2022050554572',
    customerName: '张三100',
    customerPhone: '13510001100',
    linkman: '张三100',
    phone: '13510001100',
    principalId: 7,
    principal: '高富帅',
    area: '广东省深圳市南山区',
    type: 1,
    discountAmount: 0,
    adjustAmount: 0,
    amount: 43.29,
    paidAmount: 43.29,
    refundAmount: 0,
    status: 3,
    version: 0,
    productFeeVersion: 235,
    createTime: '2022-05-01T16:31:03',
    statProductAmount: 92.79,
    statServiceAmount: 0,
    statExtendAmount: 14.95,
    statDiscountAmount: 0,
    statAmount: 64.45,
    statPaidAmount: 0,
    statRefundAmount: 0,
    statRealReceiptAmount: 0,
    statChangeAmount: 21.16,
    statOriginAmount: 43.29,
    extendFeeList: [
        {
            id: 10,
            name: '产品费',
            code: 'PRODUCT_FEE',
            originalAmount: 43.29,
            discount: 10,
            amount: 43.29,
            version: 235,
            type: 1,
            value: 100,
            lowestDiscount: 5,
            allowDiscount: 1,
            required: 1,
            itemNum: 0,
        },
        ...testFeeList,
    ],
    itemList: [
        {
            skuId: 56,
            name: '门窗传感器 T1',
            productTypeName: '门窗传感器',
            freeGiftQuantity: 0,
            img:
                'https://retail-cos.aqara.cn/retail_image_prod/dd0bbffc0bdf409599e68a62e838bd49.png',
            sn: '6970504211186',
            specification: '门窗传感器 T1',
            unit: 'Pcs',
            quantity: 1.0,
            returnQuantity: 0.0,
            salesPrice: 43.29,
            serviceFee: 0,
            retailPrice: 43.29,
            discountPrice: 0.0,
            discountAmount: 0.0,
            salesAmount: 43.29,
            paidAmount: 0.0,
            price: 43.29,
            requireService: 0,
            returnAmount: 0.0,
        },
        {
            skuId: 3,
            name:
                'G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机G10摄像机',
            freeGiftQuantity: 0,
            img: 'https://dev-img.aqaramaster.com/uat/product/image/20220110/8/G2H黄-T1.png',
            sn: 'FSZJ-G10-0001',
            specification: '绿色',
            productTypeName:
                '摄像摄像机摄像机摄像机摄像机摄像机摄像机摄像机摄像机摄像机摄像机摄像机摄像机摄像机机',
            unit: '厘米',
            originQuantity: 555.0,
            quantity: 566.0,
            returnQuantity: 566,
            saleQuantity: 566.0,
            installQuantity: 566.0,
            salesPrice: 566.9,
            serviceFee: 566,
            unOrderNum: 566,
            retailPrice: 566666666,
            discountPrice: 566666666,
            discountAmount: 566666666,
            salesAmount: 566666666,
            paidAmount: 566666666,
            price: 5666,
            requireService: 5666,
            returnAmount: 5666,
        },
    ],
    // 子单数据
    subOrders: [
        {
            id: 433,
            orderId: 'SO2022111547851',
            storeId: '8',
            houseId: '253',
            customerName: '张三100',
            customerPhone: '13510001100',
            linkman: '张三100',
            phone: '13510001100',
            principalId: 7,
            principal: '高富帅',
            area: '广东省深圳市南山区',
            type: 1,
            discountAmount: 0.0,
            adjustAmount: 0.0,
            amount: 64.45,
            paidAmount: 0.0,
            refundAmount: 0.0,
            status: 1,
            version: 0,
            createTime: '2022-11-15T15:55:28',
            statProductAmount: 0,
            statServiceAmount: 0,
            statExtendAmount: 0,
            statDiscountAmount: 0,
            statAmount: 0,
            statPaidAmount: 0,
            statRefundAmount: 0,
            statRealReceiptAmount: 0,
            statChangeAmount: 0,
            statOriginAmount: 0,
            extendFeeList: [
                {
                    id: 58,
                    name: '扯皮费',
                    originalAmount: 10,
                    discount: 10,
                    amount: 10,
                    version: 302,
                    type: 2,
                    value: 10,
                    lowestDiscount: 6,
                    allowDiscount: 0,
                    required: 1,
                    itemNum: 0,
                    enableState: 1,
                },
                {
                    id: 24,
                    name: '延保定金-固定非必收不允许打折50',
                    originalAmount: 50.25,
                    amount: 50.25,
                    version: 302,
                    type: 2,
                    value: 50.25,
                    lowestDiscount: 5,
                    allowDiscount: 1,
                    required: 0,
                    itemNum: 0,
                    enableState: 0,
                },
                {
                    id: 22,
                    name: '安装费-比例必收不允许打折10',
                    originalAmount: 4.95,
                    discount: 10,
                    amount: 4.95,
                    version: 302,
                    type: 1,
                    value: 10,
                    lowestDiscount: 10,
                    allowDiscount: 0,
                    required: 1,
                    itemNum: 0,
                    enableState: 1,
                },
                ...testFeeList,
            ],
            itemList: [
                {
                    skuId: 3,
                    name: 'G10摄像机',
                    freeGiftQuantity: 0,
                    img:
                        'https://dev-img.aqaramaster.com/uat/product/image/20220110/8/G2H黄-T1.png',
                    sn: 'FSZJ-G10-0001',
                    specification: '绿色',
                    unit: '厘米',
                    quantity: 5.0,
                    returnQuantity: 0.0,
                    salesPrice: 9.9,
                    serviceFee: 0,
                    retailPrice: 9.9,
                    discountPrice: 0.0,
                    discountAmount: 0.0,
                    salesAmount: 49.5,
                    paidAmount: 0.0,
                    price: 12.89,
                    requireService: 0,
                    returnAmount: 0.0,
                },
            ],
        },
    ],
};

index.vue 内容

<template>
    <div class="order-invoice">
        <div class="order-invoice-header">
            <h3 class="order-invoice-title">结算单</h3>
            <div class="btn-wrap">
                <a-button @click="goBack">
                    返回
                </a-button>

                <a-button @click="printPage">
                    打印结算单
                </a-button>
            </div>
        </div>
        <div class="PrintOrderInvoiceWrap">
            <div class="print-content" id="PrintOrderInvoice">
                <ContentRender :isPrintModel="true" />
                <ProductTableRender :isPrintModel="true" />
                <Summation />
                <FeeDetailRender />
            </div>
        </div>
        <div class="order-invoce-content" id="PrintOrderInvoiceWeb">
            <ContentRender />
            <ProductTableRender />
            <Summation />
            <FeeDetailRender />
        </div>
    </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import ContentRender from './component/ContentRender.vue';
import ProductTableRender from './component/ProductInfoRender.vue';
import Summation from './component/Summation.vue';
import FeeDetailRender from './component/FeeDtailRender.vue';

// const { pageList } = useCalcProductPages();
export default defineComponent({
    name: 'OrderInvoice',
    components: {
        ContentRender, // 结算单信息
        ProductTableRender, // 结算单产品列表
        Summation, // 费用合计
        FeeDetailRender, // 费用明细
    },
    setup() {
        // 返回上一页面
        const goBack = () => {
            router.back();
        };
        
        const printPage = () => {
            
            return false;
        };

        return {
            checkFormInfo,
            printPage,
            goBack,
        };
    },
});
</script>

<style lang="less" scoped>
@import url('~@/less/mixin');
.order-invoice {
    background-color: @white;
    .bR();
    margin-top: 16px;
    padding-bottom: 16px;
    font-size: 12px;
    color: #000;
}
.order-invoice-header {
    .box();
    height: 48px;
    padding: 0 24px;
    border-bottom: 1px solid @border-color;
    .order-invoice-title {
        font-size: @font-xl;
        color: @font-black;
        font-weight: 500;
    }
    .ant-btn {
        margin-left: 10px;
    }
}
.order-invoce-content {
    padding: 0 24px;
}
</style>
<style lang="less">
.PrintOrderInvoiceWrap {
    position: absolute;
    right: 99999999cm;
}
// 打印配置值
#PrintOrderInvoice {
    width: 278mm;
    padding: 12px 32px 32px;
    box-sizing: border-box;
    font-size: 9px;
    color: #000;
    .order-invoice-header {
        .small-title {
            width: 60mm;
            transform: scale(0.76) translate(30px, 13px);
        }
    }
    .print-font {
        transform: scale(0.75);
    }
    .order-invoices-product-table-wrap .order-invoice-table {
        min-width: 278mm;
    }
}
</style>

ProductInfoRender 代码书写

<template>
    <div class="order-invoice-product-render">
        <div class="table-list">
           <!-- table-item 每个item是一页,需要定宽定高 -->
            <div
                class="table-item"
                :class="{
                    otherProductList: key > 0,
                }"
                v-for="(page, key) in pageList"
                :key="key"
            >
                <ProductTable :data="page" :pageIndex="key" />
                <PageFooter
                    :currentPage="key"
                    :style="{
                        marginTop: getMarginButtom(page.length, key) + 'px',
                    }"
                />
                <!-- 需要做换行处理 -->
                <div class="empty-box" v-if="key === 0"></div>
            </div>
        </div>
    </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import ProductTable from './ProductsTable.vue';
import PageFooter from './PageFooter.vue';

export default defineComponent({
    name: 'OrderInvoiceProductRender',
    components: {
        ProductTable,
        PageFooter,
    },
    props: {
        isPrintModel: {
            type: Boolean,
            default: false,
        },
    },
    setup(props) {
        const pageList = []

        const getMarginButtom = (): number  => {
           return 0
        };

        return {
            pageList,
            getMarginButtom,
        };
    },
});
</script>

<style lang="less" scoped>
.table-item {
    margin-bottom: 4px;
}
.otherProductList {
    margin-top: 32px;
    margin-bottom: 0;
}
#PrintOrderInvoice {
    .order-invoice-product-render {
        min-width: 274mm;
        :deep(.page-footer) {
            transform: translateY(28px);
        }
    }
    .empty-box {
        min-height: 58px;
        min-width: 274mm;
    }
    .otherProductList {
        margin-top: 0;
        // min-height: 210mm;
        height: 793px;
        padding-top: 50px;
        min-width: 1036px;
        box-sizing: border-box;
    }
}
// 处理页面在没有加载出来的时候 会有抖动的问题
#PrintOrderInvoiceWeb {
    .order-invoice-product-render {
        min-height: 579px;
    }
}
</style>


ProductsTable.vue 相关代码

由于打印的时候 需要定制话,antd自带的table相对比较麻烦。所以web显示 使用antd的table组件,打印使用自己手写的table

<template>
    <div class="order-invoices-product-table-wrap">
        <!-- 只有在网页并且第一个产品列表才显示 -->
        <div class="info-tip-column" v-if="pageIndex === 0 && !isPrintModel">
            <div class="info-tip-item">结算单</div>
            <div class="info-tip-item">需退库(补单)的产品数量</div>
        </div>
        <div class="custom-table-wrap" v-if="isPrintModel">
            <div class="print-table-bg-div"></div>
            <div class="print-table-title-line"></div>
            <table class="print-table-header">
                <thead>
                    <tr class="title-tr">
                        <th v-for="column in initColumns()" :key="column.key">
                            {{ column.title ? column.title : column.titleStr }}
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(info, idx) in data" :key="info.skuId">
                        <td class="num-td">
                            <div class="tab-td-content-wrap">{{ idx + 1 }}</div>
                        </td>
                        <td class="product-type-td">
                            <div class="tab-td-content-wrap">{{ info.productTypeName }}</div>
                        </td>
                        <td class="product-name-td">
                            <ProductInfoColumn :info="info" />
                        </td>
                        <td class="num-td">
                            <div class="tab-td-content-wrap">{{ info.originQuantity }}</div>
                        </td>
                        <!-- 订单数量 -->
                        <td class="num-td">
                            <div class="tab-td-content-wrap">{{ info.quantity }}</div>
                        </td>
                        <td class="num-td">
                            <div class="tab-td-content-wrap">{{ info.returnQuantity }}</div>
                        </td>
                        <td class="num-td">
                            <div class="tab-td-content-wrap">{{ info.saleQuantity }}</div>
                        </td>
                        <td class="num-td">
                            <div class="tab-td-content-wrap">{{ info.installQuantity }}</div>
                        </td>

                        <td class="num-td">
                            <div class="tab-td-content-wrap">
                                {{ filterPrice(info.salesPrice) }}
                            </div>
                        </td>
                        <td class="num-td">
                            <div class="tab-td-content-wrap">{{ info.serviceFeeRatio }}%</div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <a-table
            :columns="initColumns()"
            :data-source="data"
            size="small"
            :pagination="false"
            :bordered="true"
            :style="{ marginBottom: getMarginButtom() + 'px' }"
            :rowKey="(record) => record.skuId"
            class="order-invoice-table"
            v-else
        >
            <!-- 序号 -->
            <template #Index="{ index }">
                <div>{{ index + 1 }}</div>
            </template>
            <template #ProductType="{ text }">
                <div class="ProductTypeDiv">{{ text }}</div>
            </template>
            <template #ProductInfo="{ record }">
                <ProductInfoColumn :info="record" />
            </template>
            <template #name="{ text }">
                <a>{{ text }}</a>
            </template>
            <!-- 价格 -->
            <template #Price="{ text }">
                <div>¥{{ filterPrice(text || 0) }}</div>
            </template>
            <!-- 数量 -->
            <template #orderQuantity="{ text, record }">
                <div>
                    {{ text || 0
                    }}{{ record.freeGiftQuantity ? ` 赠品:${record.freeGiftQuantity}` : '' }}
                </div>
            </template>
            <!-- 数量 -->
            <template #quantity="{ text }">
                <div>{{ text || 0 }}</div>
            </template>
            <!-- 服务费率 -->
            <template #serviceFeeRatio="{ text }">
                <div>{{ text || 0 }}%</div>
            </template>
            <!-- 处理自定义title -->
            <template
                v-for="item in customeTitleColumn"
                :key="item.slots.title"
                #[item.slots.title]
            >
                <ProductTableTitle
                    :info="item"
                    :isPrintModel="isPrintModel"
                    :class="{
                        width78: ['noInWarehose', 'noCreateOrder'].includes(item.slots.title),
                    }"
                />
            </template>
        </a-table>
    </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue';
import { filterPrice } from '@/components/calculate-price/calculate';
import { columns, customeTitleColumn, TableColumnItem } from './product-info-data';
import ProductTableTitle from './ProductTableTitle.vue';
import ProductInfoColumn from './ProductInfoColumn.vue';

const { calcProductTableMarginBottom } = useCalcProductPages();
export default defineComponent({
    name: 'OrderInvoicesProductsTable',
    props: {
        data: {
            type: Array as PropType<any[]>,
            default() {
                return [];
            },
        },
        pageIndex: {
            type: Number as PropType<number>,
            default: 0,
        },
        isPrintModel: {
            type: Boolean,
            default: false,
        },
    },
    components: {
        ProductTableTitle, // 自定义提示表头
        ProductInfoColumn, // 产品信息
    },
    setup(props: { pageIndex: number; data: any[]; isPrintModel: boolean }) {
        const getMarginButtom = (): number => {
            // 首页margin bottom 处理
            const marginBottom = 10

            return marginBottom;
        };
        // 打印模式下 部分信息不打印
        const initColumns = (): TableColumnItem[] => {
            if (props.isPrintModel) {
                return columns.slice(0, -2);
            }
            return columns;
        };
        // 这个函数不重要 就不写了
        const filterPrice = (price: any): any => price;
        return {
            initColumns,
            customeTitleColumn,
            getMarginButtom,
            filterPrice, // 价格过滤函数
        };
    },
});
</script>

<style lang="less" scoped>
@lineHeight: 44px;
.order-invoices-product-table-wrap {
    position: relative;
    .ant-table-wrapper .ant-table-body tr > td {
        padding: 5px;
    }
    .width85 {
        width: 78px;
    }
    .order-invoice-table {
        margin-bottom: 16px;
    }
    .info-tip-column {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 39px;
        background: #d6d7d7;
        text-align: center;
        border-bottom: 2px solid #000;
    }
    .info-tip-item:nth-of-type(1) {
        flex: 1;
    }
    .info-tip-item:nth-of-type(2) {
        min-width: 170px;
        line-height: 38px;
        border-left: 1px solid #62647a;
    }
    :deep(.ant-table .ant-table-content .ant-table-thead tr) {
        height: 39px;
    }
    :deep(.ant-table .ant-table-content .ant-table-thead tr th) {
        background: #d6d7d7;
        padding: 0 !important;
        line-height: 19px;
        border-color: #d6d7d7;
        border-bottom-color: #3e415c;
    }
    :deep(.ant-table.ant-table-small
            > .ant-table-content
            > .ant-table-body
            > table
            > .ant-table-tbody
            > tr
            > td) {
        padding: 0 5px !important;
        height: @lineHeight;
    }
    .ProductTypeDiv {
        max-width: 100px;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
        white-space: break-spaces;
    }

    .print-table-header {
        width: 100%;
        min-width: 274mm;
        max-width: 274mm;
        position: relative;
        border: 2px solid #dfdfdf;
        border-collapse: collapse;
        z-index: 2;
        th {
            height: @lineHeight;
            text-align: center;
            transform: scale(0.76);
        }
        td {
            height: @lineHeight;
            border: 2px solid #dfdfdf;
            transform: scale(0.76);
        }
        .title-tr {
            border-bottom: 2px solid #000;
            position: relative;
            z-index: 2;

            // background: #d6d7d7;
            height: @lineHeight;
        }

        .product-name-td {
            width: 204px;
        }
        .num-td {
            text-align: center;
        }
        :deep(.product-img) {
            scale: 1.2;
        }
        .tab-td-content-wrap {
            min-width: 120%;
            transform: translate(-8%, -42%);
            max-width: 60px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            position: absolute;
        }
        .product-type-td {
            min-width: 60px;
        }
    }
    .print-table-bg-div {
        border-bottom: @lineHeight solid #d6d7d7;
        width: 100%;
        min-width: 274mm;
        position: absolute;
        z-index: 1;
    }
    .print-table-title-line {
        border-bottom: 2px solid #000;
        width: 100%;
        min-width: 274mm;
        position: absolute;
        z-index: 4;
        transform: translateY(44px);
    }
}
#PrintOrderInvoice {
    .order-invoices-product-table-wrap {
        font-size: 9px;
    }
}
</style>

ContentRender 代码书写

<template>
    <div class="order-invoice-content-render">
        <InvoiceHeader v-if="isPrintModel" />
        <OrderInvoiceInfo />
    </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import InvoiceHeader from './InvoiceHeader.vue';
import OrderInvoiceInfo from './OderInvoiceInfo.vue';

export default defineComponent({
    name: 'OrderInvoceContentRender',
    components: {
        InvoiceHeader,
        OrderInvoiceInfo,
    },
    props: {
        isPrintModel: {
            type: Boolean,
            default: false,
        },
    },
    setup() {
        return {};
    },
});
</script>

<style>
.order-invoice-content-render {
    background-color: #fff;
    min-width: 274mm;
}
</style>

OriginOrderFeeDetail.vue 费用详情相关

订单汇急速一个固定的页面,这里不补齐,只有这个费用明细 需要合并 多个,以及是否展示页面通用底部 所贴上来

<template>
    <div
        class="origin-order-fee-detail"
        :class="{
            feeDetailTop: info && info.showFooter,
            lastFeeDetailTop: info && info.isLastFeeDetail,
        }"
    >
        <div class="origin-order-fee-title-bg"></div>
        <div class="origin-order-fee-title">
            <div class="scale-font">{{ title }}({{ info ? info.orderId : '' }})</div>
        </div>
        <div class="price-info-wrap" :style="{ ...(info ? info.marginBottom : {}) }">
            <div
                class="price-info-content"
                v-for="(item, key) in getExtendFeeList(info)"
                :key="key"
            >
                <div class="price-info-item">
                    <div
                        class="price-info-item-label"
                        :class="{ overLength: item.name && item.name.length > 14 }"
                        :title="item.name"
                    >
                        <div class="scale-font">{{ item.name }}</div>
                    </div>
                    <div class="price-info-item-value">
                        <div class="scale-font">{{ item.originalAmount }}</div>
                    </div>
                </div>
                <div class="price-info-item">
                    <div class="price-info-item-label">
                        <div class="scale-font">折扣</div>
                    </div>
                    <div class="price-info-item-value">
                        <div class="scale-font">{{ item.discount }}</div>
                    </div>
                </div>
                <div class="price-info-item">
                    <div class="price-info-item-label">
                        <div class="scale-font">应收金额</div>
                    </div>
                    <div class="price-info-item-value">
                        <div class="scale-font">{{ item.amount }}</div>
                    </div>
                </div>
            </div>
        </div>
        <PageFooter v-show="info ? info.showFooter : false" :currentPage="currentPage" />
    </div>
</template>

<script lang="ts">
import { computed, defineComponent, PropType } from 'vue';
import { OrderFeeDetailInfo, CostConfigItem } from '../hooks';
import PageFooter from './PageFooter.vue';

export default defineComponent({
    name: 'OriginOrderFeeDetail',
    components: {
        PageFooter,
    },
    props: {
        title: {
            type: String,
            default: '订单-费用明细',
        },
        info: {
            type: Object as PropType<OrderFeeDetailInfo>,
        },
        startIndex: {
            type: Number,
            default: 0,
        },
    },
    setup(props) {
        const getExtendFeeList = (info: OrderFeeDetailInfo | null): CostConfigItem[] => {
            if (info) {
                return info.extendFeeList;
            }
            return [];
        };
        const currentPage = computed(() => {
            const index: number = props.info ? props.info.pageIndex + props.startIndex : 0;
            return index;
        });
        return {
            getExtendFeeList,
            currentPage,
        };
    },
});
</script>

<style lang="less" scoped>
@height: 30px;
@printWidth: 274mm;
.origin-order-fee-detail {
    text-align: center;

    color: #000;
    padding-bottom: 16px;
    &.feeDetailTop {
        margin-bottom: 0;
    }
    .origin-order-fee-title {
        height: @height;
        line-height: @height;
        background: #d6d7d7;
        border-bottom: 2px solid #000;
        text-align: center;
    }
    .price-info-wrap {
        border-bottom: 1px solid #dfdfdf;
        border-left: 1px solid #dfdfdf;
        .price-info-content .price-info-item:nth-of-type(3) {
            border-right-width: 1px;
        }
    }
    .price-info-content {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        flex-wrap: wrap;
        border: 1px solid #dfdfdf;
        border-bottom: 0;
        text-align: right;
        min-width: 100%;
    }
    .price-info-item {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: flex-start;
        border-bottom: 1px solid #dfdfdf;
        border-right: 2px solid #dfdfdf;
    }
    .price-info-item-label,
    .price-info-item-value {
        min-width: 50%;
        line-height: @height;
        padding: 0 8px;
        box-sizing: border-box;
        height: @height;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .price-info-item-label {
        border-right: 2px solid #dfdfdf;
    }
}

#PrintOrderInvoice {
    .origin-order-fee-detail {
        min-width: @printWidth;
        position: relative;
        margin-top: -2px;
        .price-info-item,
        .price-info-item-label {
            border-width: 2px;
        }
    }
    .scale-font {
        width: 132%;
        overflow: hidden;
        transform: scale(0.76) translate(-17%, 6%);
    }

    .lastFeeDetailTop {
        padding-top: 28px;
        margin-bottom: 0;
    }
    .origin-order-fee-title,
    .price-info-wrap {
        min-width: @printWidth;
    }
    .origin-order-fee-title {
        position: relative;
        z-index: 3;
        background: transparent;
        .scale-font {
            transform: scale(0.76) translate(5%, 0);
        }
    }
    .origin-order-fee-title-bg {
        border-bottom: @height solid #d6d7d7;
        min-width: @printWidth;
        position: absolute;
        z-index: 1;
    }
}
</style>

PageFooter.vue

主要显示自定义脚注信息,分页信息等

<template>
    <div class="page-footer">
        <div class="info-item">
            {{ checkFormInfo.storeName }}
            /{{ checkFormInfo.principal }} / {{ checkFormInfo.principalPhone }}
        </div>
        <div class="info-item right-info-item">
            第 {{ currentPage + 1 }} 页/共 {{ paginationInfo.totalPageSize }} 页
        </div>
    </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import { useLoadCheckFormInfo, usePaginationInfo } from '../hooks';

export default defineComponent({
    name: 'OrderInvoicePageFooter',
    props: {
        currentPage: {
            type: Number,
            default: 0,
        },
    },
    setup() {
        const { checkFormInfo } = useLoadCheckFormInfo();
        const { paginationInfo } = usePaginationInfo();
        return { checkFormInfo, paginationInfo };
    },
});
</script>

<style lang="less" scoped>
.page-footer {
    line-height: 20px;
    color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
#PrintOrderInvoice {
    .page-footer {
        min-width: 274mm;
        font-size: 9px;
        .info-item {
            width: 160mm;
            text-align: left;
            transform: scale(0.76) translate(-14%);
        }
        .right-info-item {
            width: 60mm;
            text-align: right;
            transform: scale(0.76) translate(14%);
        }
    }
}
</style>

总结

  1. 以上 主要贴出了主要代码
  2. 关于分页,为了方便处理 将产品列表,收费明细进行了组件化渲染,方便进行控制以及固定宽高(物理打印A4是固定的宽高)
  3. 其次 web展示和打印是有显示差异化 js逻辑 使用一个isPrintModel判断,css相关的使用一个标签包裹,并设置id=PrintOrderInvoice,printjs打印需要一个dom id 作为选择器,所以 就使用id作为趣分类额
  4. 最后,关于计算的逻辑 我放到另外一个开个一篇,否则代码量太大,没法看
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐