tki-qrcode.vue 4.0 KB

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