Vue一个指令实现用户登录密码校验

效果演示:

使用场景:当删除或者修改等操作需要输入用户登录密码或者其他校验信息时使用。

Vue版本:Vue2,

组件库使用element-ui

指令代码:

javaScript 复制代码
/**
 * 身份验证
 */
import {
  MessageBox,
  Message,
} from 'element-ui'

// 密码验证接口
import {
  authentication
} from '@/api/login';

export default {
  bind: function (el, binding) {
    // 获取传递进来的参数
    const interceptor = binding.value || {}

    el._custclick = async (event) => {
      let result = await authenticationHttp();
      if (result) {
        if (interceptor.data) {
          interceptor.handler && interceptor.handler(interceptor.data)
        } else {
          interceptor.handler && interceptor.handler(event)
        }
      }
    }

    el.addEventListener('click', el._custclick)
  },
  unbind: function (el, binding) {
    el.removeEventListener('click', el._custclick);
    delete el._custclick;
  }
};

function authenticationHttp() {
  return new Promise((resolve, reject) => {
    MessageBox.prompt('请输入管理员密码', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        modal: false,
        inputType: "password",
        cancelButtonText: '取消',
        inputPattern: /^.+$/,
        inputErrorMessage: '管理员密码不能为空',
        customClass: "authentication-message-box",
        beforeClose: (action, instance, done) => {
          if (action !== 'confirm') {
            done();
          } else {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = '验证中...';

            setTimeout(() => {
              authentication({
                password: instance._data.inputValue
              }).then(res => {
                if (!res.data) {
                  Message({
                    message: "管理员密码不正确!",
                    type: 'error'
                  })
                } else {
                  instance._data.inputValue = "true"
                  done();
                }
              }).finally(() => {
                instance.confirmButtonLoading = false;
                instance.confirmButtonText = '确定';
              })
            }, 1500);

          }
        }
      })
      .then(({
        value
      }) => {
        resolve(value === "true" ? true : false);
      }).catch(() => {
        resolve(false);
      });
  });
}

使用

不需要传递参数:

html 复制代码
 <el-button type="danger" plain icon="el-icon-delete" size="mini" 
          v-authentication="{ handler: handleDelete }">删除</el-button>

需要传递参数: data: scope.row就是传递的参数

html 复制代码
<el-button size="mini" type="text" icon="el-icon-delete"
  v-authentication="{data: scope.row, handler: handleDelete }">删除</el-button>
js 复制代码
   /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;

     this.$modal
            .confirmHtml(
              '是否确认删除id为' + ids +'的数据项?'
            )
            .then(function () {
              return delEvidence(ids);
            })
            .then(() => {
              this.getList();
              this.$modal.msgSuccess("删除成功");
            })
            .catch(() => {});
    },
相关推荐
Java后端的Ai之路3 小时前
06-React-Props
前端·react.js·前端框架
Flynt3 小时前
开源"间谍卫星"说数据全是真的,我把它的免key数据源挨个试了一遍
前端·开源·数据可视化
Csvn3 小时前
渲染管线
前端
程序员大阳4 小时前
副队长CSS教程(33)–圆角与透明
前端·css·透明·圆角
志尊宝4 小时前
Vue3 零基础每日笔记(082):文件上传实战——el-upload 校验、自定义上传、头像预览
前端·javascript·vue.js·笔记·#javascript·#vue.js
鬼手点金4 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
柳杉4 小时前
借助 Codex 和可视化工作流,从调研到完整实现,3D 大屏竟然可以这么简单
前端·ai编程·数据可视化
IMPYLH5 小时前
HTML 的 <wbr> 元素
前端·html
Dovis(誓平步青云)5 小时前
常用命令藏得太深?怎么做一个快捷命令坞
运维·前端·自动化·音视频·prometheus
IT_陈寒6 小时前
Vite的这个问题坑了我三天,原来如此解决!
前端·人工智能·后端