package service import ( "bytes" "github.com/dchest/captcha" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" "net/http" "texas-poker-bk/internal/conf" "time" ) func Avatar(ctx *gin.Context) { avatar := ctx.Param("avatar") ctx.Header("Cache-Control", "private, max-age=86400") ctx.File(conf.Conf.Game.AvatarPath + avatar) } // Captcha 获取验证码 func Captcha(ctx *gin.Context) { l, w, h := 4, 107, 36 captchaId := captcha.NewLen(l) session := sessions.Default(ctx) session.Set("captcha", captchaId) _ = session.Save() _ = Serve(ctx.Writer, ctx.Request, captchaId, ".png", "zh", false, w, h) } func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error { w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") w.Header().Set("Pragma", "no-cache") w.Header().Set("Expires", "0") var content bytes.Buffer switch ext { case ".png": w.Header().Set("Content-Type", "image/png") _ = captcha.WriteImage(&content, id, width, height) // 音频验证码 case ".wav": w.Header().Set("Content-Type", "audio/x-wav") _ = captcha.WriteAudio(&content, id, lang) default: return captcha.ErrNotFound } if download { w.Header().Set("Content-Type", "application/octet-stream") } http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes())) return nil }