123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <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 :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: '1234567890128'
- },
- 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))
- 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
- },
- );
- },
- _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)
- },
- _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) {
- if (this.onval) {
- if (n != o && !this._empty(n)) {
- setTimeout(() => {
- this._makeCode()
- }, 0);
- }
- }
- },
- opations: {
- handler(n,o){
- if (this.onval) {
- if (!this._empty(n)) {
- setTimeout(() => {
- this._makeCode()
- }, 0);
- }
- }
- },
- deep: true
- }
- },
- mounted: function () {
- if (this.loadMake) {
- if (!this._empty(this.val)) {
- setTimeout(() => {
- this._makeCode()
- }, 0);
- }
- }
- },
- }
- </script>
- <style>
- .tki-barcode {
- position: relative;
- }
- .tki-barcode-canvas {
- position: fixed;
- top: -99999upx;
- left: -99999upx;
- z-index: -99999;
- }
- .content-main-barcode uni-image{
- width: 600rpx !important;
- }
- </style>
|