schangxiang@126.com
2025-09-10 3d43ffa3152110b7823f9fa6320c08a6ae02358a
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<template>
  <div :style="canvasStyle" class="canvas-container" @mousedown="selectWidget=null;">
    <div class="canvas-content">
      <draggable v-selectable="data.fields" v-model="data.fields" :options="{group:'people', ghostClass: 'ghost', filter:'.detail-table'}" class="drag-list" @end="handleMoveEnd" @add="handleWidgetAdd">
        <template v-for="(element, index) in data.fields">
          <DraggableResizable v-bind.sync="element.options" :key="index" :class="{'active': isActive(element)}" :style="'z-index:' + element.options.z" :data-key="element.key" class="resable-item" @click.native.stop="handleSelectWidget(index)">
            <template v-if="element.type=='table'">
              <table :style="{'border-color':element.borderColor, 'width':element.width}" class="detail-table" @mousedown.stop>
                <thead v-if="element.showHeader">
                  <draggable v-model="element.fields" :options="{group:'detail', ghostClass: 'ghost', animation:300, chosenClass:'chosen-item'}" :class="{'drag-th':true}" tag="tr">
                    <th v-for="(th, thIndex) in element.fields" :key="thIndex" :style="{'border-color':element.borderColor, 'width':th.width+'px'}" :class="{'drag-active': isActive(th)}" @click.stop="handleThSelectWidget(th)">
                      <div :style="getHeaderDivStyle(th)" class="cell">{{ th.label }}</div>
                      <div class="tool">
                        <el-button title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleDetailWidgetDelete(element.fields, thIndex)">
                          <i class="el-icon-yrt-shanchu2"></i>
                        </el-button>
                      </div>
                    </th>
                  </draggable>
                </thead>
                <tbody>
                  <tr v-for="i in [0]" :key="'tr'+i">
                    <td v-for="(th, thIndex) in element.fields" :key="thIndex" :style="{'border-color':element.borderColor}">
                      <div class="cell"></div>
                    </td>
                  </tr>
                </tbody>
              </table>
              <div class="tool">
                <el-button title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleWidgetDelete(index)">
                  <i class="el-icon-yrt-shanchu2"></i>
                </el-button>
                <el-button title="添加字段" class="widget-action-addplus" circle plain type="primary" @click.stop="handleDetailWidgetAdd(element, index)">
                  <i class="el-icon-yrt-addplus"></i>
                </el-button>
              </div>
            </template>
            <template v-else-if="element.type=='field'">
              <div :style="element.styles" :class="{'drag-active': isActive(element)}" class="text-box">
                <div :style="element.label.styles" class="label">{{ element.label.title }}
                  <template v-if="element.label.showColon">:</template>
                </div>
                <div :style="element.text.styles" class="text">{ {{ element.text.prop }} }</div>
              </div>
              <div class="tool">
                <el-button v-if="element.options.w>50" title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleWidgetDelete(index)">
                  <i class="el-icon-yrt-shanchu2"></i>
                </el-button>
                <!-- <el-button title="复制" class="widget-action-clone" circle plain type="primary" @click.stop="handleWidgetClone(index)">
                  <i class="el-icon-yrt-fuzhi4"></i>
                </el-button> -->
              </div>
            </template>
            <template v-else-if="element.type=='barcode'">
              <div :style="element.styles" :class="{'drag-active': isActive(element)}" class="text-box">
                <vue-barcode :value="element.barcode.value" :options="element.barcode.options"></vue-barcode>
              </div>
              <div class="tool">
                <el-button v-if="element.options.w>50" title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleWidgetDelete(index)">
                  <i class="el-icon-yrt-shanchu2"></i>
                </el-button>
                <!-- <el-button title="复制" class="widget-action-clone" circle plain type="primary" @click.stop="handleWidgetClone(index)">
                  <i class="el-icon-yrt-fuzhi4"></i>
                </el-button> -->
              </div>
            </template>
            <template v-else-if="element.type=='qrcode'">
              <div :style="element.styles" :class="{'drag-active': isActive(element)}" class="text-box">
                <vue-qrcode :value="element.qrOptions.value" :options="element.qrOptions"></vue-qrcode>
              </div>
              <div class="tool">
                <el-button v-if="element.options.w>50" title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleWidgetDelete(index)">
                  <i class="el-icon-yrt-shanchu2"></i>
                </el-button>
              </div>
            </template>
            <!-- 图片上传 -->
            <template v-else-if="element.type=='image'">
              <div :style="element.styles" :class="{'drag-active': isActive(element)}" class="text-box">
                <el-upload :action="common.domain + element.image.serverAction" :show-file-list="false" :http-request="(params) => {uploadHttp(params, element)}" :before-upload="handleBeforeUpload" :headers="headers" :style="{width:element.image.width+'mm', height:element.image.height+'mm'}" class="img-uploader">
                  <img v-if="element.image.imageUrl" :src="element.image.imageUrl" :style="{width:element.image.width+'mm', height:element.image.height+'mm'}" class="img">
                  <i v-else class="el-icon-plus img-uploader-icon"></i>
                </el-upload>
              </div>
              <div class="tool">
                <el-button v-if="element.options.w>50" title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleWidgetDelete(index)">
                  <i class="el-icon-yrt-shanchu2"></i>
                </el-button>
                <!-- <el-button title="复制" class="widget-action-clone" circle plain type="primary" @click.stop="handleWidgetClone(index)">
                  <i class="el-icon-yrt-fuzhi4"></i>
                </el-button> -->
              </div>
            </template>
            <template v-else-if="['line-horizontal', 'line-vertical'].indexOf(element.type)>=0">
              <div :style="element.styles" :class="{'drag-active': isActive(element)}" class="text-box">
              </div>
              <div class="tool">
                <el-button v-if="element.options.w>50" title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleWidgetDelete(index)">
                  <i class="el-icon-yrt-shanchu2"></i>
                </el-button>
                <!-- <el-button title="复制" class="widget-action-clone" circle plain type="primary" @click.stop="handleWidgetClone(index)">
                  <i class="el-icon-yrt-fuzhi4"></i>
                </el-button> -->
              </div>
            </template>
            <template v-else-if="['pagination'].indexOf(element.type)>=0">
              <div :style="element.styles" :class="{'drag-active': isActive(element)}" class="pagination-box">
                <div class="label">{{ element.layout }}</div>
              </div>
              <div class="tool">
                <el-button v-if="element.options.w>50" title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleWidgetDelete(index)">
                  <i class="el-icon-yrt-shanchu2"></i>
                </el-button>
                <!-- <el-button title="复制" class="widget-action-clone" circle plain type="primary" @click.stop="handleWidgetClone(index)">
                  <i class="el-icon-yrt-fuzhi4"></i>
                </el-button> -->
              </div>
            </template>
            <template v-else>
              <div :style="element.styles" :class="{'drag-active': isActive(element)}" class="text-box">
                <div :style="element.label.styles" class="label">{{ element.label.title }}</div>
              </div>
              <div class="tool">
                <el-button v-if="element.options.w>50" title="删除" class="widget-action-delete" circle plain type="danger" @click.stop="handleWidgetDelete(index)">
                  <i class="el-icon-yrt-shanchu2"></i>
                </el-button>
                <!-- <el-button title="复制" class="widget-action-clone" circle plain type="primary" @click.stop="handleWidgetClone(index)">
                  <i class="el-icon-yrt-fuzhi4"></i>
                </el-button> -->
              </div>
            </template>
          </DraggableResizable>
        </template>
      </draggable>
    </div>
  </div>
