1.0.0 • Published 10 months ago

secure-action v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
10 months ago

Secure Action

Secure Action 是一个基于行为的验证码组件,适用于 Vue 3。

示例

pC4cgnf.png pC4c6jP.png pC4cs1I.png

特性

  • 生成基于行为的随机验证码,用于验证用户交互。
  • 在 Vue 3 项目中轻松集成。
  • 定制主题。

安装

npm install secure-action

使用

<template>
  <SecureAction
    ref="secureActionRef"
    :request="behavioralCaptcha"
    :verify="handleVerify"
  ></SecureAction>
  <button @click="withInterceptor">登录</button>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import {
  SecureAction,
  type SecureActionInstance,
  type VerifyOptions,
  type RequestFnOptions
} from 'secure-action'
import { useBehavioralCaptcha } from 'secure-action'
const secureActionRef = ref<SecureActionInstance>()

const handleSignIn = () => {
  console.log('触发登录')
}

/**
 * useBehavioralCaptcha 辅助验证函数 当验证通过且自动关闭后 触发传入的方法
 *
 * export declare function useBehavioralCaptcha(
 *            instance: Ref<SecureActionInstance | undefined>,
 *            fn: Function): {
 *     withInterceptor: () => Promise<void>;
 * };
 */
const { withInterceptor } = useBehavioralCaptcha(secureActionRef, handleSignIn)

const baseUrl = 'http://127.0.0.1/v1/behavioral_captcha'
const behavioralCaptchaKey = ref('')

// 生成行为验证码
const behavioralCaptcha = ({ loading, onSuccess, onError }: RequestFnOptions) => {
  loading.value = true
  fetch(`${baseUrl}?account=Fady Mak&scene=1`)
    .then((res) => res.json())
    .then((res) => {
      if (String(res.code).endsWith('6200')) {
        const { image, thumbnail, key } = res.data
        onSuccess(image, thumbnail)
        behavioralCaptchaKey.value = key
      } else {
        onError(res.message)
      }
    })
    .finally(() => {
      loading.value = false
    })
}

// 验证验证码
const handleVerify = ({ onError, onSuccess, points }: VerifyOptions) => {
  fetch(`${baseUrl}/verify`, {
    method: 'post',
    body: JSON.stringify({
      account: 'Fady Mak',
      scene: 1,
      dots: String(points),
      key: behavioralCaptchaKey.value
    }),
    headers: {
      'Content-Type': 'application/json'
    }
  })
    .then((res) => res.json())
    .then((res) => {
      if (String(res.code).endsWith('6200')) {
        onSuccess()
      } else {
        onError(res.message)
      }
    })
}
</script>

Props

  • Secure Action 组件支持以下 props
属性名类型默认值必需描述
requestFunction-true请求函数,用于获取验证码图片和缩略图
verifyFunction-true验证函数,用于验证用户的操作是否正确
titleString图形验证false标题
loadingTextString加载中...false加载中的文本提示
validatingTextString验证中,请稍等...false验证中的文本提示
successTextString验证成功false验证成功的文本提示
failTextString验证失败,请按提示重新操作false验证失败的文本提示
flatBooleantruefalse是否将 points 属性扁平化成一维数组
errorRefreshWaitNumber2000false验证错误时刷新间隔时间(毫秒)
successCloseWaitNumber2000false验证成功时关闭间隔时间(毫秒)
maskClosableBooleantruefalse点击遮罩层是否可以关闭对话框
hideCancelBooleanfalsefalse是否隐藏取消按钮
closableBooleantruefalse是否显示关闭按钮
keyboardBooleantruefalse是否支持按下键盘的 ESC 键关闭对话框
okTextString确认false确认按钮的文本
cancelTextString取消false取消按钮的文本
maskClassString-false遮罩层的类名
classString-false容器的类名
maxPointNumber9false验证点的最大数量
rNumber10false检测的半径(某个点在此半径内是否有点)
fallbackBgString#f5f5f5false图片加载失败时的背景色
fallbackTextString加载失败false图片加载失败时的提示语
footerAlign'start' |center| 'end''end'false底部按钮对齐位置
  • types
export type RequestFnOptions = {
  loading: Ref<boolean>
  onSuccess: (image: string, thumbnail: string) => void
  onError: (message?: string) => void
}
export type RequestFn = (options: RequestFnOptions) => void

export type VerifyOptions = {
  loading: Ref<boolean>
  points: number[] | [number, number][]
  onSuccess: (message?: string) => void
  onError: (message?: string) => void
}
export type VerifyFn = (options: VerifyOptions) => void

Slots

插槽名描述参数
title标题-
loading加载中-
fallback加载失败图片-
status状态data(结构如下)
  • status data 类型
 interface StatusData {
   status: '' | 'SUCCESS' | 'ERROR';
   statusText: string
 }

事件

Secure Action 组件触发以下事件:

事件名描述参数
verify验证-

CSS 变量

CSS 变量默认值描述
--as-color#1d2129主文字颜色
--as-bg-color#ffffff背景颜色
--as-radius8px圆角半径
--as-font-size14px字体大小
--as-color-primary#164cff主要颜色
--as-color-danger#f53f3f危险颜色
--as-color-success#00b42a成功颜色
--as-border1px solid #edeef0边框样式
--as-header-height48px头部高度
--as-header-paddig0 16px头部内边距
--as-body-paddig24px 32px 32px内容区域内边距
--as-thumbnail-wrap-margin0 0 16px缩略图容器外边距
--as-footer-height56px底部高度
--as-footer-paddig0 16px底部内边距
--as-button-radius4px按钮圆角半径
--as-button-gap-x16px按钮水平间距
--as-button-border-width1px按钮边框宽度
--as-button-border-stylesolid按钮边框样式
--as-button-padding-y7px按钮垂直内边距
--as-button-padding-x15px按钮水平内边距
--as-button-icon-padding7px按钮图标内边距
--as-button-icon-image-size16px按钮图标尺寸
--as-button-border-color-default#dbdde2按钮默认边框颜色
--as-point-size20px验证点的尺寸
--as-point-font-weight500验证点的字体粗细
--as-point-border1px solid #f3f6ff验证点的边框样式

许可证

本项目基于 MIT 许可证