tki-barcode.vue 5.4 KB

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