tedsign vue3 web-端框架中封装一个验证码组件 以及对应node 接口逻辑说明

一个这样的组件

我直接上代码了

复制代码
<template>
  <t-loading size="small" :loading="loading" show-overlay>
    <div class="container" @click="refresh">
      <div v-if="svg" class="svg" v-html="svg" />
      <img v-else class="base64" :src="base64" alt="" />
    </div>
  </t-loading>
</template>
<script lang="ts" setup>
import { MessagePlugin } from 'tdesign-vue-next';
import { onMounted, ref } from 'vue';

import { getCaptcha } from '@/api/login';

const emit = defineEmits(['update:modelValue', 'change']);
// base64
const base64 = ref('');
// svg2
const svg = ref('');
const loading = ref(false)

async function refresh() {
  loading.value = true
    await getCaptcha({ height: 40,
      width: 150,
    })
    .then(({ captchaId, data }) => {
      if (data.includes(';base64,')) {
        base64.value = data;
      } else {
        svg.value = data;
      }
      emit('update:modelValue', captchaId);
      emit('change', {
        base64,
        svg,
        captchaId,
      });
    })
    .catch((err) => {
      MessagePlugin.error(err.message);
    }).finally(()=>{
      loading.value = false
    });
}

onMounted(() => {
  refresh();
});

defineExpose({
  refresh,
});
</script>
<style lang="less" scoped>
.container {
  height: 40px;
  width: 150px;
  cursor: pointer;
  .svg {
    height: 100%;
    position: relative;
  }
  .base64 {
    height: 100%;
  }
}
</style>

组件的使用

复制代码
  <t-form-item class="captcha-code" name="captchaCode">
        <t-input v-model="formData.captchaCode" size="large" placeholder="请输入验证码" />
        <t-button size="large">
          <template #content>
            <captcha
              ref="captchaRef"
              v-model="formData.captchaId"
              @change="
                () => {
                  formData.captchaCode = '';
                }
              "
            ></captcha>
          </template>
        </t-button>
      </t-form-item>

顺便在这里提一下 我这对接的是node 的接口 验证码是后端返回的svg 我把验证码svg 后端逻辑也说一下啊

复制代码
import * as svgCaptcha from 'svg-captcha';
import * as svgToDataURL from 'mini-svg-data-uri';
这里用到了两个插件

  /**
   * 验证码
   * @param type 图片验证码类型 svg
   * @param width 宽
   * @param height 高
   */
  async captcha(type: string, width = 150, height = 50) {
    const svg = svgCaptcha.create({
      ignoreChars: 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM',
      width,
      height,
    });
    const result = {
      captchaId: uuid(),
      data: svg.data.replace(/"/g, "'"),
    };
    // 文字变白
    const rpList = [
      '#111',
      '#222',
      '#333',
      '#444',
      '#555',
      '#666',
      '#777',
      '#888',
      '#999',
    ];
    rpList.forEach(rp => {
      result.data = result.data['replaceAll'](rp, '#fff');
    });
    if (type === 'base64') {
      result.data = svgToDataURL(result.data);
    }
    // 半小时过期
    await this.cacheManager.set(
      `verify:img:${result.captchaId}`,
      svg.text.toLowerCase(),
      { ttl: 1800 }
    );
    return result;
  }  

以上的node 的service 接口的逻辑

这是前端收到的data

相关推荐
yuki蜜语12 分钟前
「阴升阳降」不符合物理常识?我用一台热虹吸做了个可拆解的 3D 模型
前端·计算机视觉·前端工程化
金銀銅鐵17 分钟前
带您看懂字节码指令(八):数组里有什么
java·jvm·后端
zark_chuan21 分钟前
怎么让 AI 写十万行代码不腐化:我的 AGENTS.md、Pre-Push 门禁与活体 Wiki
后端
ZJPRENO27 分钟前
一个被身份证认证逼出来的开源项目:AI 辅助开发实战
前端
GoodStudyAndDayDayUp1 小时前
请求前端页面&traefik流程
前端
Rain的Java大神之路2 小时前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写
java·后端·面试
源码_空间3 小时前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端
AI开源软件4 小时前
用MQTT跑通一台模拟设备:看懂IoT产品、物模型和告警
java·vue.js·spring boot·物联网·mqtt·iot·企业管理
geovindu4 小时前
python: PCA
开发语言·后端·python·算法
qq_274475156 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/any
开发语言·javascript