1
0

index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * FeHelper QR Code Tools
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. textContent: '',
  8. qrSize: 200,
  9. qrColor: '#000000',
  10. useIcon: 'no'
  11. },
  12. mounted: function () {
  13. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  14. chrome.runtime.onMessage.addListener((request, sender, callback) => {
  15. let MSG_TYPE = Tarp.require('../static/js/msg_type');
  16. if (request.type === MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event === MSG_TYPE.QR_CODE) {
  17. if (request.content) {
  18. this.textContent = request.content;
  19. this.convert();
  20. }
  21. }
  22. });
  23. this.$refs.codeSource.focus();
  24. // 拖拽注册
  25. document.addEventListener('drop', (e) => {
  26. e.preventDefault();
  27. e.stopPropagation();
  28. let files = e.dataTransfer.files;
  29. if (files.length) {
  30. this._drawIcon(files[0]);
  31. }
  32. }, false);
  33. document.addEventListener('dragover', (e) => {
  34. e.preventDefault();
  35. e.stopPropagation();
  36. }, false);
  37. // color picker绑定
  38. $("#opt_fc").colorpicker({
  39. fillcolor: true,
  40. success: (obj, color) => {
  41. this.qrColor = color;
  42. this.convert();
  43. }
  44. })
  45. },
  46. methods: {
  47. convert: function () {
  48. this.$nextTick(() => {
  49. if (this.textContent) {
  50. $('#preview').html('').qrcode(this._createOptions());
  51. } else {
  52. $('#preview').html('再在输入框里输入一些内容,就能生成二维码了哦~')
  53. }
  54. });
  55. },
  56. fileChanged: function (event) {
  57. if (event.target.files.length) {
  58. this._drawIcon(event.target.files[0]);
  59. event.target.value = '';
  60. }
  61. },
  62. _drawIcon: function (file) {
  63. if (/image\//.test(file.type)) {
  64. this.useIcon = 'custom';
  65. let reader = new FileReader();
  66. reader.onload = (evt) => {
  67. this.$refs.logoCustom.src = evt.target.result;
  68. this.convert();
  69. };
  70. reader.readAsDataURL(file);
  71. } else {
  72. alert('请选择图片文件!');
  73. }
  74. },
  75. _createOptions: function () {
  76. let defaultOptions = {
  77. // render method: 'canvas', 'image' or 'div'
  78. render: 'canvas',
  79. // version range somewhere in 1 .. 40
  80. minVersion: 1,
  81. maxVersion: 40,
  82. // error correction level: 'L', 'M', 'Q' or 'H'
  83. ecLevel: 'L',
  84. // offset in pixel if drawn onto existing canvas
  85. left: 0,
  86. top: 0,
  87. // size in pixel
  88. size: +this.qrSize || 200,
  89. // code color or image element
  90. fill: this.qrColor,
  91. // background color or image element, null for transparent background
  92. background: '#fff',
  93. // corner radius relative to module width: 0.0 .. 0.5
  94. radius: 0,
  95. // quiet zone in modules
  96. quiet: 0,
  97. text: this.textContent,
  98. // modes
  99. // 0: normal
  100. // 1: label strip
  101. // 2: label box
  102. // 3: image strip
  103. // 4: image box
  104. mode: 0,
  105. mSize: 0.15,
  106. mPosX: 0.5,
  107. mPosY: 0.5,
  108. label: 'FH',
  109. fontname: 'sans',
  110. fontcolor: '#f00',
  111. image: false
  112. };
  113. // 判断是否需要设置icon
  114. switch (this.useIcon) {
  115. case 'default':
  116. defaultOptions.mode = 4;
  117. defaultOptions.image = this.$refs.logoDefault;
  118. break;
  119. case 'custom':
  120. defaultOptions.mode = 4;
  121. defaultOptions.image = this.$refs.logoCustom;
  122. break;
  123. }
  124. return defaultOptions;
  125. }
  126. }
  127. });