</template>
 
<script>
import { selectable } from "./directive/selectable.js";
import Draggable from "vuedraggable";
import DraggableResizable from "@/components/base/draggable-resizable";
import VueBarcode from "@xkeshi/vue-barcode";
import VueQrcode from "@chenfengyuan/vue-qrcode";
import ossClient from "@/utils/aliyun.oss.client";
 
export default {
  components: {
    Draggable,
    DraggableResizable,
    VueBarcode,
    VueQrcode
  },
  directives: {
    selectable
  },
  props: {
    data: {
      type: Object,
      default: null
    },
    select: {
      type: Object,
      default: null
    },
    configType: {
      type: String,
      default: null
    },
    detailFields: {
      type: Array,
      default: null
    }
  },
  data() {
    return {
      selectWidget: this.select
    };
  },
  computed: {
    currentConfigType: {
      get: function() {
        return this.configType;
      },
      set: function(val) {
        this.$emit("update:config-type", val);
      }
    },
    // 当前选中明细列表
    currentDetailFields: {
      get: function() {
        return this.detailFields;
      },
      set: function(val) {
        this.$emit("update:detail-fields", val);
      }
    },
    // 获得当前是否激活
    isActive: function() {
      return function(th) {
        var active = this.selectWidget && this.selectWidget.key === th.key;
        return active;
      };
    },
    // 画布样式设置
    canvasStyle: function() {
      var opts = this.data.dataOptions;
      return {
        width: opts.width + "mm",
        height: opts.height + "mm",
        "padding-top": opts.paddingTop + "mm",
        "padding-bottom": opts.paddingBottom + "mm",
        "padding-left": opts.paddingLeft + "mm",
        "padding-right": opts.paddingRight + "mm"
      };
    },
    // 上传header参数
    headers: function() {
      var timestamp = new Date().valueOf();
      var nonce = Math.random()
        .toString(36)
        .substr(2);
      var guid = this.common.getUserGuid();
      var userInfo = this.common.getUserInfo();
      var accessToken = userInfo.accessToken;
      var tokenInfo = this.common.getTokenInfo();
      var params = {
        noDataSign: true
      };
 
      var signature = this.common.getSignature(timestamp, nonce, tokenInfo, params);
      var opts = {
        timestamp: timestamp,
        nonce: nonce,
        guid: guid,
        accessToken: accessToken,
        signature: signature,
        subFolder: "print", // 设置子文件夹
        noDataSign: true // 数据不参与加密
      };
      return opts;
    }
  },
  watch: {
    select(val) {
      this.selectWidget = val;
    },
    selectWidget: {
      handler(val) {
        this.$emit("update:select", val);
      },
      deep: true
    },
    data: {
      handler(val) {},
      deep: true
    }
  },
  methods: {
    handleMoveEnd({ newIndex, oldIndex }) {},
    handleMoveStart({ oldIndex }) {},
    // 元素选中
    handleSelectWidget(index) {
      this.selectWidget = this.data.fields[index];
      this.currentConfigType = "WidgetConfig";
    },
    // 明细单元格选中
    handleThSelectWidget(th, index) {
      this.selectWidget = th;
      this.currentConfigType = "WidgetConfig";
    },
    handleWidgetAdd(evt) {
      const newIndex = evt.newIndex;
 
      // 为拖拽到容器的元素添加唯一 key
      const key = Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999);
      var newField = JSON.parse(JSON.stringify(this.data.fields[newIndex]));
      this.$set(this.data.fields, newIndex, { ...newField, key });
      delete this.data.fields[newIndex].icon;
 
      if (this.data.fields[newIndex].type === "table") {
        var _fields = this.data.fields[newIndex].fields;
        _fields.forEach((_field, _index) => {
          const _key = Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999) + _index;
          this.$set(_field, "key", _key);
        });
      }
 
      this.currentConfigType = "WidgetConfig";
      this.selectWidget = this.data.fields[newIndex];
    },
    // 删除
    handleWidgetDelete(index) {
      if (this.data.fields.length - 1 === index) {
        if (index === 0) {
          this.selectWidget = {};
        } else {
          this.selectWidget = this.data.fields[index - 1];
        }
      } else {
        this.selectWidget = this.data.fields[index + 1];
      }
 
      this.$nextTick(() => {
        this.data.fields.splice(index, 1);
      });
    },
    // 复制
    handleWidgetClone(index) {
      const cloneData = {
        ...JSON.parse(JSON.stringify(this.data.fields[index])),
        key: Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999)
      };
      cloneData.options.x += 5;
      cloneData.options.y += 5;
 
      this.data.fields.splice(index, 0, cloneData);
 
      this.$nextTick(() => {
        this.selectWidget = this.data.fields[index + 1];
      });
    },
    // 删除明细字段
    handleDetailWidgetDelete(fields, index) {
      if (fields.length - 1 === index) {
        if (index === 0) {
          this.selectWidget = {};
        } else {
          this.selectWidget = fields[index - 1];
        }
      } else {
        this.selectWidget = fields[index + 1];
      }
 
      this.$nextTick(() => {
        fields.splice(index, 1);
      });
    },
    // 新增明细字段
    handleDetailWidgetAdd(element, index) {
      this.$set(element.fields, element.fields.length, {
        type: "detail-field",
        prop: "",
        width: 150,
        header: {
          fontSize: 14,
          textAlign: "center",
          lineHeight: 20
        },
        body: {
          fontSize: 14,
          textAlign: "left",
          lineHeight: 20
        },
        label: "字段" + element.fields.length,
        key: Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999)
      });
    },
    // 表格按下事件
    onThMouseDown(field, $event) {},
    // 上传至阿里云
    uploadHttp(params, element) {
      const fileName = `upload/print/${this.common.formatDate(new Date(), "YYYY-MM-DD")}/${new Date().valueOf()}-${this.common.getGUID()}.jpg`; // 定义唯一的文件名
      ossClient(this.common.uploadConf)
        .put(fileName, params.file, {
          ContentType: "image/jpeg"
        })
        .then(({ res, url, name }) => {
          if (res && res.status === 200) {
            element.image.imageUrl = res.requestUrls[0];
          }
        })
        .catch(err => {
          this.$message.error(`上传图片失败` + err.message);
        });
    },
    // 上传图片前
    handleBeforeUpload(file) {
      const isImg = ["image/jpeg", "image/png"].indexOf(file.type) >= 0;
      const isLt2M = file.size / 1024 / 1024 < 2;
 
      if (!isImg) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isImg && isLt2M;
    },
    // 表格头部div样式
    getHeaderDivStyle(th) {
      const data = { "font-size": th.header.fontSize + "mm", "line-height": th.header.lineHeight + "mm", "text-align": th.header.textAlign };
      if (th.width) {
        data.width = th.width + "px";
      }
      return data;
    }
  }
};
</script>
 
