index.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <tki-barcode class="jnpf-barcode" v-if="barcode&&showBarCode" ref="barcode" :format="format" :cid="cid"
  3. :val="barcode" :opations="opations" loadMake :key="key" />
  4. </template>
  5. <script>
  6. import tkiBarcode from "./tki-barcode/tki-barcode.vue"
  7. let unique = 0
  8. export default {
  9. name: 'jnpf-barcode',
  10. props: {
  11. dataType: {
  12. type: String,
  13. default: 'static'
  14. },
  15. format: {
  16. type: String,
  17. default: 'code128'
  18. },
  19. lineColor: {
  20. type: String,
  21. default: '#000'
  22. },
  23. background: {
  24. type: String,
  25. default: '#fff'
  26. },
  27. relationField: {
  28. type: String,
  29. default: ''
  30. },
  31. formData: {
  32. type: Object
  33. },
  34. width: {
  35. type: Number,
  36. default: 4
  37. },
  38. height: {
  39. type: Number,
  40. default: 40
  41. },
  42. staticText: {
  43. type: String,
  44. default: ''
  45. }
  46. },
  47. components: {
  48. tkiBarcode
  49. },
  50. data() {
  51. return {
  52. cid: '',
  53. relationText: "",
  54. showBarCode: false,
  55. key: +new Date()
  56. }
  57. },
  58. computed: {
  59. barcode() {
  60. return this.dataType === 'static' ? this.staticText : this.relationText?.toString()
  61. },
  62. opations() {
  63. return {
  64. format: this.format,
  65. width: this.width,
  66. height: this.height,
  67. displayValue: false,
  68. lineColor: this.lineColor,
  69. background: this.background,
  70. }
  71. }
  72. },
  73. created() {
  74. this.cid = this.uuid()
  75. this.showBarCode = true
  76. uni.$on('updateCode', () => {
  77. this.showBarCode = false
  78. this.$nextTick(() => {
  79. this.showBarCode = true
  80. })
  81. })
  82. },
  83. watch: {
  84. formData: {
  85. handler: function(val) {
  86. if (val && this.dataType === 'relation' && this.relationField) {
  87. if (this.relationText != val[this.relationField]) {
  88. this.relationText = val[this.relationField]
  89. setTimeout(() => {
  90. this.key = +new Date()
  91. }, 50)
  92. }
  93. }
  94. },
  95. deep: true,
  96. immediate: true
  97. },
  98. },
  99. methods: {
  100. uuid() {
  101. const time = Date.now()
  102. const random = Math.floor(Math.random() * 1000000000)
  103. unique++
  104. return 'barcode_' + random + unique + String(time)
  105. }
  106. },
  107. }
  108. </script>
  109. <style lang="scss" scoped>
  110. .jnpf-barcode {
  111. width: 100%;
  112. overflow: hidden;
  113. margin-bottom: -20rpx;
  114. }
  115. </style>