tki-barcode.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template xlang="wxml" minapp="mpvue">
  2. <view class="tki-barcode">
  3. <!-- #ifndef MP-ALIPAY -->
  4. <canvas class="tki-barcode-canvas" :canvas-id="cid"
  5. :style="{width:canvasWidth+'px',height:canvasHeight+'px'}" />
  6. <!-- #endif -->
  7. <!-- #ifdef MP-ALIPAY -->
  8. <canvas :id="cid" :width="canvasWidth" :height="canvasHeight" class="tki-barcode-canvas" />
  9. <!-- #endif -->
  10. <image v-show="show" :src="result" :style="{width:canvasWidth+'px',height:canvasHeight+'px'}" />
  11. </view>
  12. </template>
  13. <script>
  14. // const barcode = require('./barcode.js');
  15. import barCode from "./barcode.js"
  16. const opations = {
  17. // format: "CODE128",//选择要使用的条形码类型 微信支持的条码类型有 code128\code39\ena13\ean8\upc\itf14\
  18. width: 4, //设置条之间的宽度
  19. height: 120, //高度
  20. displayValue: true, //是否在条形码下方显示文字
  21. // text: "1234567890",//覆盖显示的文本
  22. textAlign: "center", //设置文本的水平对齐方式
  23. textPosition: "bottom", //设置文本的垂直位置
  24. textMargin: 0, //设置条形码和文本之间的间距
  25. fontSize: 24, //设置文本的大小
  26. fontColor: "#000000", //设置文本的颜色
  27. lineColor: "#000000", //设置条形码的颜色
  28. background: "#FFFFFF", //设置条形码的背景色
  29. margin: 0, //设置条形码周围的空白边距
  30. marginTop: undefined, //设置条形码周围的上边距
  31. marginBottom: undefined, //设置条形码周围的下边距
  32. marginLeft: undefined, //设置条形码周围的左边距
  33. marginRight: undefined, //设置条形码周围的右边距
  34. }
  35. export default {
  36. name: "tkiBarcode",
  37. props: {
  38. show: {
  39. type: Boolean,
  40. default: true
  41. },
  42. cid: {
  43. type: String,
  44. default: 'tki-barcode-canvas'
  45. },
  46. unit: {
  47. type: String,
  48. default: 'upx'
  49. },
  50. val: {
  51. type: String,
  52. default: ''
  53. },
  54. format: {
  55. type: String,
  56. default: 'CODE128'
  57. },
  58. opations: {
  59. type: Object,
  60. default: function() {
  61. return {}
  62. }
  63. },
  64. onval: {
  65. type: Boolean,
  66. default: false
  67. },
  68. loadMake: {
  69. type: Boolean,
  70. default: true
  71. },
  72. },
  73. data() {
  74. return {
  75. result: '',
  76. canvasWidth: 0,
  77. canvasHeight: 0,
  78. defaultOpations: Object.assign({}, opations)
  79. }
  80. },
  81. onUnload: function() {},
  82. methods: {
  83. _makeCode() {
  84. let that = this
  85. // 合并参数
  86. Object.assign(this.defaultOpations, this.opations)
  87. if (that.unit == "upx") {
  88. if (that.defaultOpations.width) {
  89. that.defaultOpations.width = uni.upx2px(that.defaultOpations.width)
  90. }
  91. if (that.defaultOpations.height) {
  92. that.defaultOpations.height = uni.upx2px(that.defaultOpations.height)
  93. }
  94. if (that.defaultOpations.fontSize) {
  95. that.defaultOpations.fontSize = uni.upx2px(that.defaultOpations.fontSize)
  96. }
  97. }
  98. if (that._empty(that.defaultOpations.text)) {
  99. that.defaultOpations.text = that.val
  100. }
  101. if (that._empty(that.defaultOpations.format)) {
  102. that.defaultOpations.format = that.format
  103. }
  104. new barCode(that, that.cid, that.defaultOpations,
  105. function(res) { // 生成条形码款高回调
  106. that.canvasWidth = res.width
  107. that.canvasHeight = res.height
  108. },
  109. function(res) { // 生成条形码的回调
  110. // 返回值
  111. that._result(res)
  112. // 重置默认参数
  113. that.defaultOpations = opations
  114. },
  115. );
  116. },
  117. _clearCode() {
  118. this._result('')
  119. },
  120. _saveCode() {
  121. let that = this;
  122. if (this.result != "") {
  123. uni.saveImageToPhotosAlbum({
  124. filePath: that.result,
  125. success: function() {
  126. uni.showToast({
  127. title: '条形码保存成功',
  128. icon: 'success',
  129. duration: 2000
  130. });
  131. }
  132. });
  133. }
  134. },
  135. _result(res) {
  136. this.result = res;
  137. this.$emit('result', res)
  138. },
  139. _empty(v) {
  140. let tp = typeof v,
  141. rt = false;
  142. if (tp == "number" && String(v) == "") {
  143. rt = true
  144. } else if (tp == "undefined") {
  145. rt = true
  146. } else if (tp == "object") {
  147. if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
  148. } else if (tp == "string") {
  149. if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
  150. } else if (tp == "function") {
  151. rt = false
  152. }
  153. return rt
  154. }
  155. },
  156. watch: {
  157. val(n, o) {
  158. if (this.onval) {
  159. if (n != o && !this._empty(n)) {
  160. setTimeout(() => {
  161. this._makeCode()
  162. }, 0);
  163. }
  164. }
  165. },
  166. opations: {
  167. handler(n, o) {
  168. if (this.onval) {
  169. if (!this._empty(n)) {
  170. setTimeout(() => {
  171. this._makeCode()
  172. }, 0);
  173. }
  174. }
  175. },
  176. deep: true
  177. }
  178. },
  179. mounted: function() {
  180. if (this.loadMake) {
  181. if (!this._empty(this.val)) {
  182. setTimeout(() => {
  183. this._makeCode()
  184. }, 0);
  185. }
  186. }
  187. },
  188. }
  189. </script>
  190. <style>
  191. .tki-barcode {
  192. text-align: right;
  193. position: relative;
  194. }
  195. .tki-barcode-canvas {
  196. position: fixed !important;
  197. top: -99999upx;
  198. left: -99999upx;
  199. z-index: -99999;
  200. }
  201. </style>