index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // user_id:res.data.user.user_id
  58. }
  59. });
  60. return
  61. }
  62. this.$router.push({
  63. path: "/confirmOrder/details",
  64. query: {
  65. id: res.data.order_id
  66. }
  67. });
  68. }
  69. });
  70. },
  71. bindQRCode() {
  72. new QRCode(this.$refs.qrCodeDiv, {
  73. text: this.qrcode_string,
  74. width: 200,
  75. height: 200,
  76. colorDark: "#333333", //二维码颜色
  77. colorLight: "#ffffff", //二维码背景色
  78. correctLevel: QRCode.CorrectLevel.L //容错率,L/M/H
  79. });
  80. }
  81. },
  82. created() {
  83. this.getAuthCode();
  84. },
  85. mounted() {},
  86. destroyed() {
  87. if (this.checkCodeInterval) {
  88. clearInterval(this.checkCodeInterval);
  89. clearTimeout(this.checkCodeTimeout);
  90. }
  91. }
  92. };
  93. </script>
  94. <style lang='less' scoped>
  95. .mine {
  96. width: 100%;
  97. height: 100%;
  98. display: flex;
  99. justify-content: center;
  100. align-items: center;
  101. background: #ffffff;
  102. box-shadow: 0px 2px 4px 0px rgba(184, 191, 198, 0.2);
  103. border-radius: 8px;
  104. .qr-code {
  105. width: 200px;
  106. img {
  107. width: 100%;
  108. }
  109. }
  110. }
  111. </style>