element-vue實現(xiàn)網(wǎng)頁鎖屏功能(示例代碼)
element-vue實現(xiàn)網(wǎng)頁鎖屏功能
1.寫一個鎖屏頁面,這里比較簡單,自己定義一下,需要放到底層HTML中哦,比如index.html
<div id="appIndex"> <el-dialog title="請輸入密碼解鎖屏幕" :visible.sync="lockScreenFlag" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" :append-to-body="true" width="500px" center> <el-form :model="form" :rules="rules" ref="form"> <el-form-item label="用戶名" prop="loginName"> <el-input v-model="form.loginName" autocomplete="off" :disabled="true" prefix-icon="el-icon-user-solid"></el-input> </el-form-item> <el-form-item label="密碼" prop="password"> <!-- <el-input type="password" v-model="form.password" autocomplete="off" prefix-icon="el-icon-lock"></el-input>--> <el-input prefix-icon="el-icon-lock" placeholder="請輸入密碼" :type="inputType?'text':'password'" v-model="form.password"> <i slot="suffix" :class="inputType?'el-icon-minus':'el-icon-view'" style="margin-top:8px;font-size:18px;" autocomplete="auto" @click="inputType=!inputType"></i> </el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer" style="text-align: right; padding-right: 5px;"> <el-button type="primary" @click="submitPassword" size="small">確 定</el-button> </div> </el-dialog> </div>
2.里面需要結(jié)合Vue雙向綁定的成分
//用戶信息 let user = [[${user}]] //過期事件, let lockScreenTime = 30 let app = new Vue({ el: '#appIndex', data: function () { var passwordSuccess = (rule, value, callback) => { request.post(ctx+"system/user/checkLoginNameAndPassword",Qs.stringify(this.form)).then(res=>{ if (res.data == 0){ callback(); } else if (res.data == 1){ callback(new Error("輸入的密碼錯誤或輸入了非法用戶名")); } else { callback(new Error(res.data.msg)); } }) } return { lockScreenFlag: false, timer: undefined, time: parseFloat(lockScreenTime)*1000*60, form:{ loginName:user.loginName, password: '', }, inputType: false, rules: { password: [ {required: true, message: '請輸入用戶名密碼', trigger: 'blur'}, {validator: passwordSuccess, trigger: 'blur'}, ], }, } }, created: function () { if (window.localStorage.getItem("lockScreenFlag")!=undefined){ let lockScreenFlag = window.localStorage.getItem("lockScreenFlag"); if (lockScreenFlag == '0'){ this.lockScreenFlag = false; $("#wrapper").css("pointer-events","auto") }else { $("#wrapper").css("pointer-events","none") this.lockScreenFlag = true; } } this.move(); }, mounted(){ let _this = this; window.document.onmousemove = function () { _this.move(); } window.move = this.move; window.openScreen = this.openScreen; }, methods: { submitPassword(){ this.$refs['form'].validate((valid) => { if (valid) { this.lockScreenFlag = false; $("#wrapper").css("pointer-events","auto") window.localStorage.setItem("lockScreenFlag",'0') } }) }, lockScreen(){ window.clearTimeout(this.timer) this.timer = window.setTimeout(this.openScreen,this.time) }, openScreen(){ if (!this.lockScreenFlag){ this.lockScreenFlag = true; $("#wrapper").css("pointer-events","none") window.localStorage.setItem("lockScreenFlag",'1') } }, move(){ if (!this.lockScreenFlag){ this.lockScreen() } } } })
擴展
前端(vue)鎖屏功能實現(xiàn),解決刷新會退出鎖屏、重新進入顯示鎖屏頁面等問題
背景
設(shè)計一個鎖屏頁面供用戶離開(手動點擊)或者長時間未操作時使用
場景
- 用戶一段時間沒有操作或者手動點擊鎖屏按鈕時,顯示鎖屏頁面
- 在鎖屏頁面強制刷新時,需保持在顯示鎖屏頁面或者跳轉(zhuǎn)到登錄頁面(通過判斷token是否有效決定是否跳轉(zhuǎn)到登錄)
- 如果當(dāng)前窗口已經(jīng)鎖屏,重開一個該地址的新窗口;或者關(guān)閉當(dāng)前窗口時是鎖屏狀態(tài),重新打開窗口時,新窗口直接跳到登錄頁面。
實現(xiàn)
- 鎖屏頁面的實現(xiàn)
鎖屏頁面通過fixed布局的方式覆蓋整個窗口(也可以通過路由的方式,但是路由的方式不方便解鎖后的路由返回),(setIsLock方法在下文)
LockPage組件
<template> <div class="lock-container"> 自定義鎖屏內(nèi)容 </div> </template> <script> import { mapGetters, mapActions } from 'vuex' export default { name: 'Lock', data () { return { } }, computed: { }, methods: { ...mapActions({ // 設(shè)置是否鎖屏 setIsLock: 'systemLock/setIsLock' }), handleLogin () { // 省略解鎖邏輯 this.setIsLock(false) } }, components: {} } </script> <style lang="scss" scoped> .lock-container { z-index: 1001; width: 100%; height: 100vh; display: flex; align-items: center; justify-content: center; position: absolute; } </style>
2.APP.vue中使用鎖屏組件
app.vue中只包含簡單的鎖屏組件和路由視圖,通過isLock和是否登錄(islogon)判斷鎖屏是否顯示。
3.isLock的設(shè)置
使用的vuex的狀態(tài)管理來存儲isLock
點擊鎖屏按鈕、設(shè)定時間內(nèi)未操作(鼠標(biāo)未移動)則設(shè)置isLock=true,當(dāng)isLock為false,且設(shè)定時間內(nèi)為設(shè)置isLock則認為是未操作設(shè)置其為true
vuex設(shè)置isLock的方法
setIsLock ({ commit, state }, isLock) { commit('SET_ISLOCK', isLock) if (!isLock) { clearTimeout(timeOut) //設(shè)置時間內(nèi)未操作,則設(shè)置鎖屏狀態(tài)為true timeOut = setTimeout(() => { commit('SET_ISLOCK', true) }, 1000 * 30 * 60) } }
APP.vue中監(jiān)聽鼠標(biāo)移動,移動則讓isLock為false(vuex重新計時)
// 在登錄后且未鎖屏狀態(tài)監(jiān)聽用戶是否操作(鼠標(biāo)移動) wacth:{ isLock: { handler: function (newVal, oldVal) { if (this.isLock) { window.removeEventListener('mousemove', this.mousemove) } else if (this.islogon) { // 未鎖屏且已經(jīng)登錄的情況監(jiān)聽用戶操作 window.addEventListener('mousemove', this.mousemove) } }, immediate: false }, }, methods: { ...mapActions({ setIsLock: 'systemLock/setIsLock', }), mousemove () { _.throttle(() => { this.setIsLock(false) }, 1000 * 60, { leading: true, trailing: false }) } },
問題1:鎖屏?xí)r刷新頁面,vuex的內(nèi)容被初始化,從而退出鎖屏頁面,不滿足需求
解決:同時使用localStorage來存儲isLock
1.用localSotrage來初始化vuex
2.isLock變化時同時設(shè)置localStorage
跳轉(zhuǎn)isLock的watch方法調(diào)整:
watch:{ isLock: { handler: function (newVal, oldVal) { if (this.isLock) { window.removeEventListener('mousemove', this.mousemove) localStorage.setItem('isLock', 'isLock') } else { localStorage.removeItem('isLock') if (this.islogon) { window.addEventListener('mousemove', this.mousemove) } } }, immediate: false }, }
問題2:如果使用localStorage來初始化vuex,若在鎖屏?xí)r關(guān)閉窗口,后面再打開網(wǎng)站時會直接進入鎖屏頁面,不合理,應(yīng)該直接到登錄頁面
解決:
使用sessionStorage來判斷當(dāng)前的操作是第一次進入系統(tǒng)還是刷新系統(tǒng)(sessionStorage只在當(dāng)前窗口有效)。第一次進入系統(tǒng)時如果localStorage的isLock=‘isLock’則不要用localStorage初始化vuex,直接跳轉(zhuǎn)到登錄頁面;若為刷新操作,則用localSotrage來初始化vuex。
1.vuex中isLock初始化值設(shè)置為false
2.在sessionStorage設(shè)置initialized字段代表是否已經(jīng)初始化。在APP.vue的created中手動初始化vuex的isLock
created () { if (localStorage.getItem('isLock') === 'isLock') { // 刷新時(非首次進入),如果是鎖屏狀態(tài),則繼續(xù)顯示鎖屏頁面 if (sessionStorage.getItem('initialized') === 'initialized') { this.setIsLock(true) } else { this.$router.push('/login') localStorage.removeItem('isLock') } } }
附:
vuex完整代碼:
let timeOut = null const _state = { lockTime: 1000 * 30 * 60, isShowLock: false, isLock: false } const getters = { lockTime: state => state.lockTime, isShowLock: state => state.isShowLock, isLock: state => state.isLock } const mutations = { SET_ISLOCK: (state, isLock) => { state.isLock = isLock }, SET_LOCKTIME: (state, lockTime) => { state.lockTime = lockTime }, SET_ISSHOWLOCK: (state, isShowLock) => { state.isShowLock = isShowLock } } const actions = { setLockTime ({ commit }, lockTime) { commit('SET_LOCKTIME', lockTime * 1000 * 60) }, setIsShowLock ({ commit }, isShowLock) { commit('SET_ISSHOWLOCK', isShowLock) }, setIsLock ({ commit, state }, isLock) { commit('SET_ISLOCK', isLock) if (!isLock) { clearTimeout(timeOut) timeOut = setTimeout(() => { commit('SET_ISLOCK', true) }, state.lockTime) } } } export default { namespaced: true, getters, state: _state, mutations, actions }
到此這篇關(guān)于element-vue實現(xiàn)網(wǎng)頁鎖屏功能的文章就介紹到這了,更多相關(guān)element-vue網(wǎng)頁鎖屏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中使用video.js實現(xiàn)截圖和視頻錄制與下載
這篇文章主要為大家詳細介紹了Vue中如何使用video.js實現(xiàn)截圖和視頻錄制與下載,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Vue+TailWindcss實現(xiàn)一個簡單的闖關(guān)小游戲
本文將利用Vue+TailWindcss實現(xiàn)一個簡單的闖關(guān)小游戲,玩家須躲避敵人與陷阱到達終點且擁有多個關(guān)卡,感興趣的小伙伴可以了解一下2022-04-04vue 權(quán)限認證token的實現(xiàn)方法
這篇文章主要介紹了vue 權(quán)限認證token的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Vue實現(xiàn)實時更新sessionStorage數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細介紹了Vue如何實現(xiàn)實時更新sessionStorage數(shù)據(jù),文中的示例代碼講解詳細,具有一定的參考價值,需要的可以參考一下2023-06-06快速解決Vue、element-ui的resetFields()方法重置表單無效的問題
這篇文章主要介紹了快速解決Vue、element-ui的resetFields()方法重置表單無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Vue項目總結(jié)之webpack常規(guī)打包優(yōu)化方案
這篇文章主要介紹了vue項目總結(jié)之webpack常規(guī)打包優(yōu)化方案,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06