uqrcode.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <view class="uqrcode" v-if="options.canvasId">
  3. <canvas :id="options.canvasId" :canvas-id="options.canvasId"
  4. :style="{'width': `${options.size}px`, 'height': `${options.size}px`}" />
  5. </view>
  6. </template>
  7. <script>
  8. import uqrcode from './common/uqrcode'
  9. export default {
  10. name: 'uqrcode',
  11. data() {
  12. return {
  13. options: {
  14. canvasId: '',
  15. size: 354,
  16. margin: 5,
  17. text: ''
  18. },
  19. result: {}
  20. }
  21. },
  22. created() {
  23. this.options.canvasId = `qrcode_${this.uuid()}`
  24. },
  25. methods: {
  26. make(options) {
  27. return new Promise((resolve, reject) => {
  28. uqrcode.make(Object.assign(this.options, options), this).then(res => {
  29. this.result = res
  30. resolve({
  31. ...res
  32. })
  33. }).catch(err => {
  34. reject(err)
  35. })
  36. })
  37. },
  38. save() {
  39. // #ifdef H5
  40. uni.showToast({
  41. icon: 'none',
  42. title: 'H5长按image保存'
  43. })
  44. // #endif
  45. // #ifndef H5
  46. console.log(this.result)
  47. uni.saveImageToPhotosAlbum({
  48. filePath: this.result.tempFilePath,
  49. success: (res) => {
  50. uni.showToast({
  51. icon: 'success',
  52. title: '保存成功'
  53. })
  54. },
  55. fail: (err) => {
  56. uni.showToast({
  57. icon: 'none',
  58. title: JSON.stringify(err)
  59. })
  60. }
  61. })
  62. // #endif
  63. },
  64. uuid(len = 32, firstU = true, radix = null) {
  65. let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  66. let uuid = [];
  67. radix = radix || chars.length;
  68. if (len) {
  69. // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
  70. for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  71. } else {
  72. let r;
  73. // rfc4122标准要求返回的uuid中,某些位为固定的字符
  74. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  75. uuid[14] = '4';
  76. for (let i = 0; i < 36; i++) {
  77. if (!uuid[i]) {
  78. r = 0 | Math.random() * 16;
  79. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  80. }
  81. }
  82. }
  83. // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
  84. if (firstU) {
  85. uuid.shift();
  86. return 'u' + uuid.join('');
  87. } else {
  88. return uuid.join('');
  89. }
  90. }
  91. }
  92. }
  93. </script>
  94. <style>
  95. </style>