编程随笔 编程随笔
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • 知识点

  • 代码调试

  • vue2

  • vue3

    • 注意事项
    • swagger自动生成接口
    • 动态路由
    • 整合 Element Plus
    • wangeditor使用
    • monacoEditor使用
    • 自定义404页面
    • 验证码封装
    • 联表查询SQL
    • element-plus多文件手动上传
  • react

  • typescript

  • 前端
  • vue3
华总
2023-08-24
0
0

验证码封装原创

<template>
	<el-form-item class="login-container" prop="verifyCode" label="验证码">
		<el-input
			v-model="captchaData.verifyCode"
			auto-complete="off"
			placeholder="请输入右侧的计算结果"
			clearable
			class="input"
			size="large"
			@change="verifyCaptcha"
			@input="setVerifyCode"
		>
		  <template #prefix>
			<svg-icon icon-class="verify_code"/>
		  </template>
		</el-input>
		<div class="captcha">
			<img :src="captchaBase64" @click="getCaptcha"/>
		</div>
	</el-form-item>
</template>

<script lang="ts" setup>

import SvgIcon from "@/components/SvgIcon/index.vue";
import { getCaptchaApi } from "@/api/auth";
import { CaptchaCheck, CaptchaControllerService } from "@/generated";

const props = defineProps({
  verifyCode: {
	type: String,
	required: true
  },
  rules: {
	type: Object,
	required: true
  }
});


const  emit  = defineEmits(['update:verifyCode']);

const setVerifyCode = (value: string) => {
  emit('update:verifyCode', value); // 发送自定义事件更新父组件的 verifyCode 值
};

watch(() => props.verifyCode, (newValue) => {
  captchaData.value.verifyCode= newValue; // 当从父组件传来的 verifyCode 值变化时,更新本地的 verifyCode 值
});

/**
 * 验证码图片Base64字符串
 */
const captchaBase64 = ref();

const captchaData = ref<CaptchaCheck>({
	verifyCode: props.verifyCode,
	verifyCodeKey: "",
});

let selfRules = ref({})
const veriflCode = [{
  validator: verifyCaptcha,
  trigger: 'blur'
}]

/**
 * 获取验证码
 */
function getCaptcha() {
	getCaptchaApi().then(({ data }) => {
		const { verifyCodeBase64, verifyCodeKey } = data;
		captchaData.value.verifyCodeKey = verifyCodeKey;
		captchaBase64.value = verifyCodeBase64;
	});
}

/**
 * 校验
 */
async function verifyCaptcha (rule: any, value: any, callback: any)  {
  if (value){
	return new Promise((resolve, reject) => {
	  CaptchaControllerService.checkCaptcha(captchaData.value)
		.then((res) => {
		  const code = res.data
		  if (code !== value){
			callback(new Error('验证码错误!'))
		  } else if(!code){
			callback(new Error('验证码超时!'))
		  }else {
			callback()
		  }
		})
		.catch((error: any) => {
		  reject(error);
		  getCaptcha();
		});
	})
  } else {
	callback(new Error('验证码不能为空!'))
  }
}

onBeforeMount(() => {
  selfRules.value = props.rules
  selfRules.value.verifyCode = veriflCode
})

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

</script>

<style lang="scss" scoped>
:deep(.el-form-item__content){
  display: flex;
  flex-grow: 1;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: left;
}

:deep(.el-form-item__label){
  height: 48px;
  line-height: 48px;
}

.login-container {
	width: 500px;
	//padding: 20px 35px 0;
	display: flex;
	white-space: nowrap;

	.input {
		background: transparent;
		height: 48px;
	    flex-grow: 1;

		// 子组件 scoped 无效,使用 :deep
		//:deep(.el-input__wrapper) {
		//  padding: 0;
		//  background: transparent;
		//  box-shadow: none;
		//
		//  .el-input__inner {
		//	color: #fff;
		//	background: transparent;
		//	border: 0;
		//	border-radius: 0;
		//	caret-color: #fff;
		//
		//	&:-webkit-autofill {
		//	  box-shadow: 0 0 0 1000px transparent inset !important;
		//	  -webkit-text-fill-color: #fff !important;
		//	}
		//
		//	// 设置输入框自动填充的延迟属性
		//	&:-webkit-autofill,
		//	&:-webkit-autofill:hover,
		//	&:-webkit-autofill:focus,
		//	&:-webkit-autofill:active {
		//	  transition: color 99999s ease-out, background-color 99999s ease-out;
		//	  transition-delay: 99999s;
		//	}
		//  }
		//}
	}

	.captcha {
		margin-left: 1px;

		img {
		  width: 120px;
		  height: 48px;
		  cursor: pointer;
		  display: flex;
		  align-items: center;
		}
	}
}
</style>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#前端#EasyCaptcha验证码
上次更新: 2024/01/14 16:27:31
自定义404页面
联表查询SQL

← 自定义404页面 联表查询SQL→

最近更新
01
element-plus多文件手动上传 原创
11-03
02
TrueLicense 创建及安装证书 原创
10-25
03
手动修改迅捷配置 原创
09-03
04
安装 acme.sh 原创
08-29
05
zabbix部署 原创
08-20
更多文章>
Copyright © 2023-2024 liyao52033
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式