You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
3.8 KiB

4 months ago
  1. <template>
  2. <view class="u-qrcode">
  3. <canvas class="u-qrcode__canvas" :id="cid" :canvas-id="cid" :style="{ width: size + unit, height: size + unit }" />
  4. <image v-show="show" :src="result" :style="{ width: size + unit, height: size + unit }" />
  5. </view>
  6. </template>
  7. <script>
  8. import QRCode from "./qrcode.js"
  9. let qrcode
  10. export default {
  11. name: "u-qrcode",
  12. props: {
  13. cid: {
  14. type: String,
  15. default: 'u-qrcode-canvas' + Math.random().toString()
  16. },
  17. size: {
  18. type: Number,
  19. default: 200
  20. },
  21. unit: {
  22. type: String,
  23. default: 'px'
  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: true
  60. },
  61. loadMake: {
  62. type: Boolean,
  63. default: true
  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.size, // 二维码大小
  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. },
  176. mounted: function () {
  177. if (this.loadMake) {
  178. if (!this._empty(this.val)) {
  179. setTimeout(() => {
  180. this._makeCode()
  181. }, 0);
  182. }
  183. }
  184. },
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .u-qrcode {
  189. position: relative;
  190. &__canvas {
  191. position: fixed;
  192. top: -99999rpx;
  193. left: -99999rpx;
  194. z-index: -99999;
  195. }
  196. }
  197. </style>