vue做一个锁屏禁止页面前进后退

1.在router.beforeEach()路由首首位加上以下代码

javascript 复制代码
	 /**
	 * 判断锁屏
	 */
	 //登录的时候存的md5加密的密码
  let oldPasswordld = localStorage.getItem("lockPassword");
  //锁屏页面的md5加密密码
  let newlockPassword = localStorage.getItem("newlockPassword");
  console.log(oldPasswordld,newlockPassword)
	if (newlockPassword !== oldPasswordld && to.path !== '/screen') {
    next('/screen')
	}

2.书写锁屏页面和相关路由

下面代码为screen/index.js 为锁屏的页面 首先进入这个页面 默认执行一次 unlock方法里面的localStorage.setItem("newlockPassword", md5(this.userForm.newPw));

把解锁的密码存到本都对象存储里面,这样路由就好做处理。

javascript 复制代码
	<template>
  <div class="app">
    <el-form class="userInfo">
      <div class="body-icon">
      </div>
      <div class="title-icon">
      </div>
      <div class="box">
        <img src="../../assets/logo/logo.png" class="lock-avatar" />
      </div>
      <el-form-item>
        <el-row style="margin-left: 100px">
          <el-col :span="2">
          </el-col>
          <el-col :span="12" class="lock-nickName">{{ nickName }}</el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="userForm.newPw"
          placeholder="请输入登陆密码"
          type="password"
          auto-complete="off"
          @keyup.enter.native="unLock()"
          show-password
        >
          <div slot="prefix" style="margin-left: 3px">
            <i class="el-icon-lock"></i></div
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div style="text-align: center; color: #1890ff">
          <a @click="logout">退屏重新登录</a>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click="unLock"
          ><i class="el-icon-unlock"></i>解锁</el-button
        >
        <!-- <el-button
          circle
          type="primary"
          plain
          icon="el-icon-unlock"
          @click="unLock"
        ></el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import md5 from "js-md5";
export default {
  data() {
    return {
      userForm: {
        newPw: "",
        user: "",
      },
      loading: false,
    };
  },
  methods: {
    unLock() {
      let oldAuct = localStorage.getItem("lockPassword");
      localStorage.setItem("newlockPassword", md5(this.userForm.newPw));
      console.log(oldAuct, localStorage.getItem("newlockPassword"), "999990");
      if (this.userForm.newPw === "" || this.userForm.newPw === undefined) {
        return;
      } else if (md5(this.userForm.newPw) != oldAuct) {
        this.userForm.newPw = "";
        this.$notify.error({
          title: "错误",
          message: "解锁密码错误,请输入登陆密码解锁",
          duration: 1500,
        });
        return;
      } else {
        setTimeout(() => {
          this.$notify.success({
            title: "解锁成功",
            duration: 1500,
          });
          this.$router.push("/index");
          this.userForm.newPw = "";
        }, 500);
      }
    },
    async logout() {
      this.$confirm("确定注销并退出系统吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let password = localStorage.getItem("lockPassword");
        localStorage.setItem("newlockPassword", password);
        this.$store.dispatch("LogOut").then(() => {
          location.href = "/login";
        });
      });
    },
  },
  mounted() {
    this.unLock();
  },
};
</script>

<style lang="scss" scoped>
.app {
  // background-image: url("../../assets/images/back.png");
  background-size: 100%; // 背景图片大小最大
  height: 100%; //宽、高也最大
  width: 100%;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  background-color: skyblue; //一定要设置背景颜色
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1500;
  .userInfo {
    // display: flex;
    background: #ffffff;
    // height: 300px;
    width: 400px;
    padding: 25px 25px 5px 25px;
    .title-icon {
      width: 120px;
      height: 20px;
      margin-bottom: 22px;
    }
    .body-icon {
      width: 500px;
      height: 120px;
      position: absolute;
      margin-left: -152px;
      margin-top: -166px;
    }
    .box {
      display: flex;
      justify-content: center;
      align-items: center;
      .lock-avatar {
        width: 100px;
        height: 100px;
        border-radius: 100px;
      
      }
    }

    .lock-nickName {
      margin-top: -2px;
      font-size: 14px;
      font-weight: 560;
      text-align: center;
    }
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
  }
}
</style>
相关推荐
用户9385156350722 分钟前
从零搭建 AI 日记助手:用 Milvus 向量数据库 + RAG 让机器读懂你的每一天
javascript·人工智能·全栈
徐小夕1 小时前
花了一周,3亿tokens,我开源了一款 Word 文档智能审查平台,文稿自动质检+可视化分析,告别低效人工审核
前端·算法·github
a1117762 小时前
坦克大战3D Three.js 3D (开源项目)
开发语言·javascript·3d
kyriewen3 小时前
别再乱用useEffect了——你写的10个里有8个不该存在
前端·javascript·react.js
Ivanqhz3 小时前
Rust &‘static str浅析
java·前端·javascript·rust
IT_陈寒3 小时前
SpringBoot这个分页坑,我踩了三天才爬出来
前端·人工智能·后端
颜酱3 小时前
05 | 召回前置准备:根据业务数据库生成各数据库(读取配置阶段)
前端·人工智能·后端
zandy10114 小时前
衡石 Agentic BI的ReAct 推理框架在 Agentic BI 中的工程化实践
前端·javascript·react.js
罗超驿4 小时前
JavaEE进阶之路:从Web架构原理到HTML标签全解析
前端·html·web·javaee
西安小哥4 小时前
从前端到AI工程师:一场跨越鸿沟的真实蜕变之旅
前端