Skip to content

Commit 9778fd2

Browse files
feat: login
1 parent 342ef6d commit 9778fd2

File tree

4 files changed

+199
-5
lines changed

4 files changed

+199
-5
lines changed

ui/index.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="">
33
<head>
4-
<meta charset="UTF-8">
5-
<link rel="icon" href="/favicon.ico">
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>Vite App</title>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" href="/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<base target="_blank" />
8+
<title>%VITE_APP_TITLE%</title>
89
</head>
910
<body>
1011
<div id="app"></div>

ui/src/api/type/login.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ interface LoginRequest {
77
* 密码
88
*/
99
password: string
10+
/**
11+
* 验证码
12+
*/
13+
code: string
1014
}
1115
export type { LoginRequest }
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<template>
2+
<div class="VerifyCode">
3+
<canvas
4+
id="VerifyCode-canvas"
5+
:width="props.contentWidth"
6+
:height="props.contentHeight"
7+
@click="refreshCode"
8+
></canvas>
9+
</div>
10+
</template>
11+
12+
<script setup lang="ts">
13+
import { onMounted, watch, computed } from 'vue'
14+
const props = defineProps({
15+
code: {
16+
type: String,
17+
default: '1234',
18+
},
19+
fontSizeMin: {
20+
type: Number,
21+
default: 25,
22+
},
23+
fontSizeMax: {
24+
type: Number,
25+
default: 35,
26+
},
27+
backgroundColorMin: {
28+
type: Number,
29+
default: 255,
30+
},
31+
backgroundColorMax: {
32+
type: Number,
33+
default: 255,
34+
},
35+
colorMin: {
36+
type: Number,
37+
default: 0,
38+
},
39+
colorMax: {
40+
type: Number,
41+
default: 160,
42+
},
43+
lineColorMin: {
44+
type: Number,
45+
default: 40,
46+
},
47+
lineColorMax: {
48+
type: Number,
49+
default: 180,
50+
},
51+
dotColorMin: {
52+
type: Number,
53+
default: 0,
54+
},
55+
dotColorMax: {
56+
type: Number,
57+
default: 255,
58+
},
59+
contentWidth: {
60+
type: Number,
61+
default: 112,
62+
},
63+
contentHeight: {
64+
type: Number,
65+
default: 40,
66+
},
67+
})
68+
69+
//验证码
70+
const emit = defineEmits(['update:code'])
71+
const verifyCode = computed({
72+
get: () => {
73+
return props.code
74+
},
75+
set: (data) => {
76+
emit('update:code', data)
77+
},
78+
})
79+
80+
// 生成校验码
81+
const makeCode = (len = 4) => {
82+
let code = ''
83+
const codeLength = len
84+
const identifyCodes = '123456789abcdefjhijkinpqrsduvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
85+
for (let i = 0; i < codeLength; i++) {
86+
code += identifyCodes[randomNum(0, identifyCodes.length)]
87+
}
88+
return code
89+
}
90+
91+
// 生成一个随机数
92+
const randomNum = (min = 0, max: number) => Math.floor(Math.random() * (max - min)) + min
93+
94+
// 生成一个随机的颜色
95+
function randomColor(min: number, max: number) {
96+
let r = randomNum(min, max)
97+
let g = randomNum(min, max)
98+
let b = randomNum(min, max)
99+
return 'rgb(' + r + ',' + g + ',' + b + ')'
100+
}
101+
102+
// 绘制干扰线
103+
const drawLine = (ctx: CanvasRenderingContext2D) => {
104+
for (let i = 0; i < 5; i++) {
105+
ctx.strokeStyle = randomColor(props.lineColorMin, props.lineColorMax)
106+
ctx.beginPath()
107+
ctx.moveTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight))
108+
ctx.lineTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight))
109+
ctx.stroke()
110+
}
111+
}
112+
//在画布上显示数据
113+
const drawText = (ctx: CanvasRenderingContext2D, txt: string, i: number) => {
114+
ctx.fillStyle = randomColor(props.colorMin, props.colorMax)
115+
ctx.font = randomNum(props.fontSizeMin, props.fontSizeMax) + 'px SimHei'
116+
let x = (i + 1) * (props.contentWidth / (txt.length + 1))
117+
let y = randomNum(props.fontSizeMax, props.contentHeight - 5)
118+
var deg = randomNum(-45, 45)
119+
// 修改坐标原点和旋转角度
120+
ctx.translate(x, y)
121+
ctx.rotate((deg * Math.PI) / 180)
122+
ctx.fillText(txt[i], 0, 0)
123+
// 恢复坐标原点和旋转角度
124+
ctx.rotate((-deg * Math.PI) / 180)
125+
ctx.translate(-x, -y)
126+
}
127+
// 绘制干扰点
128+
const drawDot = (ctx: CanvasRenderingContext2D) => {
129+
for (let i = 0; i < 80; i++) {
130+
ctx.fillStyle = randomColor(0, 255)
131+
ctx.beginPath()
132+
ctx.arc(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight), 1, 0, 2 * Math.PI)
133+
ctx.fill()
134+
}
135+
}
136+
//画图
137+
const drawPic = () => {
138+
let canvas = document.getElementById('VerifyCode-canvas') as HTMLCanvasElement
139+
if (!canvas) {
140+
return
141+
}
142+
let ctx = canvas.getContext('2d') as CanvasRenderingContext2D
143+
ctx.textBaseline = 'bottom'
144+
// 绘制背景
145+
ctx.fillStyle = randomColor(props.backgroundColorMin, props.backgroundColorMax)
146+
ctx.fillRect(0, 0, props.contentWidth, props.contentHeight)
147+
// 绘制文字
148+
for (let i = 0; i < verifyCode.value.length; i++) {
149+
drawText(ctx, verifyCode.value, i)
150+
}
151+
drawLine(ctx)
152+
drawDot(ctx)
153+
}
154+
155+
// 重置验证码
156+
const refreshCode = () => {
157+
emit('update:code', makeCode())
158+
drawPic()
159+
}
160+
161+
// defineExpose({ refreshCode });
162+
163+
//组件挂载
164+
onMounted(() => {
165+
drawPic()
166+
})
167+
</script>
168+
<style scoped lang="scss">
169+
.VerifyCode {
170+
cursor: pointer;
171+
}
172+
</style>

