先安装 svg-captcha
依赖,创建云函数 get-captcha
如下:
import cloud from '@lafjs/cloud'
import captcha from 'svg-captcha'
export async function main(ctx: FunctionContext) {
const options = {
size: 4,
ignoreChars: '0oO1IiLl',
noise: 1,
color: true,
background: '#EEE',
charPreset: "12345689",
}
const captchaData = captcha.create(options)
const db = cloud.database()
await db.collection('captcha')
.add({
code: captchaData.text.toLocaleLowerCase(),
createdAt: new Date()
})
ctx.response.type('svg').send(captchaData.data)
}
效果如下