uniapp 长时间不操作,自动退出登录页

store 下的inex.js文件

import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		// 记录最后一次点击时间的元素
		lastTime: new Date().getTime(),
	},
	mutations: {
		//点击事件调用,刷新最后一次点击时间
		lastTimeUpdata: (state, lastTime) => {
			state.lastTime = lastTime;
		}
	},
	actions: {}
})
export default store

time.js

import store from '../store/index.js';
export const timeOut = () => {
	let WebviewList = [];
	// 上一次点击时间
	let lastTime = null
	// 当前时间
	let currentTime = null
	// 超时时间【可以自己设置】
	// let sys_timeout = 5 * 1000
	let sys_timeout = 30 * 60 * 1000
	// 每隔多长时间检查是否超时【可以自己设置】
	let check_time = 1000
	// 计时器【此为功能实现的方法,现在为空】	
	let goOut = null
	const isTimeOut = () => {
		// 页面上一次的点击时间
		lastTime = store.state.lastTime
		currentTime = new Date().getTime()
		// 超时了
		if ((currentTime - lastTime) > sys_timeout) {
			return true;
		} else {
			return false;
		}
	}
	const isLoginOut = () => {
		clearInterval(goOut);
		//setInterval方法来确定多长时间检测一次有没有超时
		goOut = setInterval(() => {

			// #ifdef APP-PLUS
			var pages = getCurrentPages();
			var page = pages[pages.length - 1];
			var currentWebview = page.$getAppWebview();
			// console.log(currentWebview); //获得当前webview的id

			// 判断一下是否超时,如果超时了就退出
			if (isTimeOut()) {
				if (currentWebview.__uniapp_route != 'pages/index/index') {
					// 需要转到的页面【这里用你自己的跳转方法和地址】
					uni.reLaunch({
						url: `/pages/index/index`,
					});
				}
				//已经超时跳转到相应界面,不需要计时了
				// clearInterval(goOut)
			} else {

			}
			let flag = false;
			for (let webview of WebviewList) {
				if (webview.__uniapp_route == currentWebview.__uniapp_route) {
					flag = true;
				}
			}
			if (!flag) {
				WebviewList.push(currentWebview);
				currentWebview.addEventListener('touchstart', function() {
					console.log('点击了');
					store.commit('lastTimeUpdata', new Date().getTime());
				}, false);
				currentWebview.addEventListener('close', function() {
					// console.log('关闭了');
					for (var i = 0; i < WebviewList.length; i++) {
						if (WebviewList[i].__uniapp_route == currentWebview
							.__uniapp_route) {
							WebviewList.splice(i, 1);
						}
					}
				}, false);
			}

			// #endif

			// #ifdef H5
			if (isTimeOut()) {
				// 需要转到的页面【这里用你自己的跳转方法和地址】
				uni.reLaunch({
					url: `/pages/index/index`,
				});
				//已经超时跳转到相应界面,不需要计时了
				// clearInterval(goOut)
			}
			// #endif


		}, check_time);
	}
	isLoginOut();
}


main.js 挂载全局

import store from './store/index.js'
Vue.prototype.$store = store

app.vue文件引用

<script>
import { timeOut } from './common/time.js';
export default {
	onLaunch: function() {
		let that = this;
		console.log('App Launch');
		timeOut();
		// #ifdef H5
		// /* 实现全局点击事件监听 */
		setTimeout(function() {
			var body_ = document.getElementsByTagName('body')[0];
			body_.addEventListener('click', function() {
				that.$store.commit('lastTimeUpdata', new Date().getTime());
			});
		}, 1000);
		// #endif
	},
	onShow: function() {
		console.log('App Show');
	},
	onHide: function() {
		console.log('App Hide');
	}
};
</script>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值