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(() => {});
    },
相关推荐
EndingCoder1 分钟前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux35289 分钟前
Web集群管理实战指南:从架构到运维
运维·前端·架构
沛沛老爹16 分钟前
Web转AI架构篇 Agent Skills vs MCP:工具箱与标准接口的本质区别
java·开发语言·前端·人工智能·架构·企业开发
小光学长1 小时前
基于Web的长江游轮公共服务系统j225o57w(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库
Joe5562 小时前
vue2 + antDesign 下拉框限制只能选择2个
服务器·前端·javascript
ChangYan.2 小时前
monorepo 多包管理识别不到新增模块,解决办法
前端·chrome
Jinuss2 小时前
React元素创建介绍
前端·react.js
济6173 小时前
linux 系统移植(第六期)--Uboot移植(5)--bootcmd 和 bootargs 环境变量-- Ubuntu20.04
java·前端·javascript
m0_748254663 小时前
AJAX 基础实例
前端·ajax·okhttp
vmiao3 小时前
【前端入门】商品页放大镜效果(仅放大镜随鼠标移动效果)
前端