You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/internal/routers/api/home.go

102 lines
2.6 KiB

3 years ago
package api
import (
"bytes"
"encoding/base64"
"image/color"
"image/png"
"time"
"github.com/afocus/captcha"
"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
"github.com/rocboss/paopao-ce/global"
"github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/internal/service"
"github.com/rocboss/paopao-ce/pkg/app"
"github.com/rocboss/paopao-ce/pkg/convert"
"github.com/rocboss/paopao-ce/pkg/errcode"
"github.com/rocboss/paopao-ce/pkg/util"
3 years ago
)
const MAX_PHONE_CAPTCHA = 10
func Version(c *gin.Context) {
response := app.NewResponse(c)
response.ToResponse(gin.H{
"version": "PaoPao Service v1.0",
})
}
func SyncSearchIndex(c *gin.Context) {
response := app.NewResponse(c)
user, _ := c.Get("USER")
if user.(*model.User).IsAdmin {
go service.PushPostsToSearch(c)
3 years ago
}
response.ToResponse(nil)
}
func GetCaptcha(c *gin.Context) {
cap := captcha.New()
3 years ago
if err := cap.SetFont("assets/comic.ttf"); err != nil {
3 years ago
panic(err.Error())
}
cap.SetSize(160, 64)
cap.SetDisturbance(captcha.MEDIUM)
cap.SetFrontColor(color.RGBA{0, 0, 0, 255})
cap.SetBkgColor(color.RGBA{218, 240, 228, 255})
img, password := cap.Create(6, captcha.NUM)
emptyBuff := bytes.NewBuffer(nil)
_ = png.Encode(emptyBuff, img)
key := util.EncodeMD5(uuid.Must(uuid.NewV4()).String())
// 五分钟有效期
global.Redis.SetEX(c, "PaoPaoCaptcha:"+key, password, time.Minute*5)
response := app.NewResponse(c)
response.ToResponse(gin.H{
"id": key,
"b64s": "data:image/png;base64," + base64.StdEncoding.EncodeToString(emptyBuff.Bytes()),
})
}
func PostCaptcha(c *gin.Context) {
param := service.PhoneCaptchaReq{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
// 验证图片验证码
if res, err := global.Redis.Get(c.Request.Context(), "PaoPaoCaptcha:"+param.ImgCaptchaID).Result(); err != nil || res != param.ImgCaptcha {
response.ToErrorResponse(errcode.ErrorCaptchaPassword)
return
}
global.Redis.Del(c.Request.Context(), "PaoPaoCaptcha:"+param.ImgCaptchaID).Result()
// 今日频次限制
if res, _ := global.Redis.Get(c.Request.Context(), "PaoPaoSmsCaptcha:"+param.Phone).Result(); convert.StrTo(res).MustInt() >= MAX_PHONE_CAPTCHA {
response.ToErrorResponse(errcode.TooManyPhoneCaptchaSend)
return
}
err := service.SendPhoneCaptcha(c, param.Phone)
3 years ago
if err != nil {
global.Logger.Errorf("app.SendPhoneCaptcha errs: %v", errs)
response.ToErrorResponse(errcode.GetPhoneCaptchaError)
return
}
response.ToResponse(nil)
}