31 lines
623 B
TypeScript
31 lines
623 B
TypeScript
import { CaptchaObj, createMathExpr } from 'svg-captcha'
|
|
|
|
/**
|
|
* 生成验证码
|
|
* @return {CaptchaObj}
|
|
*/
|
|
export function generate_captcha(): CaptchaObj {
|
|
return createMathExpr({ color: true })
|
|
}
|
|
|
|
/**
|
|
* 检查验证码
|
|
* @param req
|
|
* @param captcha
|
|
* @param stored_captcha
|
|
* @param stamp
|
|
* @return {boolean}
|
|
*/
|
|
export function validate_captcha(req, captcha, stored_captcha, stamp): boolean {
|
|
if (!stored_captcha || !captcha) {
|
|
return false
|
|
}
|
|
if (!stamp) {
|
|
return false
|
|
}
|
|
if (Date.now() - stamp > 600000) {
|
|
return false
|
|
}
|
|
return stored_captcha.toUpperCase() === captcha.toUpperCase()
|
|
}
|