ui/src/views/login/index.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434
</el-input>
3535
</el-form-item>
3636
</div>
37+
<div class="mb-24">
38+
<el-form-item prop="code">
39+
<div class="flex-between w-full">
40+
<el-input
41+
size="large"
42+
class="input-item"
43+
v-model="loginForm.code"
44+
placeholder="请输入验证码"
45+
>
46+
</el-input>
47+
<VerifyCode v-model:code="identifyCode" />
48+
</div>
49+
</el-form-item>
50+
</div>
3751
</el-form>
3852

3953
<el-button size="large" type="primary" class="w-full" @click="login"
@@ -63,18 +77,21 @@ import type { FormInstance, FormRules } from 'element-plus'
6377
import type { LoginRequest } from '@/api/type/login'
6478
import LoginContainer from '@/views/login/components/LoginContainer.vue'
6579
import LoginLayout from '@/views/login/components/LoginLayout.vue'
80+
import VerifyCode from './components/VerifyCode.vue'
6681
import { t, getBrowserLang } from '@/locales'
6782
import useStore from '@/stores'
6883
6984
const router = useRouter()
7085
const { user } = useStore()
7186
// const { locale } = useI18n({ useScope: 'global' })
7287
const loading = ref<boolean>(false)
88+
const identifyCode = ref<string>('1234')
7389
7490
const loginFormRef = ref<FormInstance>()
7591
const loginForm = ref<LoginRequest>({
7692
username: '',
7793
password: '',
94+
code: '',
7895
})
7996
8097
const rules = ref<FormRules<LoginRequest>>({

0 commit comments

Comments
 (0)