package main
import (
"github.com/kataras/iris/v12"
)
const (
StdWidth = 80
StdHeight = 40
)
func main() {
app := iris.New()
app.Get("/captcha", getCaptchaId)
app.Get("/captcha/*", getCaptchaImg)
app.Run(“:8080”)
}
func getCaptchaId(ctx *context.Context) {
m := make(map[string]interface{}, 0)
m["status"] = 0
m["httpCode"] = 200
m["message"] = "获取成功"
m["captchaId"] = captcha.NewLen(4)
ctx.JSON(m)
return
}
func getCaptchaImg(ctx *context.Context) {
captcha.Server(StdWidth, StdHeight).
ServeHTTP(ctx.ResponseWriter(), ctx.Request())
}
//客户端获取图片 captchaId.png
http://localhost:8080/captcha/Ti0JrgVuU6j6TnStAtDU.png
//获取id 如下
http://localhost:8080/captcha
{
captchaId: "Sx8ftVFYXrajbuOyO2j6",
httpCode: 200,
message: "获取成功",
status: 0
}