tki-qrcode.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. showLoading: that.showLoading, // 是否显示loading
  92. loadingText: that.loadingText, // loading文字
  93. text: that.val, // 生成内容
  94. size: that.cpSize, // 二维码大小
  95. background: that.background, // 背景色
  96. foreground: that.foreground, // 前景色
  97. pdground: that.pdground, // 定位角点颜色
  98. correctLevel: that.lv, // 容错级别
  99. image: that.icon, // 二维码图标
  100. imageSize: that.iconSize,// 二维码图标大小
  101. cbResult: function (res) { // 生成二维码的回调
  102. that._result(res)
  103. },
  104. });
  105. } else {
  106. uni.showToast({
  107. title: '二维码内容不能为空',
  108. icon: 'none',
  109. duration: 2000
  110. });
  111. }
  112. },
  113. _clearCode() {
  114. this._result('')
  115. qrcode.clear()
  116. },
  117. _saveCode() {
  118. let that = this;
  119. if (this.result != "") {
  120. uni.saveImageToPhotosAlbum({
  121. filePath: that.result,
  122. success: function () {
  123. uni.showToast({
  124. title: '二维码保存成功',
  125. icon: 'success',
  126. duration: 2000
  127. });
  128. }
  129. });
  130. }
  131. },
  132. _result(res) {
  133. this.result = res;
  134. this.$emit('result', res);
  135. },
  136. _empty(v) {
  137. let tp = typeof v,
  138. rt = false;
  139. if (tp == "number" && String(v) == "") {
  140. rt = true
  141. } else if (tp == "undefined") {
  142. rt = true
  143. } else if (tp == "object") {
  144. if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
  145. } else if (tp == "string") {
  146. if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
  147. } else if (tp == "function") {
  148. rt = false
  149. }
  150. return rt
  151. }
  152. },
  153. watch: {
  154. size: function (n, o) {
  155. if (n != o && !this._empty(n)) {
  156. this.cSize = n
  157. if (!this._empty(this.val)) {
  158. setTimeout(() => {
  159. this._makeCode()
  160. }, 100);
  161. }
  162. }
  163. },
  164. val: function (n, o) {
  165. if (this.onval) {
  166. if (n != o && !this._empty(n)) {
  167. setTimeout(() => {
  168. this._makeCode()
  169. }, 0);
  170. }
  171. }
  172. }
  173. },
  174. computed: {
  175. cpSize() {
  176. if(this.unit == "upx"){
  177. return uni.upx2px(this.size)
  178. }else{
  179. return this.size
  180. }
  181. }
  182. },
  183. mounted: function () {
  184. if (this.loadMake) {
  185. if (!this._empty(this.val)) {
  186. setTimeout(() => {
  187. this._makeCode()
  188. }, 0);
  189. }
  190. }
  191. },
  192. }
  193. </script>
  194. <style>
  195. .tki-qrcode {
  196. position: relative;
  197. }
  198. .tki-qrcode-canvas {
  199. position: fixed;
  200. top: -99999upx;
  201. left: -99999upx;
  202. z-index: -99999;
  203. }
  204. </style>