CaptchaModal.vue
4.3 KB
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
<template>
<BasicModal @register="registerModal" width="450px" :minHeight="100" :title="title" @ok="handleSubmit" destroyOnClose :canFullscreen="false">
<BasicForm @register="registerForm">
<template #captchaSlot="{ model, field }">
<div style="width: 100%; display: flex">
<a-input style="width: 200px" v-model:value="model[field]" placeholder="请输入图片验证码" />
<div class="margin-left10">
<img
class="pointer"
v-if="randCodeData.requestCodeSuccess"
style="margin-top: 2px; max-width: initial; height: 30px"
:src="randCodeData.randCodeImage"
@click="getCaptchaCode"
/>
<img v-else class="pointer" style="margin-top: 2px; max-width: initial; height: 30px" :src="codeImage" @click="getCaptchaCode" />
</div>
</div>
</template>
</BasicForm>
</BasicModal>
</template>
<script lang="ts" name="CaptchaModal">
import { defineComponent, reactive, ref } from 'vue';
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicForm, FormSchema, useForm } from '@/components/Form';
import codeImage from '@/assets/images/checkcode.png';
import { getCodeInfo } from '@/api/sys/user';
import { defHttp } from '@/utils/http/axios';
import {useMessage} from "@/hooks/web/useMessage";
export default defineComponent({
name: 'CaptchaModal',
components: { BasicModal, BasicForm },
emits: ['ok','register'],
setup(props, { emit }) {
const title = ref<string>('验证码');
const schemas: FormSchema[] = [
{
field: 'captcha',
component: 'Input',
label: '图片验证码',
rules: [{ required: true }],
slot: 'captchaSlot',
},
];
//表单配置
const [registerForm, { resetFields, validate }] = useForm({
schemas: schemas,
showActionButtonGroup: false,
baseRowStyle: { "justify-content": 'center', "display": "grid", "margin-top": "10px" },
rowProps: { "justify": "center" },
labelCol: { span: 24 },
wrapperCol: { span: 24 },
});
//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
setModalProps({ confirmLoading: true });
//重置表单
await resetFields();
await getCaptchaCode();
setModalProps({ confirmLoading: false });
});
//存放二维码的数据
const randCodeData = reactive({
randCodeImage: '',
requestCodeSuccess: false,
checkKey: -1,
});
const { createMessage } = useMessage();
/**
* 获取验证码
*/
async function getCaptchaCode() {
await resetFields();
//update-begin---author:chenrui ---date:2025/1/7 for:[QQYUN-10775]验证码可以复用 #7674------------
randCodeData.checkKey = new Date().getTime() + Math.random().toString(36).slice(-4); // 1629428467008;
//update-end---author:chenrui ---date:2025/1/7 for:[QQYUN-10775]验证码可以复用 #7674------------
getCodeInfo(randCodeData.checkKey).then((res) => {
randCodeData.randCodeImage = res;
randCodeData.requestCodeSuccess = true;
});
}
/**
* 第三方配置点击事件
*/
async function handleSubmit() {
let values = await validate();
defHttp.post({ url: '/sys/smsCheckCaptcha', params: { captcha: values.captcha, checkKey: randCodeData.checkKey } }, { isTransformResponse: false }).then((res)=>{
if(res.success){
emit('ok');
closeModal();
}else{
createMessage.warning(res.message);
getCaptchaCode();
}
}).catch((res) =>{
createMessage.warning(res.message);
getCaptchaCode();
});
}
/**
* 关闭弹窗
*/
function handleCancel() {
closeModal();
}
return {
title,
registerForm,
registerModal,
handleSubmit,
handleCancel,
randCodeData,
codeImage,
getCaptchaCode,
};
},
});
</script>
<style scoped>
.margin-left10 {
margin-left: 10px;
}
:deep(.ant-row){
display: block;
}
</style>