vue3 全局--输入口令(密码)校验通过后---再执行

文章介绍了如何在ElementPlus中创建自定义的消息提示组件,包括成功提示、确认对话框、密码验证以及与接口调用的结合,展示了如何在Vue应用中使用这些功能进行用户交互。
摘要由CSDN通过智能技术生成

hook

import request from '@/config/axios'
import { ElInput, ElMessage, ElMessageBox, ElNotification } from 'element-plus'
import { useI18n } from './useI18n'
export const useMessage = () => {
  const { t } = useI18n()
  return {
    // 消息提示
    info(content: string) {
      ElMessage.info(content)
    },
    // 成功提示
    alertSuccess(content: string) {
      ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'success' })
    },
    // 成功通知
    notifySuccess(content: string) {
      ElNotification.success(content)
    },
    // 确认窗体
    confirm(content: string, tip?: string) {
      return ElMessageBox.confirm(content, tip ? tip : t('common.confirmTitle'), {
        confirmButtonText: t('common.ok'),
        cancelButtonText: t('common.cancel'),
        type: 'warning'
      })
    },
    // 删除窗体
    delConfirm(content?: string, tip?: string) {
      return ElMessageBox.confirm(
        content ? content : t('common.delMessage'),
        tip ? tip : t('common.confirmTitle'),
        {
          confirmButtonText: t('common.ok'),
          cancelButtonText: t('common.cancel'),
          type: 'warning'
        }
      )
    },
    // 导出窗体
    exportConfirm(content?: string, tip?: string) {
      return ElMessageBox.confirm(
        content ? content : t('common.exportMessage'),
        tip ? tip : t('common.confirmTitle'),
        {
          confirmButtonText: t('common.ok'),
          cancelButtonText: t('common.cancel'),
          type: 'warning'
        }
      )
    },
    // 提交内容
    prompt(content: string, tip: string) {
      return ElMessageBox.prompt(content, tip, {
        confirmButtonText: t('common.ok'),
        cancelButtonText: t('common.cancel'),
        type: 'warning'
      })
    },
    // 执行密码校验------------全局校验执行
    runPassword() {
      return ElMessageBox({
        title: '请输入执行密码:',
        closeOnClickModal: false,
        autofocus: false,
        showCancelButton: true,
        // Should pass a function if VNode contains dynamic props
        message: () =>
          h('div', {}, [
            // h('div', { class: ['mb', 'c-green'] }, '请输入执行密码:'),
            h(ElInput, {
              // h('input', {
              modelValue: pwd.value,
              // type: 'password',
              placeholder: '输入密码',
              // showPassword: true,
              'onUpdate:modelValue': (val: boolean | string | number) => {
                console.log('val=', val)
                pwd.value = val
              }
            })
          ]),
        beforeClose: async (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = 'Loading...'
            const bool = await pass()
            if (bool) {
              pwd.value = undefined
              done()
              setTimeout(() => {
                instance.confirmButtonLoading = false
              }, 300)
            }
          } else {
            pwd.value = undefined
            done()
          }
        }
      })
    }
  }
}
const pwd = ref(undefined)
const params = reactive({
  pcsDeviceId: 4973,
  bootControl: 1
})
// 登录
export const login = (data) => {
  return request.post({ url: '/ems-monitor/pcs/control', data })
}
export const pass = async () => {
  if (pwd.value === 'hww') {
    // 如果调用接口就放开下一行注释
    // await login(params)
    return true
  } else {
    return false
  }
}

具体使用:

const message = useMessage() // 消息弹窗

const runPassword = async () => {
  await message.runPassword()
  console.log('继续执行后续操作!')
}

示例:

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值