privacy.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view class="webview-wrapper">
  3. <!-- 加载状态提示 -->
  4. <view class="loading-mask" v-if="isLoading">
  5. <uni-loading-icon type="circle" size="24" color="#333"></uni-loading-icon>
  6. <text class="loading-txt">加载中...</text>
  7. </view>
  8. <web-view
  9. :src="h5Url"
  10. id="any-id"
  11. @load="onWebViewLoad"
  12. @error="onWebViewError"
  13. @message="onWebViewMessage"
  14. ></web-view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import { ref, onUnmounted } from "vue";
  19. import { onLoad } from "@dcloudio/uni-app";
  20. import { H5_BASE_URL, TOKENNAME, WHITELIST } from "@/config/app";
  21. import { useAppStore } from "@/stores/app";
  22. import { toLogin, checkLogin } from "@/libs/login";
  23. const h5Url = ref("");
  24. const appStore = useAppStore();
  25. const isLoading = ref(true);
  26. const errorMsg = ref("");
  27. onUnmounted(() => {
  28. isLoading.value = false;
  29. });
  30. onLoad((query) => {
  31. console.log('query',query)
  32. try {
  33. h5Url.value = query.path;
  34. } catch (err) {
  35. console.error("WebView 初始化失败:", err);
  36. uni.showToast({ title: "页面加载异常", icon: "none", duration: 1500 });
  37. setTimeout(() => uni.navigateBack(), 1500);
  38. }
  39. });
  40. // 标准化H5路径
  41. /**
  42. * web-view 加载成功
  43. */
  44. const onWebViewLoad = () => {
  45. isLoading.value = false; // 隐藏加载中
  46. };
  47. /**
  48. * web-view 加载失败
  49. * @param {object} err - 错误信息
  50. */
  51. const onWebViewError = (err) => {
  52. isLoading.value = false;
  53. errorMsg.value = `页面加载失败:${err.detail.errMsg}`;
  54. uni.showToast({ title: errorMsg.value, icon: "none", duration: 2000 });
  55. console.error("WebView 加载错误:", err);
  56. setTimeout(() => uni.navigateBack(), 1500);
  57. };
  58. /**
  59. * 接收 H5 发送的消息
  60. * @param {object} e - 消息事件
  61. */
  62. const onWebViewMessage = (e) => {
  63. const h5Msg = e.detail.data[0]; // H5 发送的消息格式为 { data: [消息体] }
  64. console.log("接收 H5 消息:", h5Msg);
  65. // 示例:H5 触发「返回小程序」
  66. if (h5Msg.type === "navigateBack") {
  67. uni.navigateBack({ delta: h5Msg.delta || 1 });
  68. }
  69. if (h5Msg.type === "refreshToken") {
  70. appStore.refreshToken().then((newToken) => {
  71. const webview = uni.createSelectorQuery().select("#any-id");
  72. webview
  73. .context((res) => {
  74. res.context.postMessage({
  75. data: { type: "newToken", token: newToken },
  76. });
  77. })
  78. .exec();
  79. });
  80. }
  81. };
  82. </script>
  83. <style scoped>
  84. .webview-wrapper {
  85. width: 100vw;
  86. height: 100vh;
  87. position: relative;
  88. }
  89. .loading-mask {
  90. position: absolute;
  91. top: 0;
  92. left: 0;
  93. width: 100%;
  94. height: 100%;
  95. background: rgba(255, 255, 255, 0.8);
  96. display: flex;
  97. flex-direction: column;
  98. justify-content: center;
  99. align-items: center;
  100. z-index: 999;
  101. }
  102. .loading-txt {
  103. margin-top: 16rpx;
  104. font-size: 28rpx;
  105. color: #666;
  106. }
  107. </style>