Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
E
EMS
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hejie
EMS
Commits
09bfcce3
提交
09bfcce3
authored
5月 08, 2025
作者:
詹银鑫
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 修改TS类型bug
上级
25bf8bff
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
53 行增加
和
48 行删除
+53
-48
hooks.ts
src/components/ReImageVerify/src/hooks.ts
+53
-48
没有找到文件。
src/components/ReImageVerify/src/hooks.ts
浏览文件 @
09bfcce3
...
...
@@ -16,7 +16,9 @@ export const useImageVerify = (width = 120, height = 40) => {
function
getImgCode
()
{
if
(
!
domRef
.
value
)
return
;
imgCode
.
value
=
draw
(
domRef
.
value
,
width
,
height
);
draw
(
domRef
.
value
,
width
,
height
).
then
(
code
=>
{
imgCode
.
value
=
code
;
});
}
onMounted
(()
=>
{
...
...
@@ -42,53 +44,56 @@ function randomColor(min: number, max: number) {
const
b
=
randomNum
(
min
,
max
);
return
`rgb(
${
r
}
,
${
g
}
,
${
b
}
)`
;
}
function
draw
(
dom
:
HTMLCanvasElement
,
width
:
number
,
height
:
number
)
{
let
imgCode
=
""
;
let
captchaCode
=
""
;
const
now
=
new
Date
().
getTime
().
toString
();
getCaptcha
(
now
).
then
(
res
=>
{
if
(
res
.
code
===
"0"
)
{
captchaCode
=
res
.
data
;
useUserStoreHook
().
SET_VERIFYCODE
(
captchaCode
);
useUserStoreHook
().
SET_CAPTCHATIME
(
now
);
const
ctx
=
dom
.
getContext
(
"2d"
);
if
(
!
ctx
)
return
imgCode
;
ctx
.
fillStyle
=
randomColor
(
180
,
230
);
ctx
.
fillRect
(
0
,
0
,
width
,
height
);
for
(
let
i
=
0
;
i
<
captchaCode
.
length
;
i
+=
1
)
{
// const text = NUMBER_STRING[randomNum(0, NUMBER_STRING.length)];
const
text
=
captchaCode
[
i
];
imgCode
+=
text
;
const
fontSize
=
randomNum
(
18
,
41
);
const
deg
=
randomNum
(
-
30
,
30
);
ctx
.
font
=
`
${
fontSize
}
px Simhei`
;
ctx
.
textBaseline
=
"top"
;
ctx
.
fillStyle
=
randomColor
(
80
,
150
);
ctx
.
save
();
ctx
.
translate
(
30
*
i
+
15
,
15
);
ctx
.
rotate
((
deg
*
Math
.
PI
)
/
180
);
ctx
.
fillText
(
text
,
-
15
+
5
,
-
15
);
ctx
.
restore
();
}
for
(
let
i
=
0
;
i
<
5
;
i
+=
1
)
{
ctx
.
beginPath
();
ctx
.
moveTo
(
randomNum
(
0
,
width
),
randomNum
(
0
,
height
));
ctx
.
lineTo
(
randomNum
(
0
,
width
),
randomNum
(
0
,
height
));
ctx
.
strokeStyle
=
randomColor
(
180
,
230
);
ctx
.
closePath
();
ctx
.
stroke
();
}
for
(
let
i
=
0
;
i
<
41
;
i
+=
1
)
{
ctx
.
beginPath
();
ctx
.
arc
(
randomNum
(
0
,
width
),
randomNum
(
0
,
height
),
1
,
0
,
2
*
Math
.
PI
);
ctx
.
closePath
();
ctx
.
fillStyle
=
randomColor
(
150
,
200
);
ctx
.
fill
();
function
draw
(
dom
:
HTMLCanvasElement
,
width
:
number
,
height
:
number
):
Promise
<
string
>
{
return
new
Promise
(
resolve
=>
{
let
imgCode
=
""
;
const
now
=
new
Date
().
getTime
().
toString
();
getCaptcha
(
now
).
then
(
res
=>
{
if
(
res
.
code
===
"0"
)
{
const
captchaCode
=
res
.
data
;
useUserStoreHook
().
SET_VERIFYCODE
(
captchaCode
);
useUserStoreHook
().
SET_CAPTCHATIME
(
now
);
const
ctx
=
dom
.
getContext
(
"2d"
);
if
(
!
ctx
)
return
resolve
(
imgCode
);
ctx
.
fillStyle
=
randomColor
(
180
,
230
);
ctx
.
fillRect
(
0
,
0
,
width
,
height
);
for
(
let
i
=
0
;
i
<
captchaCode
.
length
;
i
+=
1
)
{
const
text
=
captchaCode
[
i
];
imgCode
+=
text
;
const
fontSize
=
randomNum
(
18
,
41
);
const
deg
=
randomNum
(
-
30
,
30
);
ctx
.
font
=
`
${
fontSize
}
px Simhei`
;
ctx
.
textBaseline
=
"top"
;
ctx
.
fillStyle
=
randomColor
(
80
,
150
);
ctx
.
save
();
ctx
.
translate
(
30
*
i
+
15
,
15
);
ctx
.
rotate
((
deg
*
Math
.
PI
)
/
180
);
ctx
.
fillText
(
text
,
-
15
+
5
,
-
15
);
ctx
.
restore
();
}
for
(
let
i
=
0
;
i
<
5
;
i
+=
1
)
{
ctx
.
beginPath
();
ctx
.
moveTo
(
randomNum
(
0
,
width
),
randomNum
(
0
,
height
));
ctx
.
lineTo
(
randomNum
(
0
,
width
),
randomNum
(
0
,
height
));
ctx
.
strokeStyle
=
randomColor
(
180
,
230
);
ctx
.
closePath
();
ctx
.
stroke
();
}
for
(
let
i
=
0
;
i
<
41
;
i
+=
1
)
{
ctx
.
beginPath
();
ctx
.
arc
(
randomNum
(
0
,
width
),
randomNum
(
0
,
height
),
1
,
0
,
2
*
Math
.
PI
);
ctx
.
closePath
();
ctx
.
fillStyle
=
randomColor
(
150
,
200
);
ctx
.
fill
();
}
resolve
(
imgCode
);
}
else
{
resolve
(
""
);
}
return
imgCode
as
string
;
}
else
{
return
""
;
}
});
});
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论