index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { defineComponent } from 'vue';
  2. import { AwesomeQR } from 'vue-qr/src/lib/awesome-qr';
  3. function toBoolean(val: any): boolean {
  4. if (val === '') return val;
  5. return val === 'true' || val == '1';
  6. }
  7. function readAsArrayBuffer(url: any) {
  8. return new Promise(resolve => {
  9. const xhr = new XMLHttpRequest();
  10. xhr.responseType = 'blob'; //设定返回数据类型为Blob
  11. xhr.onload = function () {
  12. const reader = new FileReader();
  13. reader.onloadend = function () {
  14. resolve(reader.result);
  15. };
  16. reader.readAsArrayBuffer(xhr.response); //xhr.response就是一个Blob,用FileReader读取
  17. };
  18. xhr.open('GET', url);
  19. xhr.send();
  20. });
  21. }
  22. export default defineComponent({
  23. name: 'TheQrCode',
  24. props: {
  25. text: {
  26. type: String,
  27. required: true
  28. },
  29. qid: {
  30. type: String
  31. },
  32. correctLevel: {
  33. type: Number,
  34. default: 0
  35. },
  36. size: {
  37. type: Number,
  38. default: 220
  39. },
  40. margin: {
  41. type: Number,
  42. default: 20
  43. },
  44. colorDark: {
  45. type: String,
  46. default: '#000000'
  47. },
  48. colorLight: {
  49. type: String,
  50. default: '#FFFFFF'
  51. },
  52. bgSrc: {
  53. type: String,
  54. default: undefined
  55. },
  56. background: {
  57. type: String,
  58. default: 'rgba(0,0,0,0)'
  59. },
  60. backgroundDimming: {
  61. type: String,
  62. default: 'rgba(0,0,0,0)'
  63. },
  64. logoSrc: {
  65. type: String,
  66. default: undefined
  67. },
  68. logoBackgroundColor: {
  69. type: String,
  70. default: 'rgba(255,255,255,1)'
  71. },
  72. gifBgSrc: {
  73. type: String,
  74. default: undefined
  75. },
  76. logoScale: {
  77. type: Number,
  78. default: 0.2
  79. },
  80. logoMargin: {
  81. type: Number,
  82. default: 0
  83. },
  84. logoCornerRadius: {
  85. type: Number,
  86. default: 8
  87. },
  88. whiteMargin: {
  89. type: [Boolean, String],
  90. default: true
  91. },
  92. dotScale: {
  93. type: Number,
  94. default: 1
  95. },
  96. autoColor: {
  97. type: [Boolean, String],
  98. default: true
  99. },
  100. binarize: {
  101. type: [Boolean, String],
  102. default: false
  103. },
  104. binarizeThreshold: {
  105. type: Number,
  106. default: 128
  107. },
  108. callback: {
  109. type: Function,
  110. default: function () {
  111. return undefined;
  112. }
  113. },
  114. bindElement: {
  115. type: Boolean,
  116. default: true
  117. },
  118. backgroundColor: {
  119. type: String,
  120. default: '#FFFFFF'
  121. },
  122. components: {
  123. default: function () {
  124. return {
  125. data: {
  126. scale: 1
  127. },
  128. timing: {
  129. scale: 1,
  130. protectors: false
  131. },
  132. alignment: {
  133. scale: 1,
  134. protectors: false
  135. },
  136. cornerAlignment: {
  137. scale: 1,
  138. protectors: true
  139. }
  140. };
  141. }
  142. }
  143. },
  144. data() {
  145. return {
  146. imgUrl: '' as any
  147. };
  148. },
  149. watch: {
  150. $props: {
  151. deep: true,
  152. handler() {
  153. this.main();
  154. }
  155. }
  156. },
  157. mounted() {
  158. this.main();
  159. },
  160. methods: {
  161. async main() {
  162. // const that = this;
  163. if (this.gifBgSrc) {
  164. const gifImg = await readAsArrayBuffer(this.gifBgSrc);
  165. const logoImg = this.logoSrc;
  166. this.render(undefined, logoImg, gifImg);
  167. return;
  168. }
  169. const bgImg = this.bgSrc;
  170. const logoImg = this.logoSrc;
  171. this.render(bgImg, logoImg);
  172. },
  173. async render(img: any, logoImg: any, gifBgSrc?: any) {
  174. console.log(img, logoImg, gifBgSrc);
  175. new AwesomeQR({
  176. gifBackground: gifBgSrc,
  177. text: this.text,
  178. size: this.size,
  179. margin: this.margin,
  180. colorDark: this.colorDark,
  181. colorLight: this.colorLight,
  182. backgroundColor: this.backgroundColor,
  183. backgroundImage: img,
  184. backgroundDimming: this.backgroundDimming,
  185. logoImage: logoImg + '?' + new Date().getTime(),
  186. logoScale: this.logoScale,
  187. logoBackgroundColor: this.logoBackgroundColor,
  188. correctLevel: this.correctLevel,
  189. logoMargin: this.logoMargin,
  190. logoCornerRadius: this.logoCornerRadius,
  191. whiteMargin: toBoolean(this.whiteMargin),
  192. dotScale: this.dotScale,
  193. autoColor: toBoolean(this.autoColor),
  194. components: this.components
  195. })
  196. .draw()
  197. .then((dataUri: any) => {
  198. console.log('🚀 ~ dataUri:', dataUri);
  199. this.imgUrl = dataUri;
  200. this.callback && this.callback(dataUri, this.qid);
  201. });
  202. }
  203. },
  204. render() {
  205. return (
  206. <>
  207. {this.bindElement && this.imgUrl && (
  208. <img style="display: inline-block" src={this.imgUrl} />
  209. )}
  210. </>
  211. );
  212. }
  213. });