zb-code.vue 4.6 KB

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