tki-qrcode.vue 3.8 KB

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