uniapp webview 页面重启功能实现 - 定时重启 webview
<template>
<view class='content' style='display: flex;align-items: center;justify-content: center;'>
<!-- <web-view @longtap='longTapTab' src='http://dcim.hxyt.net/'></web-view> -->
</view>
</template>
<script>
import {
isNotEmpty,
checkDialog
} from '../../common/util.js'
var wv;
export default {
data() {
return {
// 定时获取token
timer: null,
// 定时重启webview
restartTimer: null
}
},
// 监听返回键
onBackPress() {
uni.setStorageSync('firstFlag', false)
setTimeout(() => {
uni.reLaunch({
url: '/pages/login'
})
}, 300)
},
onLoad() {
// 强制横屏
plus.screen.lockOrientation('landscape-primary');
},
mounted() {
// this.createWebView();
let tokenSetTime = Number.parseInt(uni.getStorageSync('tokenSetTime')) * 1000
setTimeout(() => {
this.createWebView();
}, tokenSetTime)
let clearCacheTime = Number.parseInt(uni.getStorageSync('clearCacheTime')) * 1000
// 关闭web-view清空缓存定时器
this.timer = setInterval(() => {
uni.setStorageSync('clearCacheFlag', true)
setTimeout(() => {
uni.reLaunch({
url: '/pages/login'
})
}, 300)
}, clearCacheTime);
// 重启webview定时器
this.restartTimer = setInterval(() => {
this.createWebView();
}, 1000 * 60 * 60 * 24); // 每天三点重启webview
},
methods: {
loginRound() {
uni.request({
url: 'http://' + uni.getStorageSync('ip') + ':' + uni.getStorageSync('gatewayPort') +
'/api/sso-service/sso/login',
method: 'POST',
data: {
'clientId': 'ycEDNFKtLSND9d7W',
'secret': 'admin.intx',
'username': uni.getStorageSync('userName'),
'password': uni.getStorageSync('userPwd')
},
success: res => {
if (res.data.code == 0) {
uni.setStorageSync('token', res.data.data.accessToken)
uni.setStorageSync('refreshToken', res.data.data.refreshToken)
// 创建新token的webview
this.createWebView();
}
},
fail: res => {
uni.reLaunch({
url: '/pages/login'
}) //要用这个才可以
checkDialog('网络异常!请重新登录!', 'none', 2000)
}
})
},
// 创建webview
createWebView() {
if (isNotEmpty(wv)) {
wv.close()
}
wv = plus.webview.create('', 'custom-webview', { hardwareAccelerated: true })
wv.evalJS(`document.cookie='token=${uni.getStorageSync('token')};path=/'`)
wv.evalJS(`document.cookie='refreshToken=${uni.getStorageSync('refreshToken')};path=/'`)
wv.loadURL('http://' + uni.getStorageSync('ip') + ':' + uni.getStorageSync('webPort'))
// wv.loadURL('http://192.168.74.56:8080/')
//此对象相当于html5plus里的plus.webview.currentWebview()。
// 在uni-app里vue页面直接使用plus.webview.currentWebview()无效,
// 非v3编译模式使用this.$mp.page.$getAppWebview()
wv.addEventListener('error', function(e) {
checkDialog('页面请求失败,请检验输入是否正确!', 'none', 2000)
uni.reLaunch({
url: '/pages/login'
})
}, false);
wv.addEventListener('loading', () => {
plus.nativeUI.showWaiting('加载中...')
// console.log('loading')
}, false);
wv.addEventListener('loaded', () => {
plus.nativeUI.closeWaiting();
// console.log('loaded')
}, false);
var currentWebview = this.$scope
.$getAppWebview();
currentWebview.append(wv)
//如果是页面初始化调用时,需要延时一下
setTimeout(function() {
let web = currentWebview.children()[0];
web.setStyle({
// 全屏铺开
videoFullscreen: 'landscape',
});
}, 1000);
// console.log(wv.getURL())
// wv.evalJS('console.log(document.cookie)')
},
// 清空缓存
clearCache() {
let that = this;
let os = plus.os.name;
if (os == 'Android') {
let main = plus.android.runtimeMainActivity();
let sdRoot = main.getCacheDir();
let files = plus.android.invoke(sdRoot, 'listFiles');
let len = files.length;
for (let i = 0; i < len; i++) {
let filePath = '' + files[i]; // 没有找到合适的方法获取路径,这样写可以转成文件路径
plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
if (entry.isDirectory) {
entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录
uni.showToast({
title: '缓存清理完成',
duration: 2000
});
that.formatSize(); // 重新计算缓存
}, function(e) {
console.log(e.message)
});
} else {
entry.remove();
}
}, function(e) {
console.log('文件路径读取失败')
});
}
} else { // ios
plus.cache.clear(function() {
uni.showToast({
title: '缓存清理完成',
duration: 2000
});
that.formatSize();
});
}
},
},
destroyed() {
this.clearCache()
clearInterval(this.timer)
this.timer = null
this.getUrlTimer = null
clearInterval(this.restartTimer);
this.restartTimer = null;
wv.close();
}
}
</script>
<style>
.content {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
</style>
原文地址: https://www.cveoy.top/t/topic/oYWd 著作权归作者所有。请勿转载和采集!