52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"blog/global"
|
||
|
"blog/internal/service"
|
||
|
"blog/pkg/app"
|
||
|
"blog/pkg/errorcode"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
type res struct {
|
||
|
token string
|
||
|
}
|
||
|
|
||
|
// @Summary 获取token
|
||
|
// @Produce json
|
||
|
// @Param app_key formData string true "appkey" required
|
||
|
// @Param app_secret formData string true "appsecret" required
|
||
|
// @Success 200 {object} res{token=string} "成功"
|
||
|
// @Failure 400 {object} errorcode.Error "请求错误"
|
||
|
// @Failure 500 {object} errorcode.Error "内部错误"
|
||
|
// @Router /auth [post]
|
||
|
func GetAuth(c *gin.Context) {
|
||
|
param := service.AuthRequest{}
|
||
|
response := app.NewResponse(c)
|
||
|
valid, errs := app.BindAndValid(c, ¶m)
|
||
|
if !valid {
|
||
|
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
|
||
|
response.ToErrorResponse(errorcode.InvalidParams.WithDetails(errs.Errors()...))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
svc := service.New(c.Request.Context())
|
||
|
err := svc.CheckAuth(¶m)
|
||
|
if err != nil {
|
||
|
global.Logger.Errorf("svc.CheckAuth err: %v", err)
|
||
|
response.ToErrorResponse(errorcode.UnauthorizedAuthNotExist)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
token, err := app.GenerateToken(param.AppKey, param.AppSecret)
|
||
|
if err != nil {
|
||
|
global.Logger.Errorf("app.GenerateToken err: %v", err)
|
||
|
response.ToErrorResponse(errorcode.UnauthorizedTokenGenerate)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponse(gin.H{
|
||
|
"token": token,
|
||
|
})
|
||
|
}
|