schangxiang@126.com
2025-09-17 c0e20b1b0f1298cdc0af0037d60e74d51e364501
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
<template xlang="wxml" minapp="mpvue">
    <view class="tki-barcode">
        <!-- #ifndef MP-ALIPAY -->
        <canvas class="tki-barcode-canvas" :canvas-id="cid" :style="{width:canvasWidth+'px',height:canvasHeight+'px'}" />
        <!-- #endif -->
        <!-- #ifdef MP-ALIPAY -->
        <canvas v-if="val === '' || defaultOpations.text === val" :id="cid" :width="canvasWidth" :height="canvasHeight" class="tki-barcode-canvas" />
        <!-- #endif -->
        <image v-show="show" :src="result" :style="{width:canvasWidth+'px',height:canvasHeight+'px'}" />
    </view>
</template>
 
<script>
// const barcode = require('./barcode.js');
import barCode from "./barcode.js"
const opations = {
    // format: "CODE128",//选择要使用的条形码类型 微信支持的条码类型有 code128\code39\ena13\ean8\upc\itf14\
    width: 4,//设置条之间的宽度
    height: 120,//高度
    displayValue: true,//是否在条形码下方显示文字
    // text: "1234567890",//覆盖显示的文本
    textAlign: "center",//设置文本的水平对齐方式
    textPosition: "bottom",//设置文本的垂直位置
    textMargin: 0,//设置条形码和文本之间的间距
    fontSize: 24,//设置文本的大小
    fontColor: "#000000",//设置文本的颜色
    lineColor: "#000000",//设置条形码的颜色
    background: "#FFFFFF",//设置条形码的背景色
    margin: 0,//设置条形码周围的空白边距
    marginTop: undefined,//设置条形码周围的上边距
    marginBottom: undefined,//设置条形码周围的下边距
    marginLeft: undefined,//设置条形码周围的左边距
    marginRight: undefined,//设置条形码周围的右边距
}
export default {
    name: "tkiBarcode",
    props: {
        show: {
            type: Boolean,
            default: true
        },
        cid: {
            type: String,
            default: 'tki-barcode-canvas'
        },
        unit: {
            type: String,
            default: 'upx'
        },
        val: {
            type: String,
            default: ''
        },
        format: {
            type: String,
            default: 'CODE128'
        },
        opations: {
            type: Object,
            default: function () {
                return {}
            }
        },
        onval: {
            type: Boolean,
            default: false
        },
        loadMake: {
            type: Boolean,
            default: true
        },
    },
    data() {
        return {
            result: '',
            canvasWidth: 0,
            canvasHeight: 0,
            defaultOpations: Object.assign({}, opations)
        }
    },
    onUnload: function () {
    },
    methods: {
        _makeCode() {
            let that = this
            // 合并参数
            Object.assign(this.defaultOpations, this.opations)
            if (that.unit == "upx") {
                /* if (that.defaultOpations.width) {
                    that.defaultOpations.width = uni.upx2px(that.defaultOpations.width)
                } */
                if (that.defaultOpations.height) {
                    that.defaultOpations.height = uni.upx2px(that.defaultOpations.height)
                }
                if (that.defaultOpations.fontSize) {
                    that.defaultOpations.fontSize = uni.upx2px(that.defaultOpations.fontSize)
                }
            }
            if (that._empty(that.defaultOpations.text)) {
                that.defaultOpations.text = that.val
            }
            if (that._empty(that.defaultOpations.format)) {
                that.defaultOpations.format = that.format
            }
            // console.log(JSON.stringify(that.defaultOpations))
            this.$nextTick(() => {
                setTimeout(() => {
                    new barCode(that, that.cid, that.defaultOpations,
                        function (res) { // 生成条形码款高回调
                            that.canvasWidth = res.width
                            that.canvasHeight = res.height
                        },
                        function (res) { // 生成条形码的回调
                            // 返回值
                            that._result(res)
                            // 重置默认参数
                            that.defaultOpations = opations
                        },
                    );
                }, 100)
            })
        },
        _clearCode() {
            this._result('')
        },
        _saveCode() {
            let that = this;
            if (this.result != "") {
                uni.saveImageToPhotosAlbum({
                    filePath: that.result,
                    success: function () {
                        uni.showToast({
                            title: '条形码保存成功',
                            icon: 'success',
                            duration: 2000
                        });
                    }
                });
            }
        },
        _result(res) {
            this.result = res;
            this.$emit('result', res)
        },
        _empty(v) {
            let tp = typeof v,
                rt = false;
            if (tp == "number" && String(v) == "") {
                rt = true
            } else if (tp == "undefined") {
                rt = true
            } else if (tp == "object") {
                if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
            } else if (tp == "string") {
                if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
            } else if (tp == "function") {
                rt = false
            }
            return rt
        }
    },
    watch: {
        val(n, o) {
            this.defaultOpations.text = n
            if (this.onval) {
                if (n != o && !this._empty(n)) {
                    this._makeCode()
                }
            }
        },
        opations: {
            handler(n,o){
                if (this.onval) {
                    if (!this._empty(n)) {
                        this._makeCode()
                    }
                }
            },
            deep: true
        }
    },
    mounted () {
        if (this.loadMake) {
            if (!this._empty(this.val)) {
                this._makeCode()
            }
        }
    },
}
</script>
<style>
.tki-barcode {
    position: relative;
}
.tki-barcode-canvas {
    position: fixed!important;
    top: -99999upx;
    left: -99999upx;
    z-index: -99999;
}
</style>