tki-barcode.vue 4.9 KB

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