tki-qrcode.vue 4.1 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. // 生成二维码的回调
  104. that._result(res);
  105. }
  106. });
  107. } else {
  108. uni.showToast({
  109. title: '二维码内容不能为空',
  110. icon: 'none',
  111. duration: 2000
  112. });
  113. }
  114. },
  115. _clearCode() {
  116. this._result('');
  117. qrcode.clear();
  118. },
  119. _saveCode() {
  120. let that = this;
  121. if (this.result != '') {
  122. uni.saveImageToPhotosAlbum({
  123. filePath: that.result,
  124. success: function() {
  125. uni.showToast({
  126. title: '二维码保存成功',
  127. icon: 'none',
  128. duration: 2000
  129. });
  130. }
  131. });
  132. }
  133. },
  134. _result(res) {
  135. this.result = res;
  136. this.$emit('result', res);
  137. },
  138. _empty(v) {
  139. let tp = typeof v,
  140. rt = false;
  141. if (tp == 'number' && String(v) == '') {
  142. rt = true;
  143. } else if (tp == 'undefined') {
  144. rt = true;
  145. } else if (tp == 'object') {
  146. if (JSON.stringify(v) == '{}' || JSON.stringify(v) == '[]' || v == null) rt = true;
  147. } else if (tp == 'string') {
  148. if (v == '' || v == 'undefined' || v == 'null' || v == '{}' || v == '[]') rt = true;
  149. } else if (tp == 'function') {
  150. rt = false;
  151. }
  152. return rt;
  153. }
  154. },
  155. watch: {
  156. size: function(n, o) {
  157. // console.log(11)
  158. if (n != o && !this._empty(n)) {
  159. this.cSize = n;
  160. if (!this._empty(this.val)) {
  161. setTimeout(() => {
  162. this._makeCode();
  163. }, 100);
  164. }
  165. }
  166. },
  167. val: function(n, o) {
  168. if (this.onval) {
  169. if (n != o && !this._empty(n)) {
  170. setTimeout(() => {
  171. this._makeCode();
  172. }, 0);
  173. }
  174. }
  175. }
  176. },
  177. computed: {
  178. cpSize() {
  179. if (this.unit == 'upx') {
  180. return uni.upx2px(this.size);
  181. } else {
  182. return this.size;
  183. }
  184. }
  185. },
  186. mounted: function() {
  187. if (this.loadMake) {
  188. if (!this._empty(this.val)) {
  189. setTimeout(() => {
  190. this._makeCode();
  191. }, 0);
  192. }
  193. }
  194. }
  195. };
  196. </script>
  197. <style>
  198. .tki-qrcode {
  199. height: 100%;
  200. }
  201. .tki-qrcode-canvas {
  202. position: relative;
  203. top: 0rpx;
  204. left: -99999rpx;
  205. /* z-index: -99999; */
  206. }
  207. /* canvas{
  208. display: none;
  209. } */
  210. .img {
  211. margin-top: -440rpx !important;
  212. height: 100%;
  213. }
  214. </style>