index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div class="mine">
  3. <div class="qr-code">
  4. <div id="qrCode" ref="qrCodeDiv"></div>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. import QRCode from "qrcodejs2";
  10. import api from "../../server/home";
  11. export default {
  12. components: {},
  13. data() {
  14. return {
  15. qrcode_string: "",
  16. ticket: "",
  17. checkCodeInterval: null,
  18. checkCodeTimeout: null
  19. };
  20. },
  21. computed: {},
  22. watch: {},
  23. methods: {
  24. getAuthCode() {
  25. api.getAuthCode().then(res => {
  26. this.qrcode_string = res.data.qrcode_string;
  27. this.ticket = res.data.ticket;
  28. this.$nextTick(() => {
  29. this.bindQRCode();
  30. this.checkCode();
  31. });
  32. });
  33. },
  34. checkCode() {
  35. this.checkCodeInterval = setInterval(e => {
  36. api.checkCode({ ticket: this.ticket }).then(res => {
  37. if (res.data.status == 1) {
  38. clearInterval(this.checkCodeInterval);
  39. this.getOrderByCode();
  40. }
  41. });
  42. }, 1000);
  43. //十分钟强制清除,防止页面崩溃
  44. this.checkCodeTimeout = setTimeout(() => {
  45. clearTimeout(this.checkCodeTimeout);
  46. }, 360000);
  47. },
  48. getOrderByCode() {
  49. api.getOrderByCode({ ticket: this.ticket }).then(res => {
  50. if (res.code == 200) {
  51. // 无订单入口
  52. if (res.data.order_id == 0) {
  53. this.$router.push({
  54. path: "/confirmOrder/addOrder",
  55. query: { //user_id
  56. data: JSON.stringify(res.data)
  57. }
  58. });
  59. return
  60. }
  61. this.$router.push({
  62. path: "/confirmOrder/details",
  63. query: {
  64. id: res.data.order_id
  65. }
  66. });
  67. }
  68. });
  69. },
  70. bindQRCode() {
  71. new QRCode(this.$refs.qrCodeDiv, {
  72. text: this.qrcode_string,
  73. width: 200,
  74. height: 200,
  75. colorDark: "#333333", //二维码颜色
  76. colorLight: "#ffffff", //二维码背景色
  77. correctLevel: QRCode.CorrectLevel.L //容错率,L/M/H
  78. });
  79. }
  80. },
  81. created() {
  82. this.getAuthCode();
  83. },
  84. mounted() {},
  85. destroyed() {
  86. if (this.checkCodeInterval) {
  87. clearInterval(this.checkCodeInterval);
  88. clearTimeout(this.checkCodeTimeout);
  89. }
  90. }
  91. };
  92. </script>
  93. <style lang='less' scoped>
  94. .mine {
  95. width: 100%;
  96. height: 100%;
  97. display: flex;
  98. justify-content: center;
  99. align-items: center;
  100. background: #ffffff;
  101. box-shadow: 0px 2px 4px 0px rgba(184, 191, 198, 0.2);
  102. border-radius: 8px;
  103. .qr-code {
  104. width: 200px;
  105. img {
  106. width: 100%;
  107. }
  108. }
  109. }
  110. </style>