Files
tv-pro/uni_modules/yykj-tv/pages/home/home1.vue

79 lines
1.5 KiB
Vue
Raw Normal View History

2025-09-02 11:29:52 +08:00
<template>
<view>
2025-10-31 10:30:09 +08:00
<web-view :src="url" @onPostMessage="onMessage" @error="onWebViewError"></web-view>
<view v-if="loading" class="loading">页面加载中...</view>
<view v-if="loadError" class="error">
页面加载失败
<button @tap="retryLoad">重新加载</button>
</view>
2025-09-02 11:29:52 +08:00
</view>
</template>
<script>
export default {
data() {
return {
2025-10-31 10:30:09 +08:00
url: '',
originalUrl: 'http://192.168.100.201',
loading: true,
loadError: false
2025-09-02 11:29:52 +08:00
};
},
2025-10-31 10:30:09 +08:00
onLoad(options) {
this.initWebView()
2025-09-02 11:29:52 +08:00
},
2025-10-31 10:30:09 +08:00
methods: {
initWebView() {
this.loading = true
this.loadError = false
const timestamp = new Date().getTime()
this.url = `${this.originalUrl}?timestamp=${timestamp}`
// 设置超时检测
setTimeout(() => {
if (this.loading) {
this.loading = false
this.loadError = true
}
}, 10000) // 10秒超时
uni.onNetworkStatusChange((res) => {
if (res.isConnected && this.loadError) {
this.retryLoad()
}
})
},
onWebViewError(e) {
console.error('WebView加载错误:', e)
this.loading = false
this.loadError = true
},
onMessage(e) {
// WebView内部发送的消息可以用于确认页面加载完成
this.loading = false
this.loadError = false
},
retryLoad() {
this.initWebView()
}
},
onUnload() {
2025-09-02 11:29:52 +08:00
uni.offNetworkStatusChange()
}
}
2025-10-31 10:30:09 +08:00
</script>
<style>
.loading, .error {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
z-index: 9999;
}
</style>