use-qrcode.vue 3.9 KB

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