Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

tki-qrcode.vue 4.2 KiB

před 4 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view class="tki-qrcode">
  3. <canvas
  4. class="tki-qrcode-canvas"
  5. :canvas-id="cid"
  6. :id="cid"
  7. :style="{ width: cpSize + 'px', height: cpSize + 'px' }"
  8. />
  9. <image v-show="show" :src="result" :style="{ width: cpSize + 'px', height: cpSize + 'px' }" />
  10. </view>
  11. </template>
  12. <script>
  13. import QRCode from './qrcode.js'
  14. let qrcode
  15. export default {
  16. name: 'tki-qrcode',
  17. props: {
  18. cid: { type: String, default: 'tki-qrcode-canvas' },
  19. size: { type: Number, default: 200 },
  20. unit: { type: String, default: 'upx' },
  21. show: { type: Boolean, default: true },
  22. val: { type: String, default: '' },
  23. background: { type: String, default: '#ffffff' },
  24. foreground: { type: String, default: '#000000' },
  25. pdground: { type: String, default: '#000000' },
  26. icon: { type: String, default: '' },
  27. iconSize: { type: Number, default: 40 },
  28. lv: { type: Number, default: 3 },
  29. onval: { type: Boolean, default: false },
  30. loadMake: { type: Boolean, default: false },
  31. usingComponents: { type: Boolean, default: true },
  32. showLoading: { type: Boolean, default: true },
  33. loadingText: { type: String, default: '二维码生成中' }
  34. },
  35. data() {
  36. return { result: '' }
  37. },
  38. mounted: function() {
  39. if (this.loadMake) {
  40. if (!this._empty(this.val)) {
  41. setTimeout(() => {
  42. this._makeCode()
  43. }, 300)
  44. }
  45. }
  46. },
  47. methods: {
  48. _makeCode() {
  49. let that = this
  50. if (!this._empty(this.val)) {
  51. qrcode = new QRCode({
  52. context: that, // 上下文环境
  53. canvasId: that.cid, // canvas-id
  54. usingComponents: that.usingComponents, // 是否是自定义组件
  55. showLoading: that.showLoading, // 是否显示loading
  56. loadingText: that.loadingText, // loading文字
  57. text: that.val, // 生成内容
  58. size: that.cpSize, // 二维码大小
  59. background: that.background, // 背景色
  60. foreground: that.foreground, // 前景色
  61. pdground: that.pdground, // 定位角点颜色
  62. correctLevel: that.lv, // 容错级别
  63. image: that.icon, // 二维码图标
  64. imageSize: that.iconSize, // 二维码图标大小
  65. cbResult: function(res) {
  66. // 生成二维码的回调
  67. that._result(res)
  68. }
  69. })
  70. } else {
  71. uni.showToast({
  72. title: '二维码内容不能为空',
  73. icon: 'none',
  74. duration: 2000
  75. })
  76. }
  77. },
  78. _clearCode() {
  79. this._result('')
  80. qrcode.clear()
  81. },
  82. _saveCode() {
  83. let that = this
  84. if (this.result != '') {
  85. uni.saveImageToPhotosAlbum({
  86. filePath: that.result,
  87. success: function() {
  88. uni.showToast({
  89. title: '二维码保存成功',
  90. icon: 'success',
  91. duration: 2000
  92. })
  93. }
  94. })
  95. }
  96. },
  97. _result(res) {
  98. this.result = res
  99. this.$emit('result', res)
  100. },
  101. _empty(v) {
  102. let tp = typeof v,
  103. rt = false
  104. if (tp == 'number' && String(v) == '') {
  105. rt = true
  106. } else if (tp == 'undefined') {
  107. rt = true
  108. } else if (tp == 'object') {
  109. if (JSON.stringify(v) == '{}' || JSON.stringify(v) == '[]' || v == null) rt = true
  110. } else if (tp == 'string') {
  111. if (v == '' || v == 'undefined' || v == 'null' || v == '{}' || v == '[]') rt = true
  112. } else if (tp == 'function') {
  113. rt = false
  114. }
  115. return rt
  116. }
  117. },
  118. watch: {
  119. size: function(n, o) {
  120. if (n != o && !this._empty(n)) {
  121. this.cSize = n
  122. if (!this._empty(this.val)) {
  123. setTimeout(() => {
  124. this._makeCode()
  125. }, 100)
  126. }
  127. }
  128. },
  129. val: function(n, o) {
  130. if (this.onval) {
  131. if (n != o && !this._empty(n)) {
  132. setTimeout(() => {
  133. this._makeCode()
  134. }, 0)
  135. }
  136. }
  137. }
  138. },
  139. computed: {
  140. cpSize() {
  141. if (this.unit == 'upx') {
  142. return uni.upx2px(this.size)
  143. } else {
  144. return this.size
  145. }
  146. }
  147. }
  148. }
  149. </script>
  150. <style>
  151. .tki-qrcode {
  152. position: relative;
  153. }
  154. .tki-qrcode-canvas {
  155. position: fixed;
  156. top: -99999upx;
  157. left: -99999upx;
  158. z-index: -99999;
  159. }
  160. </style>