瑞利声电固定资产管理系统
ycq
2026-10-16 dbbadb021c2fc171466cc82bd9a7a82cc47165f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<%@ page language="java" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打印资产标签</title>
    <link href="static/plugins/font-awesome/css/font-awesome.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="static/plugins/layer/skin/default/layer.css" type="text/css">
    <script type="text/javascript" src="static/plugins/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="static/plugins/layer/layer.js"></script>
    <script type="text/javascript" src="static/plugins/qrcode-generator/qrcode.js"></script>
    <script type="text/javascript" src="static/fams/label/label-render.js"></script>
    <style id="pageStyle"></style>
    <style>
        html, body { margin: 0; font-family: "Microsoft YaHei", sans-serif; font-size: 13px; color: #333; }
        .toolbar { position: sticky; top: 0; z-index: 10; background: #fff; border-bottom: 1px solid #e5e5e5; padding: 10px 16px; display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }
        .toolbar select, .toolbar input { height: 28px; border: 1px solid #ccc; padding: 0 6px; box-sizing: border-box; }
        .toolbar input { width: 300px; }
        .btn { display: inline-block; padding: 5px 14px; border-radius: 3px; cursor: pointer; border: 1px solid #3f9ae0; background: #3f9ae0; color: #fff; }
        .btn:hover { opacity: .9; }
        .hint { color: #999; font-size: 12px; }
        .sheet { padding: 16px; display: flex; flex-wrap: wrap; gap: 12px; background: #f0f2f5; min-height: 300px; }
        .sheet .fams-label { box-shadow: 0 0 0 1px #bbb; }
        .empty { padding: 40px; color: #999; }
        @media print {
            .toolbar { display: none; }
            html, body { background: #fff; }
            .sheet { display: block; padding: 0; background: #fff; }
            .sheet .fams-label { box-shadow: none; page-break-after: always; break-after: page; }
            .sheet .fams-label:last-child { page-break-after: auto; break-after: auto; }
        }
    </style>
</head>
<body>
<div class="toolbar">
    <span>标签模板:<select id="templateSelect"></select></span>
    <span class="btn" id="printBtn"><i class="fa fa-print"></i> 打印</span>
    <span id="summary" class="hint"></span>
    <div class="hint" style="width:100%;">
        每张标签单独一页,纸张大小 = 标签尺寸;请在打印对话框中选择标签打印机,边距设为“无”,缩放 100%。
    </div>
</div>
<div class="sheet" id="sheet"><div class="empty">正在加载…</div></div>
<script type="text/javascript">
    var FIELDS = ${fieldsJson};
    // 列表页通过 layer 打开本页后会写入 params.ids
    var params = {ids: null, callBack: null};
    var templates = [];
    var assets = [];
    var assetIds = '';
 
    $(function () {
        $('#templateSelect').on('change', renderLabels);
        $('#printBtn').on('click', doPrint);
        waitForIds(0);
    });
 
    function waitForIds(tries) {
        var fromUrl = (location.search.match(/[?&]ids=([^&]*)/) || [])[1];
        var ids = params.ids && params.ids.length ? params.ids.join(',') : (fromUrl ? decodeURIComponent(fromUrl) : '');
        if (ids) {
            assetIds = ids;
            loadData();
        } else if (tries < 30) {
            setTimeout(function () { waitForIds(tries + 1); }, 100);
        } else {
            $('#sheet').html('<div class="empty">没有选中资产,请回到列表勾选后再打印。</div>');
        }
    }
 
    function loadData() {
        $.get('fams/label/templates', function (data) {
            if (!data.success) { return showError(data); }
            templates = data.result;
            if (!templates.length) {
                $('#sheet').html('<div class="empty">还没有启用的标签模板,请先到“标签模板”中维护。</div>');
                return;
            }
            $.each(templates, function (i, t) {
                $('#templateSelect').append($('<option></option>').val(i).text(t.TEMPLATE_NAME + '(' + t.LABEL_WIDTH + '×' + t.LABEL_HEIGHT + 'mm)'));
            });
            $.post('fams/label/printData', {ids: assetIds}, function (res) {
                if (!res.success) { return showError(res); }
                assets = res.result;
                renderLabels();
            });
        });
    }
 
    function currentTemplate() {
        return templates[parseInt($('#templateSelect').val(), 10) || 0];
    }
 
    function renderLabels() {
        var template = currentTemplate();
        if (!template) { return; }
        var $sheet = $('#sheet').empty();
        $.each(assets, function (i, asset) {
            $sheet.append(FamsLabel.render(template, asset, {fields: FIELDS}));
        });
        if (!assets.length) {
            $sheet.html('<div class="empty">选中的资产不存在或已删除。</div>');
        }
        $('#pageStyle').text('@page { size: ' + template.LABEL_WIDTH + 'mm ' + template.LABEL_HEIGHT + 'mm; margin: 0; }');
        $('#summary').text('共 ' + assets.length + ' 张标签');
    }
 
    function doPrint() {
        var template = currentTemplate();
        if (!template || !assets.length) { return; }
        printAndRecord(template);
    }
 
    function printAndRecord(template) {
        window.print();
        layer.confirm('标签是否已成功打印?确认后将记录打印并标记为已打印。', {btn: ['已打印', '未打印']}, function (index) {
            layer.close(index);
            var ids = $.map(assets, function (a) { return a.ID; }).join(',');
            $.post('fams/label/printed', {ids: ids, templateId: template.ID}, function (res) {
                if (!res.success) { return showError(res); }
                layer.msg('已记录 ' + res.result + ' 张标签的打印');
                if (params.callBack) { params.callBack(); }
            });
        });
    }
 
    function showError(data) {
        layer.alert((data && data.error && data.error.message) || '操作失败,请稍后重试');
    }
</script>
</body>
</html>