vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > element-vue网页锁屏

element-vue实现网页锁屏功能(示例代码)

作者:YJ_Root

这篇文章主要介绍了element-vue实现网页锁屏功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

element-vue实现网页锁屏功能

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.里面需要结合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)锁屏功能实现,解决刷新会退出锁屏、重新进入显示锁屏页面等问题

背景

设计一个锁屏页面供用户离开(手动点击)或者长时间未操作时使用

场景

实现

LockPage组件

<template>
  <div class="lock-container">
    自定义锁屏内容
  </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'Lock',
  data () {
    return {
    }
  },
  computed: {
  },
  methods: {
    ...mapActions({
      // 设置是否锁屏
      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的设置
使用的vuex的状态管理来存储isLock
点击锁屏按钮、设定时间内未操作(鼠标未移动)则设置isLock=true,当isLock为false,且设定时间内为设置isLock则认为是未操作设置其为true

vuex设置isLock的方法

setIsLock ({ commit, state }, isLock) {
    commit('SET_ISLOCK', isLock)
    if (!isLock) {
      clearTimeout(timeOut)
      //设置时间内未操作,则设置锁屏状态为true
      timeOut = setTimeout(() => {
        commit('SET_ISLOCK', true)
      }, 1000 * 30 * 60)
    }
  }

APP.vue中监听鼠标移动,移动则让isLock为false(vuex重新计时)

 // 在登录后且未锁屏状态监听用户是否操作(鼠标移动)
 wacth:{
	isLock: {
      handler: function (newVal, oldVal) {
        if (this.isLock) {
          window.removeEventListener('mousemove', this.mousemove)
        } else if (this.islogon) {
          // 未锁屏且已经登录的情况监听用户操作
          window.addEventListener('mousemove', this.mousemove)
        }
      },
      immediate: false
    },
 },
 methods: {
    ...mapActions({
      setIsLock: 'systemLock/setIsLock',
    }),
    mousemove () {
      _.throttle(() => {
        this.setIsLock(false)
      }, 1000 * 60, {
        leading: true,
        trailing: false
      })
    }
  },

问题1:锁屏时刷新页面,vuex的内容被初始化,从而退出锁屏页面,不满足需求
解决:同时使用localStorage来存储isLock

1.用localSotrage来初始化vuex

在这里插入图片描述

2.isLock变化时同时设置localStorage
跳转isLock的watch方法调整:

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,若在锁屏时关闭窗口,后面再打开网站时会直接进入锁屏页面,不合理,应该直接到登录页面
解决
使用sessionStorage来判断当前的操作是第一次进入系统还是刷新系统(sessionStorage只在当前窗口有效)。第一次进入系统时如果localStorage的isLock=‘isLock’则不要用localStorage初始化vuex,直接跳转到登录页面;若为刷新操作,则用localSotrage来初始化vuex。

1.vuex中isLock初始化值设置为false

在这里插入图片描述

2.在sessionStorage设置initialized字段代表是否已经初始化。在APP.vue的created中手动初始化vuex的isLock

 created () {
    if (localStorage.getItem('isLock') === 'isLock') {
      // 刷新时(非首次进入),如果是锁屏状态,则继续显示锁屏页面
      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
}

到此这篇关于element-vue实现网页锁屏功能的文章就介绍到这了,更多相关element-vue网页锁屏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文