<style lang="scss" scoped>
.canvas-container {
  -moz-box-shadow: 0px 0px 10px #3d3d3d;
  -webkit-box-shadow: 0px 0px 10px #3d3d3d;
  box-shadow: 0px 0px 10px #3d3d3d;
  margin: 20px 6px;
  .canvas-content {
    height: 100%;
    padding-bottom: 0px;
    border: 1px dotted #000000;
    .drag-list {
      height: 100%;
      position: relative;
    }
 
    .resable-item {
      &.active {
        border: 1px dashed #888;
      }
      &.selected {
        border: 1px dashed #888;
      }
    }
    .tool {
      display: none;
      position: absolute;
      right: 0;
      bottom: -30px;
      z-index: 100;
    }
    .active {
      background-color: rgb(213, 234, 255);
      > .tool {
        display: block;
      }
    }
    .text-box {
      overflow: hidden;
      white-space: nowrap;
      .label {
        width: 120px;
        display: inline-block;
        text-align: right;
      }
      .text {
        display: inline-block;
      }
    }
  }
 
  .ghost {
    background: #fff;
    border: 1px dashed #409eff;
 
    &::after {
      background: #fff;
    }
  }
 
  li.ghost {
    height: 30px;
    list-style: none;
    font-size: 0;
  }
 
  .detail-table {
    border-collapse: collapse;
    border: 1px solid #dcdfe6;
    background-color: white;
    th,
    td {
      font-weight: normal;
      position: relative;
      border-collapse: collapse;
      border: 1px solid #dcdfe6;
      padding: 3px 2px;
      .cell {
        min-height: 20px;
      }
      &.drag-active {
        background-color: rgb(213, 234, 255);
        .tool {
          display: block;
        }
      }
    }
  }
 
  // 选择框
  /deep/ .selectable {
    position: absolute;
    width: 0px;
    height: 0px;
    background-color: #409eff20;
    border: 1px solid #409eff70;
  }
 
  // 图片上传
  /deep/ .img-uploader {
    .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      &:hover {
        border-color: #409eff;
      }
    }
    .img-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 100px;
      height: 60px;
      line-height: 60px;
      text-align: center;
    }
    .img {
      width: 100px;
      height: 60px;
      display: block;
    }
  }
}
</style>