barcode.vue 11.2 KB
Newer Older
G
guop 已提交
1
<template>
B
baiy 已提交
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
    <div>
        <div id="barcode-setting">
            <Row :gutter="10">
                <Col span="12">
                    <Form :label-width="80">
                        <FormItem :label="$t('barcode_content')">
                            <Input v-model="current.text">
                                <Select v-model="current.format" slot="append" style="width: 100px">
                                    <Option v-for="type in barcodeFormat" :key="type" :value="type">{{ type }}</Option>
                                </Select>
                            </Input>
                        </FormItem>
                        <Row>
                            <Col span="12">
                                <FormItem :label="$t('barcode_background')">
                                    <ColorPicker recommend v-model="current.background"/>
                                </FormItem>
                            </Col>
                            <Col span="12">
                                <FormItem :label="$t('barcode_line_color')">
                                    <ColorPicker recommend v-model="current.lineColor"/>
                                </FormItem>
                            </Col>
                        </Row>
                        <FormItem :label="$t('barcode_bar_width')">
                            <Row>
                                <Col span="21">
                                    <Slider v-model="current.width" :min="1" :max="4"></Slider>
                                </Col>
                                <Col span="3">
                                    <span style="float: right">{{ this.current.width }}</span>
                                </Col>
                            </Row>
                        </FormItem>
                        <FormItem :label="$t('barcode_height')">
                            <Row>
                                <Col span="21">
                                    <Slider v-model="current.height" :min="10" :max="150"></Slider>
                                </Col>
                                <Col span="3">
                                    <span style="float: right">{{ this.current.height }}</span>
                                </Col>
                            </Row>
                        </FormItem>
                        <FormItem :label="$t('barcode_margin')">
                            <Row>
                                <Col span="21">
                                    <Slider v-model="current.margin" :min="0" :max="25"></Slider>
                                </Col>
                                <Col span="3">
                                    <span style="float: right">{{ this.current.margin }}</span>
                                </Col>
                            </Row>
                        </FormItem>
                    </Form>
G
guop 已提交
57
                </Col>
B
baiy 已提交
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
                <Col span="12">
                    <Form :label-width="80">
                        <FormItem :label="$t('barcode_show_text')">
                            <RadioGroup v-model="current.textPosition" type="button">
                                <Radio label="close">
                                    <span>{{ $t('barcode_hide') }}</span>
                                </Radio>
                                <Radio label="top">
                                    <span>{{ $t('barcode_top') }}</span>
                                </Radio>
                                <Radio label="bottom">
                                    <span>{{ $t('barcode_bottom') }}</span>
                                </Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem :label="$t('barcode_text_align')">
                            <RadioGroup v-model="current.textAlign" type="button">
                                <Radio :disabled="!showText" label="left">
                                    <span>{{ $t('barcode_left') }}</span>
                                </Radio>
                                <Radio :disabled="!showText" label="center">
                                    <span>{{ $t('barcode_center') }}</span>
                                </Radio>
                                <Radio :disabled="!showText" label="right">
                                    <span>{{ $t('barcode_right') }}</span>
                                </Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem :label="$t('barcode_font')">
                            <Row :gutter="10">
                                <Col span="12">
                                    <Select :disabled="!showText" v-model="current.font">
                                        <Option v-for="font in fontFamily" :key="font" :value="font">{{ font }}</Option>
                                    </Select>
                                </Col>
                                <Col span="12">
                                    <CheckboxGroup v-model="current.fontOptions">
                                        <Checkbox :disabled="!showText" label="bold">
                                            <span>{{ $t('barcode_bold') }}</span>
                                        </Checkbox>
                                        <Checkbox :disabled="!showText" label="italic">
                                            <span>{{ $t('barcode_italic') }}</span>
                                        </Checkbox>
                                    </CheckboxGroup>
                                </Col>
                            </Row>
                        </FormItem>
                        <FormItem :label="$t('barcode_font_size')">
                            <Row>
                                <Col span="22">
                                    <Slider :disabled="!showText" v-model="current.fontSize" :min="8"
                                            :max="36"></Slider>
                                </Col>
                                <Col span="2">
                                    <span style="float: right">{{ this.current.fontSize }}</span>
                                </Col>
                            </Row>
                        </FormItem>
                        <FormItem :label="$t('barcode_text_margin')">
                            <Row>
                                <Col span="22">
                                    <Slider :disabled="!showText" v-model="current.textMargin" :min="-15"
                                            :max="40"></Slider>
                                </Col>
                                <Col span="2">
                                    <span style="float: right">{{ this.current.textMargin }}</span>
                                </Col>
                            </Row>
                        </FormItem>
                    </Form>
G
guop 已提交
128
                </Col>
