mirror of
https://github.com/certd/certd.git
synced 2026-04-15 05:00:52 +08:00
perf: 登录注册、找回密码都支持极验验证码和图片验证码
This commit is contained in:
@@ -36,7 +36,7 @@ export class SysPublicSettings extends BaseSettings {
|
||||
captchaEnabled = false;
|
||||
//验证码类型
|
||||
captchaType?: string;
|
||||
captchaAddonId?:string;
|
||||
captchaAddonId?:number;
|
||||
}
|
||||
|
||||
export class SysPrivateSettings extends BaseSettings {
|
||||
|
||||
@@ -76,9 +76,21 @@ export class AddonService extends BaseService<AddonEntity> {
|
||||
}
|
||||
|
||||
async getAddonById(id: any, checkUserId: boolean, userId?: number): Promise<any> {
|
||||
const ctx = {
|
||||
http: http,
|
||||
logger: logger,
|
||||
utils: utils,
|
||||
};
|
||||
|
||||
|
||||
if (!id){
|
||||
//使用图片验证码
|
||||
return await newAddon("captcha", "image", {},ctx);
|
||||
}
|
||||
const entity = await this.info(id);
|
||||
if (entity == null) {
|
||||
throw new Error(`该Addon配置不存在,请确认是否已被删除:id=${id}`);
|
||||
//使用图片验证码
|
||||
return await newAddon("captcha", "image", {},ctx);
|
||||
}
|
||||
if (checkUserId) {
|
||||
if (userId == null) {
|
||||
@@ -89,17 +101,12 @@ export class AddonService extends BaseService<AddonEntity> {
|
||||
}
|
||||
}
|
||||
|
||||
// const access = accessRegistry.get(entity.type);
|
||||
const setting = JSON.parse(entity.setting ??"{}")
|
||||
const input = {
|
||||
id: entity.id,
|
||||
...setting,
|
||||
};
|
||||
const ctx = {
|
||||
http: http,
|
||||
logger: logger,
|
||||
utils: utils,
|
||||
};
|
||||
|
||||
return await newAddon(entity.addonType, entity.type, input,ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<template>
|
||||
<component :is="captchaComponent" v-if="settingStore.inited" ref="captchaRef" class="captcha_input" :captcha-get="getCaptcha" @change="onChange" />
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, defineAsyncComponent } from "vue";
|
||||
import { useSettingStore } from "/@/store/settings";
|
||||
import { nanoid } from "nanoid";
|
||||
import { request } from "/@/api/service";
|
||||
|
||||
const captchaRef = ref(null);
|
||||
const settingStore = useSettingStore();
|
||||
|
||||
const emits = defineEmits(["update:modelValue", "change"]);
|
||||
const captchaImpls = import.meta.glob("./captchas/*.vue");
|
||||
|
||||
const captchaAddonId = computed(() => {
|
||||
return settingStore.sysPublic.captchaAddonId ?? 0;
|
||||
});
|
||||
const captchaComponent = computed(() => {
|
||||
let type = "image";
|
||||
if (settingStore.sysPublic.captchaAddonId && settingStore.sysPublic.captchaType) {
|
||||
type = settingStore.sysPublic.captchaType;
|
||||
}
|
||||
const componentName = `${type}_captcha`;
|
||||
return defineAsyncComponent(captchaImpls[`./captchas/${componentName}.vue`]);
|
||||
});
|
||||
|
||||
async function getCaptcha(): Promise<any> {
|
||||
const randomStr = nanoid(10);
|
||||
return await request({
|
||||
url: `/basic/code/captcha/get?randomStr=${randomStr}`,
|
||||
method: "post",
|
||||
data: {
|
||||
captchaAddonId: captchaAddonId.value,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function onChange(data) {
|
||||
emits("update:modelValue", data);
|
||||
emits("change", data);
|
||||
}
|
||||
|
||||
async function getCaptchaForm() {
|
||||
return await captchaRef.value.getCaptchaForm();
|
||||
}
|
||||
defineExpose({
|
||||
getCaptchaForm,
|
||||
});
|
||||
</script>
|
||||
@@ -1,64 +1,56 @@
|
||||
<template>
|
||||
<div ref="captchaRef" class="captcha_input"></div>
|
||||
<div ref="captchaRef" class="geetest_captcha_wrapper"></div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { onMounted, defineProps, defineEmits, ref } from "vue";
|
||||
import { onMounted, defineProps, defineEmits, ref, onUnmounted } from "vue";
|
||||
import { useSettingStore } from "/@/store/settings";
|
||||
import { request } from "/src/api/service";
|
||||
import { notification } from "ant-design-vue";
|
||||
const props = defineProps<{
|
||||
modelValue?: any;
|
||||
}>();
|
||||
const emit = defineEmits(["update:modelValue", "change"]);
|
||||
|
||||
defineOptions({
|
||||
name: "GeetestCaptcha",
|
||||
});
|
||||
const emit = defineEmits(["update:modelValue", "change"]);
|
||||
const props = defineProps<{
|
||||
captchaGet: () => Promise<any>;
|
||||
}>();
|
||||
const captchaRef = ref(null);
|
||||
// const addonApi = createAddonApi();
|
||||
const settingStore = useSettingStore();
|
||||
|
||||
const api = {
|
||||
async getClientParams(): Promise<any> {
|
||||
const res = await request({
|
||||
url: "/captcha/getParams",
|
||||
method: "post",
|
||||
});
|
||||
return res;
|
||||
},
|
||||
};
|
||||
|
||||
// async function getCaptchaAddonDefine() {
|
||||
// const type = settingStore.public.captchaType;
|
||||
// const define = addonApi.getDefineByType("captcha", type);
|
||||
// }
|
||||
|
||||
const captchaInstanceRef = ref({});
|
||||
async function init() {
|
||||
const params = await api.getClientParams();
|
||||
// if (!initGeetest4) {
|
||||
// await import("https://static.geetest.com/v4/gt4.js");
|
||||
// }
|
||||
|
||||
const { captchaId } = await props.captchaGet();
|
||||
// @ts-ignore
|
||||
initGeetest4(
|
||||
{
|
||||
captchaId: params.captchaId,
|
||||
captchaId: captchaId,
|
||||
},
|
||||
(captcha: any) => {
|
||||
// captcha为验证码实例
|
||||
captcha.appendTo(captchaRef.value); // 调用appendTo将验证码插入到页的某一个元素中,这个元素用户可以自定义
|
||||
captchaInstanceRef.value.instance = captcha;
|
||||
captchaInstanceRef.value.captchaId = params.captchaId;
|
||||
captchaInstanceRef.value.captchaId = captchaId;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function getValidatedForm() {
|
||||
function getCaptchaForm() {
|
||||
if (!captchaInstanceRef.value?.instance) {
|
||||
notification.error({
|
||||
message: "验证码还未初始化",
|
||||
});
|
||||
// notification.error({
|
||||
// message: "验证码还未初始化",
|
||||
// });
|
||||
return false;
|
||||
}
|
||||
const result = await captchaInstanceRef.value.instance.getValidate();
|
||||
const result = captchaInstanceRef.value.instance.getValidate();
|
||||
if (!result) {
|
||||
notification.error({
|
||||
message: "请先完成验证码验证",
|
||||
});
|
||||
// notification.error({
|
||||
// message: "请先完成验证码验证",
|
||||
// });
|
||||
return false;
|
||||
}
|
||||
result.captcha_id = captchaInstanceRef.value.captchaId;
|
||||
@@ -66,13 +58,27 @@ async function getValidatedForm() {
|
||||
return result;
|
||||
}
|
||||
|
||||
function onChange(value: string) {
|
||||
const valueRef = ref(null);
|
||||
const timeoutId = setInterval(() => {
|
||||
const form = getCaptchaForm();
|
||||
if (form && valueRef.value != form) {
|
||||
console.log("form", form);
|
||||
valueRef.value = form;
|
||||
emitChange(form);
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
onUnmounted(() => {
|
||||
clearTimeout(timeoutId);
|
||||
});
|
||||
|
||||
function emitChange(value: string) {
|
||||
emit("update:modelValue", value);
|
||||
emit("change", value);
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
getValidatedForm,
|
||||
getCaptchaForm,
|
||||
});
|
||||
|
||||
onMounted(async () => {
|
||||
@@ -80,7 +86,7 @@ onMounted(async () => {
|
||||
});
|
||||
</script>
|
||||
<style lang="less">
|
||||
.captcha_input {
|
||||
.geetest_captcha_wrapper {
|
||||
.geetest_captcha {
|
||||
.geetest_holder {
|
||||
width: 100%;
|
||||
@@ -0,0 +1,59 @@
|
||||
<template>
|
||||
<div class="flex">
|
||||
<a-input :value="valueRef" placeholder="请输入图片验证码" autocomplete="off" @update:value="onChange">
|
||||
<template #prefix>
|
||||
<fs-icon icon="ion:image-outline"></fs-icon>
|
||||
</template>
|
||||
</a-input>
|
||||
<div class="input-right pointer" title="点击刷新">
|
||||
<img class="image-code" :src="imageCodeSrc" @click="resetImageCode" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { defineEmits, defineExpose, defineProps, ref } from "vue";
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
const props = defineProps<{
|
||||
captchaGet?: () => Promise<any>;
|
||||
}>();
|
||||
defineOptions({
|
||||
name: "ImageCaptcha",
|
||||
});
|
||||
const emit = defineEmits(["update:modelValue", "change"]);
|
||||
|
||||
const valueRef = ref("");
|
||||
const randomStrRef = ref();
|
||||
const imageCodeSrc = ref();
|
||||
async function resetImageCode() {
|
||||
const res = await props.captchaGet();
|
||||
randomStrRef.value = res.randomStr;
|
||||
valueRef.value = "";
|
||||
emitChange(null);
|
||||
imageCodeSrc.value = "data:image/svg+xml," + encodeURIComponent(res.imageData);
|
||||
}
|
||||
|
||||
function getCaptchaForm() {
|
||||
return {
|
||||
imageCode: valueRef.value,
|
||||
randomStr: randomStrRef.value,
|
||||
};
|
||||
}
|
||||
defineExpose({
|
||||
resetImageCode,
|
||||
getCaptchaForm,
|
||||
});
|
||||
|
||||
resetImageCode();
|
||||
|
||||
function onChange(value: string) {
|
||||
valueRef.value = value;
|
||||
const form = getCaptchaForm();
|
||||
emitChange(form);
|
||||
}
|
||||
|
||||
function emitChange(value) {
|
||||
emit("update:modelValue", value);
|
||||
emit("change", value);
|
||||
}
|
||||
</script>
|
||||
@@ -53,7 +53,6 @@ const pagerRef: Ref = ref({
|
||||
current: 1,
|
||||
});
|
||||
const getOptions = async () => {
|
||||
debugger;
|
||||
if (loading.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -712,7 +712,7 @@ export default {
|
||||
showRunStrategy: "Show RunStrategy",
|
||||
showRunStrategyHelper: "Allow modify the run strategy of the task",
|
||||
|
||||
captchaEnabled: "Enable Captcha",
|
||||
captchaEnabled: "Enable Login Captcha",
|
||||
captchaHelper: "Whether to enable captcha verification for login",
|
||||
captchaType: "Captcha Type",
|
||||
},
|
||||
|
||||
@@ -715,7 +715,7 @@ export default {
|
||||
showRunStrategy: "显示运行策略选择",
|
||||
showRunStrategyHelper: "任务设置中是否允许选择运行策略",
|
||||
|
||||
captchaEnabled: "启用验证码",
|
||||
captchaEnabled: "启用登录验证码",
|
||||
captchaHelper: "登录时是否启用验证码",
|
||||
captchaType: "验证码类型",
|
||||
},
|
||||
|
||||
@@ -140,6 +140,11 @@ async function emitValue(value: any) {
|
||||
async function refreshTarget(value: any) {
|
||||
if (value > 0) {
|
||||
target.value = await api.GetSimpleInfo(value);
|
||||
} else {
|
||||
target.value = {
|
||||
//captchaType会监听此字段,给个默认值
|
||||
type: "",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,11 @@
|
||||
</template>
|
||||
</a-input>
|
||||
</a-form-item>
|
||||
<a-form-item has-feedback name="captchaForEmail" label="验证码">
|
||||
<CaptchaInput v-model:model-value="formState.captchaForEmail"></CaptchaInput>
|
||||
</a-form-item>
|
||||
<a-form-item has-feedback name="validateCode" label="邮件验证码">
|
||||
<email-code v-model:value="formState.validateCode" :img-code="formState.imgCode" :email="formState.input" :random-str="formState.randomStr" verification-type="forgotPassword" />
|
||||
<email-code v-model:value="formState.validateCode" :captcha="formState.captchaForEmail" :email="formState.input" :random-str="formState.randomStr" verification-type="forgotPassword" />
|
||||
</a-form-item>
|
||||
</a-tab-pane>
|
||||
<a-tab-pane key="mobile" tab="手机号找回">
|
||||
@@ -32,23 +35,15 @@
|
||||
</template>
|
||||
</a-input>
|
||||
</a-form-item>
|
||||
<a-form-item has-feedback name="captchaForSms" label="验证码">
|
||||
<CaptchaInput v-model:model-value="formState.captchaForSms"></CaptchaInput>
|
||||
</a-form-item>
|
||||
<a-form-item name="validateCode" label="手机验证码">
|
||||
<sms-code
|
||||
v-model:value="formState.validateCode"
|
||||
:img-code="formState.imgCode"
|
||||
:mobile="formState.input"
|
||||
:phone-code="formState.phoneCode"
|
||||
:random-str="formState.randomStr"
|
||||
verification-type="forgotPassword"
|
||||
/>
|
||||
<sms-code v-model:value="formState.validateCode" :captcha="formState.captchaForSms" :mobile="formState.input" :phone-code="formState.phoneCode" verification-type="forgotPassword" />
|
||||
</a-form-item>
|
||||
</a-tab-pane>
|
||||
</a-tabs>
|
||||
|
||||
<a-form-item has-feedback name="imgCode" label="图片验证码">
|
||||
<image-code ref="imageCodeRef" v-model:value="formState.imgCode" v-model:random-str="formState.randomStr"></image-code>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item has-feedback name="password" label="新密码">
|
||||
<a-input-password v-model:value="formState.password" placeholder="新密码" size="large" autocomplete="off">
|
||||
<template #prefix>
|
||||
@@ -66,8 +61,10 @@
|
||||
<a-form-item>
|
||||
<a-button type="primary" size="large" html-type="submit" class="submit-button"> 找回密码</a-button>
|
||||
|
||||
<div v-comm="false" class="mt-2">
|
||||
<a href="https://certd.docmirror.cn/guide/use/forgotpasswd/" target="_blank"> 管理员无绑定通信方式或MFA丢失找回 </a>
|
||||
<div class="mt-2 flex-between">
|
||||
<a v-comm="false" href="https://certd.docmirror.cn/guide/use/forgotpasswd/" target="_blank"> 管理员无绑定通信方式或MFA丢失找回 </a>
|
||||
|
||||
<router-link :to="{ name: 'login' }"> 返回登录 </router-link>
|
||||
</div>
|
||||
</a-form-item>
|
||||
</a-form>
|
||||
@@ -82,6 +79,7 @@ import SmsCode from "/@/views/framework/login/sms-code.vue";
|
||||
import { utils } from "@fast-crud/fast-crud";
|
||||
import { useUserStore } from "/@/store/user";
|
||||
import { useSettingStore } from "/@/store/settings";
|
||||
import CaptchaInput from "/@/components/captcha/captcha-input.vue";
|
||||
defineOptions({
|
||||
name: "ForgotPasswordPage",
|
||||
});
|
||||
@@ -89,7 +87,8 @@ defineOptions({
|
||||
const rules = {
|
||||
input: [{ required: true }],
|
||||
validateCode: [{ required: true }],
|
||||
imgCode: [{ required: true }, { min: 4, max: 4, message: "请输入4位图片验证码" }],
|
||||
captchaForEmail: [{ required: true }],
|
||||
captchaForSms: [{ required: true }],
|
||||
password: [
|
||||
{ required: true, trigger: "change", message: "请输入密码" },
|
||||
{ min: 6, message: "至少输入6位密码" },
|
||||
@@ -119,15 +118,13 @@ const forgotPasswordType = ref();
|
||||
const userStore = useUserStore();
|
||||
const settingStore = useSettingStore();
|
||||
const formRef = ref();
|
||||
const imageCodeRef = ref();
|
||||
|
||||
const formState: any = reactive({
|
||||
input: "",
|
||||
randomStr: "",
|
||||
imgCode: "",
|
||||
captchaForSms: null,
|
||||
captchaForEmail: null,
|
||||
phoneCode: "86",
|
||||
validateCode: "",
|
||||
|
||||
password: "",
|
||||
confirmPassword: "",
|
||||
});
|
||||
@@ -141,7 +138,6 @@ onMounted(() => {
|
||||
watch(forgotPasswordType, () => {
|
||||
formState.input = "";
|
||||
formState.validateCode = "";
|
||||
imageCodeRef.value.resetImageCode();
|
||||
formRef.value.clearValidate(Object.keys(formState).filter(key => !["password", "confirmPassword"].includes(key)));
|
||||
});
|
||||
|
||||
@@ -150,8 +146,6 @@ const handleFinish = async (values: any) => {
|
||||
toRaw({
|
||||
type: forgotPasswordType.value,
|
||||
input: formState.input,
|
||||
randomStr: formState.randomStr,
|
||||
imgCode: formState.imgCode,
|
||||
validateCode: formState.validateCode,
|
||||
password: formState.password,
|
||||
confirmPassword: formState.confirmPassword,
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
<template>
|
||||
<div class="flex">
|
||||
<a-input :value="value" placeholder="请输入图片验证码" autocomplete="off" @update:value="onChange">
|
||||
<template #prefix>
|
||||
<fs-icon icon="ion:image-outline"></fs-icon>
|
||||
</template>
|
||||
</a-input>
|
||||
<div class="input-right pointer" title="点击刷新">
|
||||
<img class="image-code" :src="imageCodeUrl" @click="resetImageCode" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { ref, useAttrs, defineExpose } from "vue";
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
const props = defineProps<{
|
||||
randomStr?: string;
|
||||
value?: string;
|
||||
}>();
|
||||
const emit = defineEmits(["update:value", "update:randomStr", "change"]);
|
||||
|
||||
function onChange(value: string) {
|
||||
emit("update:value", value);
|
||||
emit("change", value);
|
||||
}
|
||||
|
||||
const imageCodeUrl = ref();
|
||||
function resetImageCode() {
|
||||
const randomStr = nanoid(10);
|
||||
let url = "api/basic/code/captcha";
|
||||
imageCodeUrl.value = url + "?randomStr=" + randomStr;
|
||||
emit("update:randomStr", randomStr);
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
resetImageCode,
|
||||
})
|
||||
|
||||
resetImageCode();
|
||||
</script>
|
||||
@@ -21,8 +21,8 @@
|
||||
</a-input-password>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item v-if="settingStore.sysPublic.captchaEnabled" required name="captcha">
|
||||
<CaptchaInput ref="captchaInputRef" v-model:model-value="formState.captcha"></CaptchaInput>
|
||||
<a-form-item v-if="settingStore.sysPublic.captchaEnabled" has-feedback required name="captcha" :rules="rules.captcha">
|
||||
<CaptchaInput v-model:model-value="formState.captcha"></CaptchaInput>
|
||||
</a-form-item>
|
||||
</template>
|
||||
</a-tab-pane>
|
||||
@@ -36,12 +36,12 @@
|
||||
</a-input>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item has-feedback name="imgCode">
|
||||
<image-code v-model:value="formState.imgCode" v-model:random-str="formState.randomStr"></image-code>
|
||||
<a-form-item has-feedback name="smsCaptcha">
|
||||
<CaptchaInput v-model:model-value="formState.smsCaptcha"></CaptchaInput>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item name="smsCode" :rules="rules.smsCode">
|
||||
<sms-code v-model:value="formState.smsCode" :img-code="formState.imgCode" :mobile="formState.mobile" :phone-code="formState.phoneCode" :random-str="formState.randomStr" />
|
||||
<sms-code v-model:value="formState.smsCode" :captcha="formState.smsCaptcha" :mobile="formState.mobile" :phone-code="formState.phoneCode" />
|
||||
</a-form-item>
|
||||
</template>
|
||||
</a-tab-pane>
|
||||
@@ -91,14 +91,13 @@ import { defineComponent, nextTick, reactive, ref, toRaw } from "vue";
|
||||
import { useUserStore } from "/src/store/user";
|
||||
import { useSettingStore } from "/@/store/settings";
|
||||
import { utils } from "@fast-crud/fast-crud";
|
||||
import ImageCode from "/@/views/framework/login/image-code.vue";
|
||||
import SmsCode from "/@/views/framework/login/sms-code.vue";
|
||||
import { useI18n } from "/@/locales";
|
||||
import { LanguageToggle } from "/@/vben/layouts";
|
||||
import CaptchaInput from "./captcha-input.vue";
|
||||
import CaptchaInput from "/@/components/captcha/captcha-input.vue";
|
||||
export default defineComponent({
|
||||
name: "LoginPage",
|
||||
components: { LanguageToggle, SmsCode, ImageCode, CaptchaInput },
|
||||
components: { LanguageToggle, SmsCode, CaptchaInput },
|
||||
setup() {
|
||||
const { t } = useI18n();
|
||||
const verifyCodeInputRef = ref();
|
||||
@@ -112,10 +111,9 @@ export default defineComponent({
|
||||
mobile: "",
|
||||
password: "",
|
||||
loginType: "password", //password
|
||||
imgCode: "",
|
||||
smsCode: "",
|
||||
randomStr: "",
|
||||
captcha: {},
|
||||
captcha: null,
|
||||
smsCaptcha: null,
|
||||
});
|
||||
|
||||
const rules = {
|
||||
@@ -143,6 +141,12 @@ export default defineComponent({
|
||||
message: "请输入短信验证码",
|
||||
},
|
||||
],
|
||||
captcha: [
|
||||
{
|
||||
required: true,
|
||||
message: "请进行验证码验证",
|
||||
},
|
||||
],
|
||||
};
|
||||
const layout = {
|
||||
labelCol: {
|
||||
@@ -165,10 +169,10 @@ export default defineComponent({
|
||||
const handleFinish = async (values: any) => {
|
||||
loading.value = true;
|
||||
try {
|
||||
formState.captcha = await doCaptchaValidate();
|
||||
if (!formState.captcha) {
|
||||
return;
|
||||
}
|
||||
// formState.captcha = await doCaptchaValidate();
|
||||
// if (!formState.captcha) {
|
||||
// return;
|
||||
// }
|
||||
const loginType = formState.loginType;
|
||||
await userStore.login(loginType, toRaw(formState));
|
||||
} catch (e: any) {
|
||||
@@ -204,6 +208,7 @@ export default defineComponent({
|
||||
}
|
||||
|
||||
const captchaInputRef = ref();
|
||||
const captchaInputForSmsCode = ref();
|
||||
async function doCaptchaValidate() {
|
||||
if (!sysPublicSettings.captchaEnabled) {
|
||||
return {};
|
||||
@@ -235,6 +240,7 @@ export default defineComponent({
|
||||
verifyCodeInputRef,
|
||||
settingStore,
|
||||
captchaInputRef,
|
||||
captchaInputForSmsCode,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<fs-icon icon="ion:mail-outline"></fs-icon>
|
||||
</template>
|
||||
</a-input>
|
||||
<div class="input-right">
|
||||
<div class="input-right ml-5">
|
||||
<a-button class="getCaptcha" type="primary" tabindex="-1" :disabled="smsSendBtnDisabled" @click="sendSmsCode">
|
||||
{{ smsTime <= 0 ? "发送" : smsTime + " s" }}
|
||||
</a-button>
|
||||
@@ -21,8 +21,7 @@ const props = defineProps<{
|
||||
value?: string;
|
||||
mobile?: string;
|
||||
phoneCode?: string;
|
||||
imgCode?: string;
|
||||
randomStr?: string;
|
||||
captcha?: any;
|
||||
verificationType?: string;
|
||||
}>();
|
||||
const emit = defineEmits(["update:value", "change"]);
|
||||
@@ -48,8 +47,8 @@ async function sendSmsCode() {
|
||||
notification.error({ message: "请输入手机号" });
|
||||
return;
|
||||
}
|
||||
if (!props.imgCode) {
|
||||
notification.error({ message: "请输入图片验证码" });
|
||||
if (!props.captcha) {
|
||||
notification.error({ message: "请输入验证码" });
|
||||
return;
|
||||
}
|
||||
loading.value = true;
|
||||
@@ -57,8 +56,7 @@ async function sendSmsCode() {
|
||||
await api.sendSmsCode({
|
||||
phoneCode: props.phoneCode,
|
||||
mobile: props.mobile,
|
||||
imgCode: props.imgCode,
|
||||
randomStr: props.randomStr,
|
||||
captcha: props.captcha,
|
||||
verificationType: props.verificationType,
|
||||
});
|
||||
} finally {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<fs-icon icon="ion:mail-outline"></fs-icon>
|
||||
</template>
|
||||
</a-input>
|
||||
<div class="input-right">
|
||||
<div class="input-right ml-5">
|
||||
<a-button class="getCaptcha" type="primary" tabindex="-1" :disabled="smsSendBtnDisabled" @click="sendSmsCode">
|
||||
{{ smsTime <= 0 ? "发送" : smsTime + " s" }}
|
||||
</a-button>
|
||||
@@ -20,8 +20,7 @@ import * as api from "/@/store/settings/api.basic";
|
||||
const props = defineProps<{
|
||||
value?: string;
|
||||
email?: string;
|
||||
imgCode?: string;
|
||||
randomStr?: string;
|
||||
captcha?: any;
|
||||
verificationType?: string;
|
||||
}>();
|
||||
const emit = defineEmits(["update:value", "change"]);
|
||||
@@ -44,16 +43,15 @@ async function sendSmsCode() {
|
||||
notification.error({ message: "请输入邮箱" });
|
||||
return;
|
||||
}
|
||||
if (!props.imgCode) {
|
||||
notification.error({ message: "请输入图片验证码" });
|
||||
if (!props.captcha) {
|
||||
notification.error({ message: "请输入验证码" });
|
||||
return;
|
||||
}
|
||||
loading.value = true;
|
||||
try {
|
||||
await api.sendEmailCode({
|
||||
email: props.email,
|
||||
imgCode: props.imgCode,
|
||||
randomStr: props.randomStr,
|
||||
captcha: props.captcha,
|
||||
verificationType: props.verificationType,
|
||||
});
|
||||
} finally {
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
</template>
|
||||
</a-input-password>
|
||||
</a-form-item>
|
||||
<a-form-item has-feedback name="imgCode" label="图片验证码" :rules="rules.imgCode">
|
||||
<image-code v-model:value="formState.imgCode" v-model:random-str="formState.randomStr"></image-code>
|
||||
<a-form-item has-feedback name="captcha" label="验证码" :rules="rules.captcha">
|
||||
<CaptchaInput v-model:model-value="formState.captcha"></CaptchaInput>
|
||||
</a-form-item>
|
||||
</template>
|
||||
</a-tab-pane>
|
||||
@@ -61,12 +61,12 @@
|
||||
</a-input-password>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item has-feedback name="imgCode" label="图片验证码" :rules="rules.imgCode">
|
||||
<image-code v-model:value="formState.imgCode" v-model:random-str="formState.randomStr"></image-code>
|
||||
<a-form-item has-feedback name="imgCode" label="验证码" :rules="rules.imgCode">
|
||||
<CaptchaInput v-model:model-value="formState.captchaForEmail"></CaptchaInput>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item has-feedback name="validateCode" :rules="rules.validateCode" label="邮件验证码">
|
||||
<email-code v-model:value="formState.validateCode" :img-code="formState.imgCode" :email="formState.email" :random-str="formState.randomStr" />
|
||||
<email-code v-model:value="formState.validateCode" :captcha="formState.captchaForEmail" :email="formState.email" />
|
||||
</a-form-item>
|
||||
</template>
|
||||
</a-tab-pane>
|
||||
@@ -86,13 +86,13 @@
|
||||
import { defineComponent, reactive, ref, toRaw } from "vue";
|
||||
import { useUserStore } from "/src/store/user";
|
||||
import { utils } from "@fast-crud/fast-crud";
|
||||
import ImageCode from "/@/views/framework/login/image-code.vue";
|
||||
import EmailCode from "./email-code.vue";
|
||||
import { useSettingStore } from "/@/store/settings";
|
||||
import { notification } from "ant-design-vue";
|
||||
import CaptchaInput from "/@/components/captcha/captcha-input.vue";
|
||||
export default defineComponent({
|
||||
name: "RegisterPage",
|
||||
components: { EmailCode, ImageCode },
|
||||
components: { CaptchaInput, EmailCode },
|
||||
setup() {
|
||||
const settingsStore = useSettingStore();
|
||||
const registerType = ref("email");
|
||||
@@ -114,7 +114,7 @@ export default defineComponent({
|
||||
username: "",
|
||||
password: "",
|
||||
confirmPassword: "",
|
||||
randomStr: "",
|
||||
captcha: null,
|
||||
});
|
||||
|
||||
const rules = {
|
||||
@@ -159,17 +159,6 @@ export default defineComponent({
|
||||
},
|
||||
],
|
||||
|
||||
imgCode: [
|
||||
{
|
||||
required: true,
|
||||
message: "请输入图片验证码",
|
||||
},
|
||||
{
|
||||
min: 4,
|
||||
max: 4,
|
||||
message: "请输入4位图片验证码",
|
||||
},
|
||||
],
|
||||
smsCode: [
|
||||
{
|
||||
required: true,
|
||||
@@ -198,9 +187,8 @@ export default defineComponent({
|
||||
type: registerType.value,
|
||||
password: formState.password,
|
||||
username: formState.username,
|
||||
imgCode: formState.imgCode,
|
||||
randomStr: formState.randomStr,
|
||||
email: formState.email,
|
||||
captcha: formState.captcha,
|
||||
validateCode: formState.validateCode,
|
||||
}) as any
|
||||
);
|
||||
@@ -214,16 +202,7 @@ export default defineComponent({
|
||||
formRef.value.resetFields();
|
||||
};
|
||||
|
||||
const imageCodeUrl = ref();
|
||||
function resetImageCode() {
|
||||
let url = "/basic/code";
|
||||
imageCodeUrl.value = url + "?t=" + new Date().getTime();
|
||||
}
|
||||
resetImageCode();
|
||||
|
||||
return {
|
||||
resetImageCode,
|
||||
imageCodeUrl,
|
||||
formState,
|
||||
formRef,
|
||||
rules,
|
||||
|
||||
@@ -51,13 +51,11 @@
|
||||
<a-switch v-model:checked="formState.public.captchaEnabled" />
|
||||
<div class="helper" v-html="t('certd.sys.setting.captchaHelper')"></div>
|
||||
</a-form-item>
|
||||
<a-form-item v-if="formState.public.captchaEnabled" :label="t('certd.sys.setting.captchaType')" :name="['public', 'captchaAddonId']">
|
||||
<a-form-item :label="t('certd.sys.setting.captchaType')" :name="['public', 'captchaAddonId']">
|
||||
<addon-selector v-model:model-value="formState.public.captchaAddonId" addon-type="captcha" from="sys" @selected-change="onAddonChanged" />
|
||||
|
||||
<a-input v-model:model-value="formState.public.captchaType" class="hidden"></a-input>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item v-if="formState.public.captchaEnabled" :name="['public', 'captchaType']" class="hidden">
|
||||
<a-form-item :name="['public', 'captchaType']" class="hidden">
|
||||
<a-input v-model:model-value="formState.public.captchaType"></a-input>
|
||||
</a-form-item>
|
||||
|
||||
@@ -130,7 +128,6 @@ async function stopOtherUserTimer() {
|
||||
}
|
||||
|
||||
function onAddonChanged(target: any) {
|
||||
debugger;
|
||||
formState.public.captchaType = target.type;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Rule, RuleType } from '@midwayjs/validate';
|
||||
import { ALL, Body, Controller, Get, Inject, Post, Provide, Query } from '@midwayjs/core';
|
||||
import { BaseController, Constants } from '@certd/lib-server';
|
||||
import { CodeService } from '../../modules/basic/service/code-service.js';
|
||||
import { EmailService } from '../../modules/basic/service/email-service.js';
|
||||
import { Rule, RuleType } from "@midwayjs/validate";
|
||||
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
|
||||
import { BaseController, Constants, SysSettingsService } from "@certd/lib-server";
|
||||
import { CodeService } from "../../modules/basic/service/code-service.js";
|
||||
import { EmailService } from "../../modules/basic/service/email-service.js";
|
||||
import { CaptchaService } from "../../modules/basic/service/captcha-service.js";
|
||||
|
||||
export class SmsCodeReq {
|
||||
@Rule(RuleType.string().required())
|
||||
@@ -11,11 +12,8 @@ export class SmsCodeReq {
|
||||
@Rule(RuleType.string().required())
|
||||
mobile: string;
|
||||
|
||||
@Rule(RuleType.string().required().max(10))
|
||||
randomStr: string;
|
||||
|
||||
@Rule(RuleType.string().required().max(4))
|
||||
imgCode: string;
|
||||
@Rule(RuleType.required())
|
||||
captcha: any;
|
||||
|
||||
@Rule(RuleType.string())
|
||||
verificationType: string;
|
||||
@@ -25,11 +23,8 @@ export class EmailCodeReq {
|
||||
@Rule(RuleType.string().required())
|
||||
email: string;
|
||||
|
||||
@Rule(RuleType.string().required().max(10))
|
||||
randomStr: string;
|
||||
|
||||
@Rule(RuleType.string().required().max(4))
|
||||
imgCode: string;
|
||||
@Rule(RuleType.required())
|
||||
captcha: any;
|
||||
|
||||
@Rule(RuleType.string())
|
||||
verificationType: string;
|
||||
@@ -48,6 +43,17 @@ export class BasicController extends BaseController {
|
||||
|
||||
@Inject()
|
||||
emailService: EmailService;
|
||||
@Inject()
|
||||
sysSettingsService: SysSettingsService;
|
||||
|
||||
@Inject()
|
||||
captchaService: CaptchaService;
|
||||
|
||||
@Post('/captcha/get', { summary: Constants.per.guest })
|
||||
async getCaptcha(@Query("captchaAddonId") captchaAddonId:number) {
|
||||
const form = await this.captchaService.getCaptcha(captchaAddonId)
|
||||
return this.ok(form);
|
||||
}
|
||||
|
||||
@Post('/sendSmsCode', { summary: Constants.per.guest })
|
||||
public async sendSmsCode(
|
||||
@@ -64,8 +70,8 @@ export class BasicController extends BaseController {
|
||||
// opts.verificationCodeLength = 6; //部分厂商这里会设置参数长度这里就不改了
|
||||
}
|
||||
|
||||
await this.codeService.checkCaptcha(body.randomStr, body.imgCode);
|
||||
await this.codeService.sendSmsCode(body.phoneCode, body.mobile, body.randomStr, opts);
|
||||
await this.codeService.checkCaptcha(body.captcha);
|
||||
await this.codeService.sendSmsCode(body.phoneCode, body.mobile, opts);
|
||||
return this.ok(null);
|
||||
}
|
||||
|
||||
@@ -88,16 +94,10 @@ export class BasicController extends BaseController {
|
||||
opts.verificationCodeLength = 6;
|
||||
}
|
||||
|
||||
await this.codeService.checkCaptcha(body.randomStr, body.imgCode);
|
||||
await this.codeService.sendEmailCode(body.email, body.randomStr, opts);
|
||||
await this.codeService.checkCaptcha(body.captcha);
|
||||
await this.codeService.sendEmailCode(body.email, opts);
|
||||
// 设置缓存内容
|
||||
return this.ok(null);
|
||||
}
|
||||
|
||||
@Get('/captcha', { summary: Constants.per.guest })
|
||||
public async getCaptcha(@Query('randomStr') randomStr: any) {
|
||||
const captcha = await this.codeService.generateCaptcha(randomStr);
|
||||
this.ctx.res.setHeader('Content-Type', 'image/svg+xml');
|
||||
return captcha.data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {ALL, Body, Controller, Inject, Post, Provide, Query} from '@midwayjs/core';
|
||||
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
|
||||
import {
|
||||
CrudController,
|
||||
SysPrivateSettings,
|
||||
@@ -6,14 +6,14 @@ import {
|
||||
SysSafeSetting,
|
||||
SysSettingsEntity,
|
||||
SysSettingsService
|
||||
} from '@certd/lib-server';
|
||||
import {cloneDeep, merge} from 'lodash-es';
|
||||
import {PipelineService} from '../../../modules/pipeline/service/pipeline-service.js';
|
||||
import {UserSettingsService} from '../../../modules/mine/service/user-settings-service.js';
|
||||
import {getEmailSettings} from '../../../modules/sys/settings/fix.js';
|
||||
import {http, logger, simpleNanoId, utils} from '@certd/basic';
|
||||
import {CodeService} from '../../../modules/basic/service/code-service.js';
|
||||
import {SmsServiceFactory} from '../../../modules/basic/sms/factory.js';
|
||||
} from "@certd/lib-server";
|
||||
import { cloneDeep, merge } from "lodash-es";
|
||||
import { PipelineService } from "../../../modules/pipeline/service/pipeline-service.js";
|
||||
import { UserSettingsService } from "../../../modules/mine/service/user-settings-service.js";
|
||||
import { getEmailSettings } from "../../../modules/sys/settings/fix.js";
|
||||
import { http, logger, utils } from "@certd/basic";
|
||||
import { CodeService } from "../../../modules/basic/service/code-service.js";
|
||||
import { SmsServiceFactory } from "../../../modules/basic/sms/factory.js";
|
||||
|
||||
|
||||
/**
|
||||
@@ -158,7 +158,7 @@ export class SysSettingsController extends CrudController<SysSettingsService> {
|
||||
|
||||
@Post('/testSms', { summary: 'sys:settings:edit' })
|
||||
async testSms(@Body(ALL) body) {
|
||||
await this.codeService.sendSmsCode(body.phoneCode, body.mobile, simpleNanoId());
|
||||
await this.codeService.sendSmsCode(body.phoneCode, body.mobile );
|
||||
return this.ok({});
|
||||
}
|
||||
|
||||
|
||||
@@ -29,25 +29,23 @@ export class LoginController extends BaseController {
|
||||
throw new CommonException('暂未开启自助找回');
|
||||
}
|
||||
// 找回密码的验证码允许错误次数
|
||||
const errorNum = 5;
|
||||
const maxErrorCount = 5;
|
||||
|
||||
if(body.type === 'email') {
|
||||
this.codeService.checkEmailCode({
|
||||
verificationType: 'forgotPassword',
|
||||
email: body.input,
|
||||
randomStr: body.randomStr,
|
||||
validateCode: body.validateCode,
|
||||
errorNum,
|
||||
maxErrorCount: maxErrorCount,
|
||||
throwError: true,
|
||||
});
|
||||
} else if(body.type === 'mobile') {
|
||||
await this.codeService.checkSmsCode({
|
||||
verificationType: 'forgotPassword',
|
||||
mobile: body.input,
|
||||
randomStr: body.randomStr,
|
||||
phoneCode: body.phoneCode,
|
||||
smsCode: body.validateCode,
|
||||
errorNum,
|
||||
maxErrorCount: maxErrorCount,
|
||||
throwError: true,
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -3,8 +3,7 @@ import { LoginService } from "../../../modules/login/service/login-service.js";
|
||||
import { AddonService, BaseController, Constants, SysPublicSettings, SysSettingsService } from "@certd/lib-server";
|
||||
import { CodeService } from "../../../modules/basic/service/code-service.js";
|
||||
import { checkComm } from "@certd/plus-core";
|
||||
import { logger } from "@certd/basic";
|
||||
import { ICaptchaAddon } from "../../../plugins/plugin-captcha/api.js";
|
||||
import { CaptchaService } from "../../../modules/basic/service/captcha-service.js";
|
||||
|
||||
/**
|
||||
*/
|
||||
@@ -21,12 +20,18 @@ export class LoginController extends BaseController {
|
||||
@Inject()
|
||||
addonService: AddonService;
|
||||
|
||||
@Inject()
|
||||
captchaService: CaptchaService;
|
||||
|
||||
@Post('/login', { summary: Constants.per.guest })
|
||||
public async login(
|
||||
@Body(ALL)
|
||||
body: any
|
||||
) {
|
||||
await this.loginService.doCaptchaValidate({form:body.captcha})
|
||||
const settings = await this.sysSettingsService.getPublicSettings()
|
||||
if (settings.captchaEnabled === true) {
|
||||
await this.captchaService.doValidate({form:body.captcha,must:false,captchaAddonId:settings.captchaAddonId})
|
||||
}
|
||||
const token = await this.loginService.loginByPassword(body);
|
||||
this.writeTokenCookie(token);
|
||||
return this.ok(token);
|
||||
@@ -83,24 +88,4 @@ export class LoginController extends BaseController {
|
||||
});
|
||||
return this.ok();
|
||||
}
|
||||
|
||||
@Post('/captcha/getParams', { summary: Constants.per.guest })
|
||||
async getCaptchaParams() {
|
||||
|
||||
const settings = await this.sysSettingsService.getPublicSettings()
|
||||
if (settings.captchaEnabled) {
|
||||
const addonId = settings.captchaAddonId;
|
||||
|
||||
const addon:ICaptchaAddon = await this.addonService.getAddonById(addonId,true,0)
|
||||
if (!addon) {
|
||||
logger.warn('验证码插件还未配置')
|
||||
return this.ok({});
|
||||
}
|
||||
|
||||
const params = await addon.getClientParams()
|
||||
return this.ok(params);
|
||||
}
|
||||
|
||||
return this.ok({});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ export type RegisterReq = {
|
||||
phoneCode?: string;
|
||||
|
||||
validateCode: string;
|
||||
imgCode: string;
|
||||
randomStr: string;
|
||||
captcha:any;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -52,7 +51,7 @@ export class RegisterController extends BaseController {
|
||||
throw new Error('用户名不能为空');
|
||||
}
|
||||
|
||||
await this.codeService.checkCaptcha(body.randomStr, body.imgCode);
|
||||
await this.codeService.checkCaptcha(body.captcha);
|
||||
const newUser = await this.userService.register(body.type, {
|
||||
username: body.username,
|
||||
password: body.password,
|
||||
@@ -68,7 +67,6 @@ export class RegisterController extends BaseController {
|
||||
mobile: body.mobile,
|
||||
phoneCode: body.phoneCode,
|
||||
smsCode: body.validateCode,
|
||||
randomStr: body.randomStr,
|
||||
throwError: true,
|
||||
});
|
||||
const newUser = await this.userService.register(body.type, {
|
||||
@@ -85,7 +83,6 @@ export class RegisterController extends BaseController {
|
||||
checkPlus();
|
||||
this.codeService.checkEmailCode({
|
||||
email: body.email,
|
||||
randomStr: body.randomStr,
|
||||
validateCode: body.validateCode,
|
||||
throwError: true,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
||||
import { AddonService, SysSettingsService } from "@certd/lib-server";
|
||||
import { logger } from "@certd/basic";
|
||||
import { ICaptchaAddon } from "../../../plugins/plugin-captcha/api.js";
|
||||
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
||||
export class CaptchaService {
|
||||
@Inject()
|
||||
sysSettingsService: SysSettingsService;
|
||||
@Inject()
|
||||
addonService: AddonService;
|
||||
|
||||
|
||||
async getCaptcha(captchaAddonId?:number){
|
||||
if (!captchaAddonId) {
|
||||
const settings = await this.sysSettingsService.getPublicSettings()
|
||||
captchaAddonId = settings.captchaAddonId ?? 0
|
||||
}
|
||||
const addon:ICaptchaAddon = await this.addonService.getAddonById(captchaAddonId,true,0)
|
||||
if (!addon) {
|
||||
throw new Error('验证码插件还未配置')
|
||||
}
|
||||
return await addon.getCaptcha()
|
||||
}
|
||||
|
||||
|
||||
async doValidate(opts:{form:any,must?:boolean,captchaAddonId?:number}){
|
||||
if (!opts.captchaAddonId) {
|
||||
const settings = await this.sysSettingsService.getPublicSettings()
|
||||
opts.captchaAddonId = settings.captchaAddonId ?? 0
|
||||
}
|
||||
const addon = await this.addonService.getById(opts.captchaAddonId,0)
|
||||
if (!addon) {
|
||||
if (opts.must) {
|
||||
throw new Error('请先配置验证码插件');
|
||||
}
|
||||
logger.warn('验证码插件还未配置,忽略验证码校验')
|
||||
return true
|
||||
}
|
||||
|
||||
if (!opts.form) {
|
||||
throw new Error('请输入验证码');
|
||||
}
|
||||
const res = await addon.onValidate(opts.form)
|
||||
if (!res) {
|
||||
throw new Error('验证码错误');
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import { EmailService } from './email-service.js';
|
||||
import { AccessService } from '@certd/lib-server';
|
||||
import { AccessSysGetter } from '@certd/lib-server';
|
||||
import { isComm } from '@certd/plus-core';
|
||||
import { CaptchaService } from "./captcha-service.js";
|
||||
|
||||
// {data: '<svg.../svg>', text: 'abcd'}
|
||||
/**
|
||||
@@ -23,44 +24,19 @@ export class CodeService {
|
||||
@Inject()
|
||||
accessService: AccessService;
|
||||
|
||||
/**
|
||||
*/
|
||||
async generateCaptcha(randomStr) {
|
||||
const svgCaptcha = await import('svg-captcha');
|
||||
const c = svgCaptcha.create();
|
||||
//{data: '<svg.../svg>', text: 'abcd'}
|
||||
const imgCode = c.text; // = RandomUtil.randomStr(4, true);
|
||||
cache.set('imgCode:' + randomStr, imgCode, {
|
||||
ttl: 2 * 60 * 1000, //过期时间 2分钟
|
||||
});
|
||||
return c;
|
||||
}
|
||||
@Inject()
|
||||
captchaService: CaptchaService;
|
||||
|
||||
async getCaptchaText(randomStr) {
|
||||
return cache.get('imgCode:' + randomStr);
|
||||
}
|
||||
|
||||
async removeCaptcha(randomStr) {
|
||||
cache.delete('imgCode:' + randomStr);
|
||||
}
|
||||
|
||||
async checkCaptcha(randomStr: string, userCaptcha: string) {
|
||||
const code = await this.getCaptchaText(randomStr);
|
||||
if (code == null) {
|
||||
throw new Error('验证码已过期');
|
||||
}
|
||||
if (code.toLowerCase() !== userCaptcha.toLowerCase()) {
|
||||
throw new Error('验证码不正确');
|
||||
}
|
||||
await this.removeCaptcha(randomStr);
|
||||
return true;
|
||||
async checkCaptcha(body:any) {
|
||||
return await this.captchaService.doValidate({form:body})
|
||||
}
|
||||
/**
|
||||
*/
|
||||
async sendSmsCode(
|
||||
phoneCode = '86',
|
||||
mobile: string,
|
||||
randomStr: string,
|
||||
opts?: {
|
||||
duration?: number,
|
||||
verificationType?: string,
|
||||
@@ -70,9 +46,6 @@ export class CodeService {
|
||||
if (!mobile) {
|
||||
throw new Error('手机号不能为空');
|
||||
}
|
||||
if (!randomStr) {
|
||||
throw new Error('randomStr不能为空');
|
||||
}
|
||||
|
||||
const verificationCodeLength = Math.floor(Math.max(Math.min(opts?.verificationCodeLength || 4, 8), 4));
|
||||
const duration = Math.floor(Math.max(Math.min(opts?.duration || 5, 15), 1));
|
||||
@@ -96,7 +69,7 @@ export class CodeService {
|
||||
phoneCode,
|
||||
});
|
||||
|
||||
const key = this.buildSmsCodeKey(phoneCode, mobile, randomStr, opts?.verificationType);
|
||||
const key = this.buildSmsCodeKey(phoneCode, mobile, opts?.verificationType);
|
||||
cache.set(key, smsCode, {
|
||||
ttl: duration * 60 * 1000, //5分钟
|
||||
});
|
||||
@@ -106,12 +79,10 @@ export class CodeService {
|
||||
/**
|
||||
*
|
||||
* @param email 收件邮箱
|
||||
* @param randomStr
|
||||
* @param opts title标题 content内容模版 duration有效时间单位分钟 verificationType验证类型
|
||||
*/
|
||||
async sendEmailCode(
|
||||
email: string,
|
||||
randomStr: string,
|
||||
opts?: {
|
||||
title?: string,
|
||||
content?: string,
|
||||
@@ -123,9 +94,7 @@ export class CodeService {
|
||||
if (!email) {
|
||||
throw new Error('Email不能为空');
|
||||
}
|
||||
if (!randomStr) {
|
||||
throw new Error('randomStr不能为空');
|
||||
}
|
||||
|
||||
|
||||
let siteTitle = 'Certd';
|
||||
if (isComm()) {
|
||||
@@ -149,7 +118,7 @@ export class CodeService {
|
||||
receivers: [email],
|
||||
});
|
||||
|
||||
const key = this.buildEmailCodeKey(email, randomStr, opts?.verificationType);
|
||||
const key = this.buildEmailCodeKey(email,opts?.verificationType);
|
||||
cache.set(key, code, {
|
||||
ttl: duration * 60 * 1000, //5分钟
|
||||
});
|
||||
@@ -159,31 +128,32 @@ export class CodeService {
|
||||
/**
|
||||
* checkSms
|
||||
*/
|
||||
async checkSmsCode(opts: { mobile: string; phoneCode: string; smsCode: string; randomStr: string; verificationType?: string; throwError: boolean; errorNum?: number }) {
|
||||
const key = this.buildSmsCodeKey(opts.phoneCode, opts.mobile, opts.randomStr, opts.verificationType);
|
||||
if (isDev()) {
|
||||
async checkSmsCode(opts: { mobile: string; phoneCode: string; smsCode: string; verificationType?: string; throwError: boolean; maxErrorCount?: number }) {
|
||||
const key = this.buildSmsCodeKey(opts.phoneCode, opts.mobile, opts.verificationType);
|
||||
return this.checkValidateCode("sms",key, opts.smsCode, opts.throwError, opts.maxErrorCount);
|
||||
|
||||
}
|
||||
|
||||
buildSmsCodeKey(phoneCode: string, mobile: string, verificationType?: string) {
|
||||
return ['sms', verificationType, phoneCode, mobile].filter(item => !!item).join(':');
|
||||
}
|
||||
|
||||
buildEmailCodeKey(email: string, verificationType?: string) {
|
||||
return ['email', verificationType, email].filter(item => !!item).join(':');
|
||||
}
|
||||
checkValidateCode(type:string,key: string, userCode: string, throwError = true, maxErrorCount = 3) {
|
||||
// 记录异常次数key
|
||||
if (isDev() && userCode==="1234567") {
|
||||
return true;
|
||||
}
|
||||
return this.checkValidateCode(key, opts.smsCode, opts.throwError, opts.errorNum);
|
||||
}
|
||||
|
||||
buildSmsCodeKey(phoneCode: string, mobile: string, randomStr: string, verificationType?: string) {
|
||||
return ['sms', verificationType, phoneCode, mobile, randomStr].filter(item => !!item).join(':');
|
||||
}
|
||||
|
||||
buildEmailCodeKey(email: string, randomStr: string, verificationType?: string) {
|
||||
return ['email', verificationType, email, randomStr].filter(item => !!item).join(':');
|
||||
}
|
||||
checkValidateCode(key: string, userCode: string, throwError = true, errorNum = 3) {
|
||||
// 记录异常次数key
|
||||
const err_num_key = key + ':err_num';
|
||||
//验证图片验证码
|
||||
//验证邮件验证码
|
||||
const code = cache.get(key);
|
||||
if (code == null || code !== userCode) {
|
||||
let maxRetryCount = false;
|
||||
if (!!code && errorNum > 0) {
|
||||
if (!!code && maxErrorCount > 0) {
|
||||
const err_num = cache.get(err_num_key) || 0
|
||||
if(err_num >= errorNum - 1) {
|
||||
if(err_num >= maxErrorCount - 1) {
|
||||
maxRetryCount = true;
|
||||
cache.delete(key);
|
||||
cache.delete(err_num_key);
|
||||
@@ -194,7 +164,8 @@ export class CodeService {
|
||||
}
|
||||
}
|
||||
if (throwError) {
|
||||
throw new CodeErrorException(!maxRetryCount ? '验证码错误': '验证码错误请获取新的验证码');
|
||||
const label = type ==='sms' ? '手机' : '邮箱';
|
||||
throw new CodeErrorException(!maxRetryCount ? `${label}验证码错误`: `${label}验证码错误请获取新的验证码`);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -203,9 +174,9 @@ export class CodeService {
|
||||
return true;
|
||||
}
|
||||
|
||||
checkEmailCode(opts: { randomStr: string; validateCode: string; email: string; verificationType?: string; throwError: boolean; errorNum?: number }) {
|
||||
const key = this.buildEmailCodeKey(opts.email, opts.randomStr, opts.verificationType);
|
||||
return this.checkValidateCode(key, opts.validateCode, opts.throwError, opts.errorNum);
|
||||
checkEmailCode(opts: { validateCode: string; email: string; verificationType?: string; throwError: boolean; maxErrorCount?: number }) {
|
||||
const key = this.buildEmailCodeKey(opts.email, opts.verificationType);
|
||||
return this.checkValidateCode('email',key, opts.validateCode, opts.throwError, opts.maxErrorCount);
|
||||
}
|
||||
|
||||
compile(templateString: string) {
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
import {Config, Inject, Provide, Scope, ScopeEnum} from '@midwayjs/core';
|
||||
import {UserService} from '../../sys/authority/service/user-service.js';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import {AuthException, CommonException, Need2FAException} from "@certd/lib-server";
|
||||
import {RoleService} from '../../sys/authority/service/role-service.js';
|
||||
import {UserEntity} from '../../sys/authority/entity/user.js';
|
||||
import {SysSettingsService} from '@certd/lib-server';
|
||||
import {SysPrivateSettings} from '@certd/lib-server';
|
||||
import { cache, logger, utils } from "@certd/basic";
|
||||
import {LoginErrorException} from '@certd/lib-server/dist/basic/exception/login-error-exception.js';
|
||||
import {CodeService} from '../../basic/service/code-service.js';
|
||||
import {TwoFactorService} from "../../mine/service/two-factor-service.js";
|
||||
import {UserSettingsService} from '../../mine/service/user-settings-service.js';
|
||||
import {isPlus} from "@certd/plus-core";
|
||||
import { Config, Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
||||
import { UserService } from "../../sys/authority/service/user-service.js";
|
||||
import jwt from "jsonwebtoken";
|
||||
import {
|
||||
AuthException,
|
||||
CommonException,
|
||||
Need2FAException,
|
||||
SysPrivateSettings,
|
||||
SysSettingsService
|
||||
} from "@certd/lib-server";
|
||||
import { RoleService } from "../../sys/authority/service/role-service.js";
|
||||
import { UserEntity } from "../../sys/authority/entity/user.js";
|
||||
import { cache, utils } from "@certd/basic";
|
||||
import { LoginErrorException } from "@certd/lib-server/dist/basic/exception/login-error-exception.js";
|
||||
import { CodeService } from "../../basic/service/code-service.js";
|
||||
import { TwoFactorService } from "../../mine/service/two-factor-service.js";
|
||||
import { UserSettingsService } from "../../mine/service/user-settings-service.js";
|
||||
import { isPlus } from "@certd/plus-core";
|
||||
import { AddonService } from "@certd/lib-server/dist/user/addon/service/addon-service.js";
|
||||
|
||||
/**
|
||||
@@ -100,33 +104,6 @@ export class LoginService {
|
||||
throw new LoginErrorException(errorMessage, leftTimes);
|
||||
}
|
||||
|
||||
async doCaptchaValidate(opts:{form:any}){
|
||||
|
||||
const pubSetting = await this.sysSettingsService.getPublicSettings()
|
||||
|
||||
if (pubSetting.captchaEnabled) {
|
||||
|
||||
const addon = await this.addonService.getById(pubSetting.captchaAddonId,0)
|
||||
if (!addon) {
|
||||
logger.warn('验证码插件还未配置,忽略验证码校验')
|
||||
return true
|
||||
}
|
||||
if (addon.define.name !== pubSetting.captchaType) {
|
||||
logger.warn('验证码插件类型错误,忽略验证码校验')
|
||||
return true
|
||||
}
|
||||
|
||||
const res = await addon.onValidate(opts.form)
|
||||
if (!res) {
|
||||
throw new Error('验证码错误');
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
async loginBySmsCode(req: { mobile: string; phoneCode: string; smsCode: string; randomStr: string }) {
|
||||
|
||||
@@ -136,13 +113,12 @@ export class LoginService {
|
||||
mobile: req.mobile,
|
||||
phoneCode: req.phoneCode,
|
||||
smsCode: req.smsCode,
|
||||
randomStr: req.randomStr,
|
||||
throwError: false,
|
||||
});
|
||||
|
||||
const {mobile, phoneCode} = req;
|
||||
if (!smsChecked) {
|
||||
this.addErrorTimes(mobile, '验证码错误');
|
||||
this.addErrorTimes(mobile, '手机验证码错误');
|
||||
}
|
||||
let info = await this.userService.findOne({phoneCode, mobile: mobile});
|
||||
if (info == null) {
|
||||
|
||||
@@ -238,9 +238,12 @@ export class UserService extends BaseService<UserEntity> {
|
||||
|
||||
async forgotPassword(
|
||||
data: {
|
||||
type: ForgotPasswordType; input?: string, phoneCode?: string,
|
||||
randomStr: string, imgCode:string, validateCode: string,
|
||||
password: string, confirmPassword: string,
|
||||
type: ForgotPasswordType;
|
||||
input?: string,
|
||||
phoneCode?: string,
|
||||
validateCode: string,
|
||||
password: string,
|
||||
confirmPassword: string,
|
||||
}
|
||||
) {
|
||||
if(!data.type) {
|
||||
@@ -249,7 +252,13 @@ export class UserService extends BaseService<UserEntity> {
|
||||
if(data.password !== data.confirmPassword) {
|
||||
throw new CommonException('两次输入的密码不一致');
|
||||
}
|
||||
const user = await this.findOne([{ [data.type]: data.input }]);
|
||||
const where :any= {
|
||||
[data.type]: data.input,
|
||||
};
|
||||
if (data.type === 'mobile' ) {
|
||||
where.phoneCode = data.phoneCode ?? '86';
|
||||
}
|
||||
const user = await this.findOne({ [data.type]: data.input });
|
||||
console.log('user', user)
|
||||
if(!user) {
|
||||
throw new CommonException('用户不存在');
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export interface ICaptchaAddon{
|
||||
onValidate(data?:any):Promise<any>;
|
||||
getClientParams():Promise<any>;
|
||||
getCaptcha():Promise<any>;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { AddonInput, BaseAddon, IsAddon } from "@certd/lib-server";
|
||||
import crypto from 'crypto';
|
||||
import crypto from "crypto";
|
||||
import { ICaptchaAddon } from "../api.js";
|
||||
|
||||
@IsAddon({
|
||||
addonType:"captcha",
|
||||
name: 'geetest',
|
||||
@@ -8,6 +9,7 @@ import { ICaptchaAddon } from "../api.js";
|
||||
desc: '',
|
||||
})
|
||||
export class GeeTestCaptcha extends BaseAddon implements ICaptchaAddon{
|
||||
|
||||
@AddonInput({
|
||||
title: 'captchaId',
|
||||
component: {
|
||||
@@ -28,7 +30,9 @@ export class GeeTestCaptcha extends BaseAddon implements ICaptchaAddon{
|
||||
|
||||
|
||||
async onValidate(data?:any) {
|
||||
|
||||
if (!data) {
|
||||
return false
|
||||
}
|
||||
// geetest 服务地址
|
||||
// geetest server url
|
||||
const API_SERVER = "http://gcaptcha4.geetest.com";
|
||||
@@ -107,11 +111,10 @@ export class GeeTestCaptcha extends BaseAddon implements ICaptchaAddon{
|
||||
return result;
|
||||
}
|
||||
|
||||
async getClientParams(): Promise<any> {
|
||||
async getCaptcha(): Promise<any> {
|
||||
return {
|
||||
captchaId: this.captchaId,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { AddonInput, BaseAddon, IsAddon } from "@certd/lib-server";
|
||||
import crypto from 'crypto';
|
||||
import { BaseAddon, IsAddon } from "@certd/lib-server";
|
||||
import { ICaptchaAddon } from "../api.js";
|
||||
import { cache } from "@certd/basic";
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
@IsAddon({
|
||||
addonType:"captcha",
|
||||
name: 'image',
|
||||
@@ -9,42 +11,45 @@ import { ICaptchaAddon } from "../api.js";
|
||||
})
|
||||
export class ImageCaptcha extends BaseAddon implements ICaptchaAddon{
|
||||
|
||||
|
||||
|
||||
|
||||
async onValidate(data?:any) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 生成签名
|
||||
// Generate signature
|
||||
hmac_sha256_encode(value, key){
|
||||
var hash = crypto.createHmac("sha256", key)
|
||||
.update(value, 'utf8')
|
||||
.digest('hex');
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
// 发送post请求, 响应json数据如:{"result": "success", "reason": "", "captcha_args": {}}
|
||||
// Send a post request and respond to JSON data, such as: {result ":" success "," reason ":" "," captcha_args ": {}}
|
||||
async doRequest(datas, url){
|
||||
var options = {
|
||||
url: url,
|
||||
method: "POST",
|
||||
params: datas,
|
||||
timeout: 5000
|
||||
};
|
||||
const result = await this.ctx.http.request(options);
|
||||
return result;
|
||||
}
|
||||
|
||||
async getClientParams(): Promise<any> {
|
||||
return {
|
||||
captchaId: this.captchaId,
|
||||
if (!data) {
|
||||
return false;
|
||||
}
|
||||
return await this.checkCaptcha(data.randomStr, data.imageCode)
|
||||
}
|
||||
|
||||
async getCaptchaText(randomStr:string) {
|
||||
return cache.get('imgCode:' + randomStr);
|
||||
}
|
||||
|
||||
async removeCaptcha(randomStr:string) {
|
||||
cache.delete('imgCode:' + randomStr);
|
||||
}
|
||||
|
||||
async checkCaptcha(randomStr: string, userCaptcha: string) {
|
||||
const code = await this.getCaptchaText(randomStr);
|
||||
if (code == null) {
|
||||
throw new Error('验证码已过期');
|
||||
}
|
||||
if (code.toLowerCase() !== userCaptcha?.toLowerCase()) {
|
||||
throw new Error('验证码不正确');
|
||||
}
|
||||
await this.removeCaptcha(randomStr);
|
||||
return true;
|
||||
}
|
||||
|
||||
async getCaptcha(): Promise<any> {
|
||||
const svgCaptcha = await import('svg-captcha');
|
||||
const c = svgCaptcha.create();
|
||||
//{data: '<svg.../svg>', text: 'abcd'}
|
||||
const imgCode = c.text; // = RandomUtil.randomStr(4, true);
|
||||
const randomStr = nanoid(10)
|
||||
cache.set('imgCode:' + randomStr, imgCode, {
|
||||
ttl: 2 * 60 * 1000, //过期时间 2分钟
|
||||
})
|
||||
return {
|
||||
randomStr: randomStr,
|
||||
imageData: c.data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export * from './geetest/index.js';
|
||||
export * from './image/index.js';
|
||||
|
||||
Reference in New Issue
Block a user