uqrcode.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view class="uqrcode">
  3. <view v-if="options.mode === 'view'" class="uqrcode-view" :style="{
  4. 'width': `${options.size}px`,
  5. 'height': `${options.size}px`,
  6. 'padding': `${options.margin}px`,
  7. 'background-color': options.backgroundColor
  8. }">
  9. <view class="uqrcode-view-row" v-for="(row, rowIndex) in modules.length" :key="rowIndex">
  10. <view class="uqrcode-view-col" v-for="(col, colIndex) in modules.length" :key="colIndex" :style="{
  11. 'width': `${colSize}px`,
  12. 'height': `${colSize}px`,
  13. 'background-color': modules[rowIndex][colIndex] ? options.foregroundColor : options.backgroundColor
  14. }">
  15. </view>
  16. </view>
  17. </view>
  18. <canvas v-else-if="options.mode === 'canvas'" class="uqrcode-canvas" :id="options.canvasId" :canvas-id="options.canvasId" :style="{'width': `${options.size}px`, 'height': `${options.size}px`}" />
  19. </view>
  20. </template>
  21. <script>
  22. import uqrcode from './common/uqrcode'
  23. export default {
  24. name: 'uqrcode',
  25. // props: {
  26. // mode: {
  27. // type: String,
  28. // default: 'view' // view|canvas
  29. // }
  30. // },
  31. data() {
  32. return {
  33. options: uqrcode.defaults,
  34. modules: [],
  35. result: {}
  36. }
  37. },
  38. computed: {
  39. colSize() {
  40. return (this.options.size - this.options.margin * 2) / this.modules.length
  41. }
  42. },
  43. methods: {
  44. make(options) {
  45. options = {
  46. ...this.options,
  47. ...options
  48. }
  49. if (!options.mode) {
  50. options.mode = 'view'
  51. }
  52. if (!options.canvasId) {
  53. options.canvasId = this.uuid()
  54. }
  55. this.options = options
  56. if (options.mode === 'view') {
  57. this.modules = uqrcode.getModules(options)
  58. } else if (options.mode === 'canvas') {
  59. return new Promise((resolve, reject) => {
  60. uqrcode.make(options, this).then(res => {
  61. this.result = res
  62. resolve({
  63. ...res
  64. })
  65. }).catch(err => {
  66. reject(err)
  67. })
  68. })
  69. }
  70. },
  71. save() {
  72. if (this.options.mode === 'view') {
  73. uni.showToast({
  74. icon: 'none',
  75. title: 'view模式不支持保存,请提示用户使用截屏保存'
  76. })
  77. } else if (this.options.mode === 'canvas') {
  78. // #ifdef H5
  79. uni.showToast({
  80. icon: 'none',
  81. title: 'canvas H5不支持保存,请将二维码放置在image组件,再提示用户长按image保存'
  82. })
  83. // #endif
  84. // #ifndef H5
  85. uni.saveImageToPhotosAlbum({
  86. filePath: this.result.tempFilePath,
  87. success: (res) => {
  88. uni.showToast({
  89. icon: 'success',
  90. title: '保存成功'
  91. })
  92. },
  93. fail: (err) => {
  94. uni.showToast({
  95. icon: 'none',
  96. title: JSON.stringify(err)
  97. })
  98. }
  99. })
  100. // #endif
  101. }
  102. },
  103. uuid(len = 32, firstU = true, radix = null) {
  104. let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  105. let uuid = [];
  106. radix = radix || chars.length;
  107. if (len) {
  108. // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
  109. for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  110. } else {
  111. let r;
  112. // rfc4122标准要求返回的uuid中,某些位为固定的字符
  113. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  114. uuid[14] = '4';
  115. for (let i = 0; i < 36; i++) {
  116. if (!uuid[i]) {
  117. r = 0 | Math.random() * 16;
  118. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  119. }
  120. }
  121. }
  122. // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
  123. if (firstU) {
  124. uuid.shift();
  125. return 'u' + uuid.join('');
  126. } else {
  127. return uuid.join('');
  128. }
  129. }
  130. }
  131. }
  132. </script>
  133. <style>
  134. .uqrcode-view {
  135. /* #ifndef APP-NVUE */
  136. display: flex;
  137. box-sizing: border-box;
  138. /* #endif */
  139. flex-direction: column;
  140. }
  141. .uqrcode-view-row {
  142. /* #ifndef APP-NVUE */
  143. display: flex;
  144. box-sizing: border-box;
  145. /* #endif */
  146. flex-direction: row;
  147. }
  148. .uqrcode-view-col {
  149. /* #ifndef APP-NVUE */
  150. display: flex;
  151. box-sizing: border-box;
  152. /* #endif */
  153. }
  154. </style>