applePay.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <Popup isClose ref="popRef">
  3. <template #content>
  4. <view class="apple-pay__box">
  5. <view id="apple_pay_card"></view>
  6. </view>
  7. </template>
  8. </Popup>
  9. <GlobalLoading :msg="loadingStatus" />
  10. </template>
  11. <script setup>
  12. import { t } from '@/locale';
  13. import { ref, reactive, nextTick } from "vue";
  14. import Popup from '@/components/popup.vue';
  15. import GlobalLoading from '@/components/GlobalLoading'
  16. import { Toast } from "@/utils"
  17. import { useAppleInfo } from './hooks'
  18. const popRef = ref(null);
  19. // props
  20. const props = defineProps({
  21. paramsObj: { type: Object }
  22. })
  23. // emits
  24. const emit = defineEmits(['on-open', 'on-close', 'on-success', 'on-error']);
  25. const loadingStatus = reactive({ show: false });
  26. const { appleInstance, init, unmountCard, fetchCardPaymentInfo } = useAppleInfo();
  27. // 打开回调
  28. const open = () => {
  29. emit('on-open');
  30. popRef.value && popRef.value.open();
  31. nextTick(() => {
  32. fetchCardPaymentInfo(props.paramsObj, bindEventListener);
  33. })
  34. }
  35. // 关闭回调
  36. const close = () => {
  37. emit('on-close')
  38. unmountCard()
  39. popRef.value && popRef.value.close()
  40. }
  41. // 事件绑定
  42. function bindEventListener() {
  43. if (!appleInstance.value) return;
  44. appleInstance.value.on('success', onPaymentSuccess);
  45. appleInstance.value.on('error', onPaymentError);
  46. appleInstance.value.on('ready', onPaymentReady);
  47. }
  48. // 加载完成
  49. const onPaymentReady = (event) => {
  50. console.log('----- apple payment ready -----', event)
  51. }
  52. // 成功回调
  53. const onPaymentSuccess = (event) => {
  54. console.log('----- apple payment success -----', event)
  55. return
  56. emit('on-success');
  57. }
  58. // 失败回调
  59. const onPaymentError = (event) => {
  60. console.log('----- apple payment error -----', event)
  61. Toast(event.detail.error.message || event.detail.error || t("pay_fail_msg", {}) || "fail!");
  62. }
  63. defineExpose({
  64. open,
  65. close
  66. })
  67. </script>
  68. <style lang="less" scoped>
  69. @import url('@/style.less');
  70. .apple-pay__box {
  71. width: 100%;
  72. height: 25vh;
  73. #apple_pay_card {
  74. display: flex;
  75. align-items: center;
  76. padding: 12rpx;
  77. width: 100%;
  78. height: 44px;
  79. /* border: 1px solid #ccc;
  80. border-radius: 14rpx; */
  81. }
  82. }
  83. .submit_btn {
  84. height: 38px;
  85. padding: 16rpx 30rpx;
  86. background-color: var(--black);
  87. color: var(--light);
  88. .flex_center();
  89. border-radius: 16rpx;
  90. .size(24rpx);
  91. }
  92. </style>