WechatPay.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <div class="wechat-pay">
  3. <div class="order">
  4. <div>
  5. <div>订单编号:{{ props.order.id }}</div>
  6. <div>订单类型:{{ props.order.goods.type }}</div>
  7. </div>
  8. <div class="flex items-center">
  9. 应付金额:
  10. <span style="font-size: 20px; color: #f5222d">
  11. ¥{{ props.order.amount }}</span
  12. >
  13. </div>
  14. </div>
  15. <div class="wepay">
  16. <h5>微信支付</h5>
  17. <div class="flex center middle">
  18. <div class="mr-20">
  19. <img class="wepay-qrcode" :src="payQRCode" />
  20. <div class="wepay-text">
  21. <div>请使用微信扫描</div>
  22. <div>二维码完成支付</div>
  23. </div>
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. </template>
  29. <script lang="ts" setup>
  30. import { onBeforeMount, onUnmounted, ref } from 'vue';
  31. import axios from 'axios';
  32. import QRCode from 'qrcode';
  33. const props = defineProps<{
  34. order: any;
  35. codeUrl: string;
  36. }>();
  37. const emit = defineEmits(['success']);
  38. const payType = ref('wechat');
  39. const payQRCode = ref('');
  40. let timer: any;
  41. onBeforeMount(async () => {
  42. payQRCode.value = await QRCode.toDataURL(props.codeUrl);
  43. timer = setInterval(async () => {
  44. const success = await getPayResult();
  45. if (success) {
  46. clearInterval(timer);
  47. emit('success', success);
  48. }
  49. }, 5000);
  50. });
  51. onUnmounted(() => {
  52. clearInterval(timer);
  53. });
  54. const getPayResult = async () => {
  55. const result: { state: number } = await axios.post('/api/order/pay/state', {
  56. id: props.order.id || props.order._id,
  57. });
  58. if (result && result.state) {
  59. return true;
  60. }
  61. };
  62. </script>
  63. <style lang="postcss" scoped>
  64. .wechat-pay {
  65. .order {
  66. padding: 10px 0;
  67. line-height: 30px;
  68. display: flex;
  69. justify-content: space-between;
  70. align-items: center;
  71. }
  72. .pay-type {
  73. background-color: #f7f8fa;
  74. }
  75. .wepay {
  76. color: var(--color-title);
  77. margin-top: 16px;
  78. }
  79. .wepay-qrcode {
  80. width: 150px;
  81. margin-top: 24px;
  82. }
  83. .wepay-text {
  84. padding: 10px 40px;
  85. line-height: 20px;
  86. color: #ffffff;
  87. font-size: 13px;
  88. }
  89. }
  90. </style>