index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <view>
  3. <view class="container-box">
  4. <view class="img-box">
  5. <canvas @tap="previewImg" canvasId="mycanvas" v-if="!canvasHidden" style="width: 686rpx; height: 686rpx; background: #f1f1f1"></canvas>
  6. </view>
  7. <form @submit="formSubmit">
  8. <view class="input-row">
  9. <label>网址</label>
  10. <input maxlength="255" name="url" :placeholder="placeholder" type="text" value="" />
  11. </view>
  12. <button class="mybtn" formType="submit">生成二维码</button>
  13. </form>
  14. </view>
  15. <view class="mask" v-if="!maskHidden"></view>
  16. <view class="canvas-box"></view>
  17. </view>
  18. </template>
  19. <script>
  20. var a = require('../../utils/qrcode.js');
  21. export default {
  22. data() {
  23. return {
  24. maskHidden: true,
  25. imagePath: '',
  26. placeholder: '请输入网址',
  27. canvasHidden: ''
  28. };
  29. },
  30. onLoad: function (a) {
  31. var t = this.setCanvasSize();
  32. var e = 'http://' + this.placeholder;
  33. this.createQrCode(e, 'mycanvas', t.w, t.h);
  34. },
  35. onReady: function () {},
  36. onShow: function () {},
  37. onHide: function () {},
  38. onUnload: function () {},
  39. methods: {
  40. setCanvasSize: function () {
  41. var a = {};
  42. try {
  43. var t = uni.getSystemInfoSync();
  44. var e = t.windowWidth / 1.0932944606413995;
  45. var n = e;
  46. a.w = e;
  47. a.h = n;
  48. } catch (a) {
  49. console.log('CatchClause', a);
  50. console.log('CatchClause', a);
  51. }
  52. return a;
  53. },
  54. createQrCode: function (t, e, n, i) {
  55. a.qrApi.draw(t, e, n, i);
  56. var that = this;
  57. var s = setTimeout(function () {
  58. that.canvasToTempImage();
  59. clearTimeout(s);
  60. }, 3000);
  61. },
  62. canvasToTempImage: function () {
  63. var that = this;
  64. uni.canvasToTempFilePath({
  65. canvasId: 'mycanvas',
  66. success: function (t) {
  67. var e = t.tempFilePath;
  68. that.imagePath=e;
  69. },
  70. fail: function (a) {}
  71. });
  72. },
  73. previewImg: function (a) {
  74. var t = this.imagePath;
  75. uni.previewImage({
  76. current: t,
  77. urls: [t]
  78. });
  79. },
  80. formSubmit: function (a) {
  81. var that = this;
  82. var e = a.detail.value.url;
  83. e = '' == e ? 'http://' + that.placeholder : 'http://' + e;
  84. that.maskHidden=false;
  85. uni.showToast({
  86. title: '生成中...',
  87. icon: 'loading',
  88. duration: 2000
  89. });
  90. var n = setTimeout(function () {
  91. uni.hideToast();
  92. var a = that.setCanvasSize();
  93. that.createQrCode(e, 'mycanvas', a.w, a.h);
  94. that.maskHidden=true;
  95. clearTimeout(n);
  96. }, 2000);
  97. }
  98. }
  99. };
  100. </script>
  101. <style>
  102. @import './index.css';
  103. </style>