B
baiy 已提交
129 130 131 132 133 134 135
            </Row>
        </div>
        <heightResize @resize="resize" ignore :append="['#barcode-setting']">
            <div :style="`height: ${outputHeight}px;line-height:${outputHeight}px;text-align: center;vertical-align: middle;`">
                <canvas @click="saveImage" :style="`border: ${canvasBorder};vertical-align: middle;`" ref="barcode"
                        class="barcode" v-show="!validStr" style="cursor:pointer"></canvas>
                <p style="color: red" v-show="validStr">{{ validStr }}</p>
G
guop 已提交
136
            </div>
B
baiy 已提交
137 138
        </heightResize>
    </div>
G
guop 已提交
139 140 141 142 143 144 145 146 147
</template>
<script>
/**
 * @author alvinkwok
 * @date 2021/10/30
 * 基于jsbarcode生成条形码,可以自定义条码各项属性
 * 不支持中文
 */
import JsBarcode from 'jsbarcode'
B
baiy 已提交
148
import heightResize from "./components/heightResize";
G
guop 已提交
149
export default {
B
baiy 已提交
150 151
    components: {
        heightResize
G
guop 已提交
152
    },
B
baiy 已提交
153 154
    created() {
        this.$initToolData('text')
G
guop 已提交
155
    },
B
baiy 已提交
156 157 158 159 160 161 162 163 164 165
    computed: {
        showText() {
            return ['top', 'bottom'].includes(this.current.textPosition)
        },
        canvasBorder() {
            if (this.current.background.toUpperCase() === "#FFFFFF") {
                return "1px dashed #666"
            }
            return "1px dashed #fff";
        }
G
guop 已提交
166
    },
B
baiy 已提交
167 168
    mounted() {
        this.generate()
B
baiy 已提交
169
    },
B
baiy 已提交
170 171 172 173 174 175
    watch: {
        current: {
            handler() {
                this.generate()
            },
            deep: true
B
baiy 已提交
176 177
        }
    },
B
baiy 已提交
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
    methods: {
        generate() {
            // 处理字体样式
            let fontOptions = this.current.fontOptions.join(" ")
            const barcodeContent = this.current.text ? this.current.text : "Example 1234"
            JsBarcode(this.$refs.barcode, barcodeContent, {
                format: this.current.format,
                width: this.current.width,
                height: this.current.height,
                margin: this.current.margin,
                background: this.current.background,
                lineColor: this.current.lineColor,
                displayValue: this.showText,
                textPosition: this.current.textPosition,
                textAlign: this.current.textAlign,
                font: this.current.font,
                fontOptions: fontOptions,
                fontSize: this.current.fontSize,
                textMargin: this.current.textMargin,
                valid: (valid) => {
                    this.validStr = !valid ? `"${barcodeContent}" ${this.$t('barcode_invalid_content').toString()}` : "";
                    if (!this.validStr && this.current.text) {
                        this.$saveToolData(this.current)
                    }
                }
            })
G
guop 已提交
204
        },
B
baiy 已提交
205 206 207 208
        saveImage() {
            if (!this.validStr && this.current.text) {
                this.$clipboardCopyImages(this.$refs.barcode.toDataURL("image/png"), true)
            }
G
guop 已提交
209
        },
B
baiy 已提交
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
        resize(height){
            this.outputHeight = Math.max(250,height)
        }
    },
    data() {
        return {
            outputHeight:250,
            current: {
                text: "",
                format: "CODE128",
                width: 2,
                height: 50,
                margin: 10,
                background: "#FFFFFF",
                lineColor: "#000000",
                textAlign: "center",
                textPosition: "bottom",
                font: "monospace",
                fontOptions: [],
                fontSize: 20,
                textMargin: 0
            },
            validStr: '',
            // 条码格式
            barcodeFormat: [
                "CODE128",
                "CODE128A",
                "CODE128B",
                "CODE128C",
                "EAN13",
                "EAN8",
                "UPC",
                "CODE39",
                "ITF14",
                "ITF",
                "MSI",
                "MSI10",
                "MSI11",
                "MSI1010",
                "MSI1110",
                "pharmacode",
            ],
            fontFamily: [
                "monospace",
                "Sans-serif",
                "Serif",
                "Fantasy",
                "Cursive"
            ],
            barcodeBase64: "",
        }
    },
G
guop 已提交
262 263 264 265 266
}
</script>

<style scoped>
/**iview原来的formitem太高了,在浏览器直接使用插件时会被撑开,因此需要压缩下高度**/
B
baiy 已提交
267 268
#barcode-setting .ivu-form-item {
    margin-bottom: 2px;
G
guop 已提交
269
}
B
baiy 已提交
270
</style>