payOrder.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <div class="pay-order-box">
  3. <div class="pay-order-item"><span class="label">订单编号:</span><span>{{orderNo}}</span></div>
  4. <div class="pay-order-item"><span class="label">支付方式:</span><span>微信支付</span></div>
  5. <div v-if="state === 'NOTPAY'">
  6. <el-skeleton :loading="loading" animated>
  7. <template slot="template">
  8. <div class="pay-img">
  9. <el-skeleton-item variant="image" style="width: 300px; height: 300px;"></el-skeleton-item>
  10. </div>
  11. </template>
  12. <template>
  13. <div class="pay-img" v-if="payUrl">
  14. <img :src="payUrl" alt="pay.png" />
  15. </div>
  16. </template>
  17. </el-skeleton>
  18. </div>
  19. <div v-else-if="state === 'SUCCESS'">
  20. <el-result icon="success" title="支付成功">
  21. <template slot="extra">
  22. <slot></slot>
  23. </template>
  24. </el-result>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. import {
  30. createPayQR,
  31. getOrderPayState
  32. } from '@/api/pay'
  33. export default {
  34. props: ['orderNo'],
  35. data() {
  36. return {
  37. payUrl: '',
  38. timer: null,
  39. state: 'NOTPAY',
  40. loading: false
  41. }
  42. },
  43. mounted() {
  44. this.init();
  45. },
  46. beforeDestroy() {
  47. this.stopTimer();
  48. },
  49. methods: {
  50. init() {
  51. this.loading = true;
  52. createPayQR(this.orderNo).then(res => {
  53. if (res.state) {
  54. this.payUrl = res.data.base64;
  55. this.findState();
  56. }
  57. this.loading = false;
  58. })
  59. },
  60. findState() {
  61. this.timer = setInterval(() => {
  62. getOrderPayState(this.orderNo).then(res => {
  63. if (res.state) {
  64. this.state = res.data;
  65. if (this.state !== 'NOTPAY') this.stopTimer();
  66. if (this.state === 'SUCCESS') this.$emit('callback', 'payState');
  67. }
  68. })
  69. }, 3000)
  70. },
  71. stopTimer() {
  72. if (!this.timer) return;
  73. clearTimeout(this.timer);
  74. this.timer = null;
  75. }
  76. },
  77. }
  78. </script>
  79. <style lang="scss">
  80. .pay-order-box {
  81. padding: 20px;
  82. .pay-order-item {
  83. margin-bottom: 10px;
  84. .label {
  85. font-weight: bold;
  86. }
  87. }
  88. .pay-img {
  89. display: flex;
  90. justify-content: center;
  91. align-items: center;
  92. img {
  93. width: 300px;
  94. height: 300px;
  95. }
  96. }
  97. }
  98. </style>