tki-qrcode.vue 4.2 KB

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