| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <template>
- <view class="webview-page">
- <web-view
- :src="webviewUrl"
- @load="handleLoad"
- @error="handleError"
- class="webview-container"
- ></web-view>
- <!-- 加载状态 -->
- <view v-if="loading" class="loading-overlay">
- <view class="loading-content">
- <view class="loading-spinner"></view>
- <text class="loading-text">加载中...</text>
- </view>
- </view>
- </view>
- </template>
- <script setup>
- import { ref } from 'vue'
- import { onLoad } from '@dcloudio/uni-app'
- // 响应式数据
- const loading = ref(true)
- const webviewUrl = ref('')
- /**
- * 构建 webview URL
- */
- const buildWebviewUrl = (params) => {
- // 基础 H5 域名 - 同域部署
- // const baseUrl = 'http://127.0.0.1:5501/page/'
- // const baseUrl = 'https://yx.newfeifan.cn/page/'
- const baseUrl = 'https://m.hfdcschool.com/page'
-
-
- // 根据 dest 映射到对应的 HTML 文件
- const dest = params.dest || 'home'
- console.log(dest)
- let url = `${baseUrl}/${dest}.html`
- // 添加查询参数
- const queryParts = []
- // 传递所有参数给 H5
- Object.keys(params).forEach(key => {
- if (params[key] !== undefined && params[key] !== null) {
- // result参数已经是编码过的,不需要再次编码
- if (key === 'result') {
- queryParts.push(`${encodeURIComponent(key)}=${params[key]}`)
- } else {
- queryParts.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
- }
- }
- })
- // 添加小程序标识
- queryParts.push('from=miniprogram')
- queryParts.push(`timestamp=${Date.now()}`)
- // 添加认证信息 - JSESSIONID
- const jsessionId = uni.getStorageSync('JSESSIONID')
- if (jsessionId) {
- queryParts.push(`JSESSIONID=${encodeURIComponent(jsessionId)}`)
- }
- // 添加设备信息
- const deviceInfo = uni.getStorageSync("deviceInfo") || {}
- const devId = deviceInfo.deviceId || ''
- const sbmc = deviceInfo.model || ''
- if (devId) {
- queryParts.push(`devId=${encodeURIComponent(devId)}`)
- }
- if (sbmc) {
- queryParts.push(`sbmc=${encodeURIComponent(sbmc)}`)
- }
- if (queryParts.length > 0) {
- url += '?' + queryParts.join('&')
- }
- console.log('构建的 webview URL:', url)
- webviewUrl.value = url
- loading.value = false
- }
- /**
- * WebView 加载完成 - 监听 URL 参数
- */
- const handleLoad = (e) => {
- console.log('WebView 加载完成',e)
- loading.value = false
- // 获取当前 WebView 的 URL
- const currentUrl = e.detail.src || webviewUrl.value
- console.log('当前 WebView URL:', currentUrl+"."+e.detail.src)
- }
- /**
- * WebView 加载错误
- */
- const handleError = (e) => {
- console.error('WebView 加载错误:', e)
- loading.value = false
- }
- // 页面生命周期
- onLoad((options) => {
- console.log('webview 页面 onLoad:', options)
- buildWebviewUrl(options || {})
- })
- </script>
- <style scoped lang="scss">
- .webview-page {
- height: 100vh;
- background: #fff;
- }
- .webview-container {
- width: 100%;
- height: 100vh;
- }
- .loading-overlay {
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background: #fff;
- display: flex;
- align-items: center;
- justify-content: center;
- z-index: 999;
- }
- .loading-content {
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- .loading-spinner {
- width: 60rpx;
- height: 60rpx;
- border: 4rpx solid #f3f3f3;
- border-top: 4rpx solid #007AFF;
- border-radius: 50%;
- animation: spin 1s linear infinite;
- }
- .loading-text {
- margin-top: 32rpx;
- font-size: 28rpx;
- color: #666;
- }
- @keyframes spin {
- 0% { transform: rotate(0deg); }
- 100% { transform: rotate(360deg); }
- }
- </style>
|