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
| const express = require('express') const router = express.Router()
app.use(express.json()) app.use(express.urlencoded({extended:false}))
const svgCaptcha = require('svg-captcha')
const cookieParser = require('cookie-parser') router.use(cookieParser())
router.get('/captcha', (req, res) => { const captcha = svgCaptcha.create({ inverse: false, fontSize: 48, noise: 2, width: req.query.width || 150, height: req.query.height || 50, size: 4, ignoreChars: '0o1i', color: true, background: '#cc9966', }) res.cookie('captcha', captcha.text.toLowerCase()) res.type('svg') res.send(captcha.data) })
router.post('/login', (req, res) => { const { account, password, captcha } = req.body if (captcha.toLowerCase() !== req.cookies.captcha) { res.send({ success: false, code: 0, msg: '验证码输入错误', data: null }) return } })
|