日志
This commit is contained in:
@@ -1,79 +1,139 @@
|
||||
<template>
|
||||
<view>
|
||||
<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>
|
||||
<web-view v-if="urlAvailable" :src="url"></web-view>
|
||||
<view v-else class="error-container">
|
||||
<text class="error-text">无法访问目标地址,请检查网络连接</text>
|
||||
<button type="primary" @tap="retryConnection">重试</button>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { write } from '@/uni_modules/yk-log'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
url: '',
|
||||
originalUrl: 'http://192.168.100.201',
|
||||
loading: true,
|
||||
loadError: false
|
||||
url: 'http://192.168.100.201',
|
||||
urlAvailable: false,
|
||||
logMessages: [] // 用于收集日志
|
||||
};
|
||||
},
|
||||
onLoad(options) {
|
||||
this.addLog('🏠 home1页面开始加载')
|
||||
console.log('📄 home1 onLoad 执行', options)
|
||||
this.initWebView()
|
||||
},
|
||||
onReady() {
|
||||
this.addLog('✅ home1页面准备就绪')
|
||||
console.log('✅ home1 onReady 执行')
|
||||
},
|
||||
onShow() {
|
||||
this.addLog('👀 home1页面显示')
|
||||
console.log('✅ home1 onShow 执行')
|
||||
},
|
||||
onHide() {
|
||||
this.addLog('🔚 home1页面隐藏')
|
||||
console.log('🔚 home1 onHide 执行')
|
||||
},
|
||||
onUnload() {
|
||||
this.addLog('🗑️ home1页面卸载')
|
||||
console.log('🗑️ home1 onUnload 执行')
|
||||
},
|
||||
onError(err) {
|
||||
this.addLog('💥 home1页面错误: ' + err)
|
||||
console.error('💥 home1页面错误:', err)
|
||||
},
|
||||
methods: {
|
||||
initWebView() {
|
||||
this.loading = true
|
||||
this.loadError = false
|
||||
addLog(message) {
|
||||
const timestamp = new Date().toLocaleTimeString()
|
||||
this.logMessages.push(`[${timestamp}] ${message}`)
|
||||
console.log(message)
|
||||
write(message)
|
||||
},
|
||||
async initWebView() {
|
||||
this.addLog('🌐 开始初始化WebView')
|
||||
this.addLog(`🔗 检查URL可达性: ${this.url}`)
|
||||
|
||||
const timestamp = new Date().getTime()
|
||||
this.url = `${this.originalUrl}?timestamp=${timestamp}`
|
||||
|
||||
// 设置超时检测
|
||||
setTimeout(() => {
|
||||
if (this.loading) {
|
||||
this.loading = false
|
||||
this.loadError = true
|
||||
}
|
||||
}, 10000) // 10秒超时
|
||||
// 检查URL是否可达
|
||||
const isReachable = await this.checkUrlReachability(this.url)
|
||||
|
||||
if (isReachable) {
|
||||
this.urlAvailable = true
|
||||
this.addLog('✅ URL可达,加载WebView')
|
||||
} else {
|
||||
this.urlAvailable = false
|
||||
this.addLog('❌ URL不可达,显示错误页面')
|
||||
}
|
||||
|
||||
// 监听网络状态变化
|
||||
uni.onNetworkStatusChange((res) => {
|
||||
if (res.isConnected && this.loadError) {
|
||||
this.retryLoad()
|
||||
this.addLog(`📶 网络状态变化: ${res.isConnected ? '已连接' : '未连接'}`)
|
||||
if (res.isConnected) {
|
||||
this.retryConnection()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
onWebViewError(e) {
|
||||
console.error('WebView加载错误:', e)
|
||||
this.loading = false
|
||||
this.loadError = true
|
||||
// 检查URL可达性
|
||||
checkUrlReachability(url) {
|
||||
return new Promise((resolve) => {
|
||||
// 方法1: 使用uni.request检查
|
||||
uni.request({
|
||||
url: url,
|
||||
method: 'GET',
|
||||
timeout: 5000,
|
||||
success: (res) => {
|
||||
this.addLog(`✅ URL检查成功,状态码: ${res.statusCode}`)
|
||||
resolve(res.statusCode < 400) // 状态码小于400认为可达
|
||||
},
|
||||
fail: (err) => {
|
||||
if (err.errMsg && err.errMsg.includes('request:fail')) {
|
||||
this.addLog('❌ 网络请求失败')
|
||||
resolve(false)
|
||||
} else {
|
||||
// 其他错误(包括CORS)认为URL基本可达
|
||||
this.addLog(`⚠️ URL可能可达,但有CORS限制: ${err.errMsg}`)
|
||||
resolve(true)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
onMessage(e) {
|
||||
// WebView内部发送的消息,可以用于确认页面加载完成
|
||||
this.loading = false
|
||||
this.loadError = false
|
||||
},
|
||||
|
||||
retryLoad() {
|
||||
this.initWebView()
|
||||
// 重试连接
|
||||
async retryConnection() {
|
||||
this.addLog('🔄 尝试重新连接')
|
||||
const isReachable = await this.checkUrlReachability(this.url)
|
||||
this.urlAvailable = isReachable
|
||||
|
||||
if (isReachable) {
|
||||
this.addLog('✅ 重新连接成功')
|
||||
uni.showToast({
|
||||
title: '连接成功',
|
||||
icon: 'success'
|
||||
})
|
||||
} else {
|
||||
this.addLog('❌ 重新连接失败')
|
||||
uni.showToast({
|
||||
title: '连接失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
onUnload() {
|
||||
uni.offNetworkStatusChange()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.loading, .error {
|
||||
position: fixed;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
text-align: center;
|
||||
z-index: 9999;
|
||||
<style scoped>
|
||||
.error-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
}
|
||||
.error-text {
|
||||
font-size: 20px;
|
||||
line-height: 24px;
|
||||
color: #999;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
</style>
|
||||
@@ -1,43 +0,0 @@
|
||||
<template>
|
||||
<view class="content">
|
||||
<view class="">
|
||||
<input type="text" class="input" v-model="url">
|
||||
<button type="primary" size="size" @tap="toConfig">跳转</button>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
url: 'http://192.168.100.201'
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
toConfig () {
|
||||
let url = '/uni_modules/yykj-tv/pages/home/home' + '?url=' + this.url
|
||||
uni.redirectTo({
|
||||
url: url
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.content {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-image: linear-gradient(to bottom, #2E3092, #797979);
|
||||
}
|
||||
.input {
|
||||
width: 50vw;
|
||||
height: 5vh;
|
||||
background-color: #fff;
|
||||
margin-bottom: 2vh;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user