防止用户复制管理员网址进入页面、设置禁用隐藏按钮权限以及防止修改禁用按钮权限

简介: 防止用户复制管理员网址进入页面、设置禁用隐藏按钮权限以及防止修改禁用按钮权限

防止用户复制管理员网址进入页面

Login.vue页面

<script>
import {initDynaminRoutes} from '../router/index.js';
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "admin",
        password: "admin",
      },
      isloading: false,
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    login() {
      this.isloading = true;
      this.$api
        .login({
          username: this.form.username,
          password: this.form.password,
        })
        .then((res) => {
          console.log(res);
          this.$store.commit("setUser", res.data);
          window.sessionStorage.setItem("token", res.data.token);
          this.isloading = false;
          this.$router.push("/");
          // 调用方法
          initDynaminRoutes()
          this.$message({
            message: "恭喜你,登录成功",
            type: "success",
          });
        });
    },
  },
};
</script>

router页面

import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import NotFound from "../views/NotFound.vue";
import store from "../store/index.js";
Vue.use(VueRouter);
const one = {
  path: "/menu/one",
  component: () => import("@/views/Page1.vue"),
};
const two = {
  path: "/menu/two",
  component: () => import("@/views/Page1.vue"),
};
const three = {
  path: "/menu/three",
  component: () => import("@/views/Page1.vue"),
};
const four = {
  path: "/menu/four",
  component: () => import("@/views/Page1.vue"),
};
const five = {
  path: "/menu/five",
  component: () => import("@/views/Page1.vue"),
};
// 这里的ruleMapping
const ruleMapping = {
  "/menu/one": one,
  "/menu/two": two,
  "/menu/three": three,
  "/menu/four": four,
  "/menu/five": five,
};
console.log(ruleMapping);
const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
    redirect: "/menu/one",
    children: [],
  },
  {
    path: "/about",
    name: "About",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Login.vue"),
  },
  {
    path: "*",
    name: "NotFound",
    component: NotFound,
  },
];
const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next();
  } else {
    let token = window.sessionStorage.getItem("token");
    if (!token) {
      next("/login");
    } else {
      next();
    }
  }
});
// 动态导出路由
export function initDynaminRoutes() {
  /* console.log(store);
  console.log(router); */
  const rulerouter = router.options.routes;
  console.log(rulerouter);
  console.log(rulerouter[0].children);
  const rightlist = store.state.user.rights;
  console.log(rightlist);
  rightlist.forEach((v) => {
    // console.log(v);
   if (v.children) {
    v.children.forEach(item => {
/*       console.log(item);
      console.log(item.path); */
      if (item.path) {
        // 这里的ruleMapping就是上面创建数组的名称
        const temp = ruleMapping[item.path]
        console.log(temp);
        rulerouter[0].children.push(temp)
        console.log( rulerouter[0].children);
        rulerouter[0].children.forEach(v=>{
          router.addRoute('Home',v)
        })
      }
    })
   }
  })
  router.addRoutes(rulerouter)
}
const originalPush = VueRouter.prototype.push;
// 重写了原型上的push方法,统一的处理了错误信息
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
export default router;

设置禁用隐藏按钮权限

<el-button type="primary" @click="$emit('show')" v-permission="{action:'add',effect:'disabled'}">添加商品</el-button>

b34654466d634eea8390837129aed0ef.png

import Vue from 'vue'
import router from '@/router/index.js'
Vue.directive('permission',{
  inserted:function(el,binding){
  console.log(el);
  console.log(binding.value.action);
  var action = binding.value.action
  var effect = binding.value.effect
  console.log(router.currentRoute.meta);
  if(router.currentRoute.meta.indexOf(action) === -1){
    if(effect === "disabled"){
    el.disabled = true
    el.classList.add('is-disabled')
    }else {
    el.parentNode.removeChild(el)
    }
  }
  }
}) 

f58629d1e8154c93afbbcea263f5a128.png

0dbad151c8a64f198c3bff432a006414.png 一定要刷新一次页面!!!

371cc25bd78a43b8a0fc5f503220f8a7.png

防止修改禁用按钮权限

/* resful语法 */
const map = {
  'get':'view',
  'post':'add',
  'put':'edit',
  'delete':'delete'
}
// 添加请求拦截器
axios.interceptors.request.use(function(config) {
  // 在发送请求之前做些什么
  console.log(config);
  var method = map[config.method]
  var currentRoute = router.currentRoute.meta
  console.log(currentRoute);
  if(config.url != '/login'){
  if(currentRoute && currentRoute.indexOf(method) === -1){
    alert('您没有相关权限!')
    return new Promise(function(resolve,reject){
    reject('请求错误')
    })
  }
  }
  return config;
});

a5243b33829446d7aa1d70d20353df05.pngf5a556b1438b46e6936bc4a6601e53ec.png

相关文章
|
5月前
|
Shell Linux 开发工具
哇~真的是你呀!今天是用户操作中的修改属性、密码设置、删除
在Linux系统中,修改属性、密码设置和删除用户都是管理用户和文件系统的常见操作,下面让我们一起来看看。
47 1
|
5月前
|
JSON 数据格式
VSCode无法写入用户设置 请打开用户设置并清除错误或警告, 然后重式
VSCode无法写入用户设置 请打开用户设置并清除错误或警告, 然后重式
|
10月前
|
数据安全/隐私保护
fastadmin是如何设置没有权限的用户不能访问某些页面的?
fastadmin是如何设置没有权限的用户不能访问某些页面的?
257 0
|
Java 数据安全/隐私保护
登陆页面实现保存帐号密码功能
本文主要讲如何登陆页面实现保存帐号密码功能
412 0
登陆页面实现保存帐号密码功能
|
4月前
|
开发者
在用户关闭页面时,提示用户有内容未保存-论onbeforeunload事件的用法
在用户关闭页面时,提示用户有内容未保存-论onbeforeunload事件的用法
25 0
无插件解除网页禁用右键的限制
无插件解除网页禁用右键的限制
119 0
|
安全 数据安全/隐私保护
Confluence 6 为登录失败编辑,禁用和配置验证码
在默认的情况下,验证码将会在失败登录次数达到的时候显示。 如果为登录失败编辑,禁用和配置验证码: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接。
1338 0
Confluence 6 禁用管理员联系表单
如果你希望禁用这个功能,不允许用户通过发送电子邮件的方式联系站点管理员。你可以禁用这个页面中有关输入用户信息发送电子邮件的部分。
846 0
Confluence 6 删除或禁用用户
你可以 删除(delete)没有在你 Confluence 站点中添加或者编辑过内容的用户。这些内容包括页面,博客内容和编辑已经存在页面的跟帖,意见等。
1212 0