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(() => {});
    },
相关推荐
前端Hardy17 小时前
别再忽略 Promise 拒绝了!你的 Node.js 服务正在“静默自杀”
前端·javascript·面试
前端Hardy17 小时前
别再被setTimeout闭包坑了!90% 的人都写错过这个经典循环
前端·javascript·vue.js
小林coding17 小时前
专为程序员打造的简历模版来啦!覆盖前端、后端、测开、大模型等专业简历
前端·后端
前端Hardy17 小时前
你的 Vue 组件正在偷偷吃掉内存!5 个常见的内存泄漏陷阱与修复方案
前端·javascript·面试
RaidenLiu17 小时前
Flutter Platform Channel 底层架构解析 —— 从 BinaryMessenger 到跨平台消息通信机制
前端·flutter·前端框架
bluceli18 小时前
CSS容器查询:响应式设计的新范式
前端·css
Tapir18 小时前
被 Karpathy 下场推荐的 NanoClaw 是什么来头
前端·后端·github
前端人类学18 小时前
深入解析JavaScript中的null与undefined:区别、用法及判断技巧
前端·javascript
ssshooter19 小时前
Tauri 项目实践:客户端与 Web 端的授权登录实现方案
前端·后端·rust
兆子龙19 小时前
【React】19 深度解析:掌握新一代 React 特性
前端·架构