uqrcode.vue 4.0 KB

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