kyc.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <cwg-page-wrapper>
  3. <view class="safe-wrapper">
  4. <web-view class="aaa" v-if="qrCodeUrl" :src="qrCodeUrl" :webview-styles="webviewStyles"></web-view>
  5. </view>
  6. </cwg-page-wrapper>
  7. </template>
  8. <script setup lang="ts">
  9. import { ref } from "vue";
  10. import { useI18n } from "vue-i18n";
  11. import useRouter from "@/hooks/useRouter";
  12. import { ucardApi } from "@/api/ucard";
  13. import { onLoad } from '@dcloudio/uni-app'
  14. import { computed, onMounted } from 'vue'
  15. import useGlobalStore from '@/stores/use-global-store'
  16. import { permission } from "@/js_sdk/wa-permission/permission.js"
  17. const globalStore = useGlobalStore()
  18. const statusBarHeight = computed(() => globalStore.statusBarHeight);
  19. const cardId = ref("");
  20. onLoad((options) => {
  21. // #ifdef APP-PLUS
  22. const isIos = (plus.os.name == "iOS")
  23. // #endif
  24. // #ifdef APP-PLUS
  25. if (isIos) {
  26. const camera = permission.judgeIosPermission('camera')
  27. console.log(camera, 'iOS camera permission');
  28. if (!camera) {
  29. permission.gotoAppPermissionSetting()
  30. }
  31. } else {
  32. permission.requestAndroidPermission('android.permission.CAMERA').then((result) => {
  33. console.log(result, 'Android camera permission result');
  34. if (result !== 1) {
  35. permission.gotoAppPermissionSetting()
  36. }
  37. })
  38. }
  39. // #endif
  40. cardId.value = options?.cardId
  41. getWebsdkLink();
  42. });
  43. const webviewStyles = ref({
  44. progress: {
  45. color: '#ea002a'
  46. },
  47. bounce: 'none',
  48. scrollIndicator: 'none',
  49. top: statusBarHeight.value + 60 + 'px',
  50. bottom: '0px'
  51. })
  52. const { t } = useI18n();
  53. const router = useRouter();
  54. // 二维码链接
  55. const qrCodeUrl = ref("");
  56. // API 响应码
  57. const responseCode = ref(200);
  58. // 设备元信息
  59. const metaInfo = ref<Record<string, any> | null>(null);
  60. /**
  61. * 获取设备元信息
  62. */
  63. function getMetaInfo() {
  64. try {
  65. // 安全获取 metaInfo,兼容不同环境
  66. if (typeof window !== "undefined" && (window as any).getMetaInfo) {
  67. metaInfo.value = (window as any).getMetaInfo();
  68. metaInfo.value = { ...metaInfo.value, deviceType: "h5" };
  69. } else {
  70. // 默认值
  71. metaInfo.value = { deviceType: "h5" };
  72. }
  73. } catch (error) {
  74. console.warn("获取设备信息失败:", error);
  75. metaInfo.value = { deviceType: "h5" };
  76. }
  77. }
  78. /**
  79. * 获取 WebSDK 链接
  80. * @param cardId 卡片ID
  81. */
  82. async function getWebsdkLink() {
  83. if (!cardId.value) {
  84. console.warn("cardId 为空,无法获取 WebSDK 链接");
  85. return;
  86. }
  87. try {
  88. // 获取设备信息
  89. getMetaInfo();
  90. // 调用 API
  91. const res = await ucardApi.getWebsdkLink({
  92. cardId: String(cardId.value),
  93. metaInfo: metaInfo.value,
  94. });
  95. responseCode.value = res.code || 201;
  96. if (res.code === 200 && res.data) {
  97. try {
  98. // 安全解析 JSON
  99. const data = typeof res.data === "string" ? JSON.parse(res.data) : res.data;
  100. qrCodeUrl.value = data.url || data.link || "";
  101. } catch (parseError) {
  102. console.error("解析响应数据失败:", parseError);
  103. responseCode.value = 201;
  104. }
  105. } else {
  106. qrCodeUrl.value = "";
  107. }
  108. } catch (error: any) {
  109. console.error("获取 WebSDK 链接失败:", error);
  110. responseCode.value = 201;
  111. qrCodeUrl.value = "";
  112. }
  113. }
  114. </script>
  115. <style scoped lang="scss">
  116. @import "@/uni.scss";
  117. .aaa {
  118. background-color: #fff;
  119. position: absolute;
  120. top: env(safe-area-inset-top);
  121. left: 0;
  122. right: 0;
  123. bottom: env(safe-area-inset-bottom);
  124. }
  125. .safe-wrapper {
  126. position: fixed;
  127. top: 0;
  128. left: 0;
  129. right: 0;
  130. bottom: 0;
  131. padding-top: env(safe-area-inset-top);
  132. padding-bottom: env(safe-area-inset-bottom);
  133. background-color: #fff;
  134. }
  135. </style>