tki-barcode.vue 5.9 KB

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