game_demo/api/auth.go

55 lines
1.1 KiB
Go

package api
import (
"game_demo/models"
"game_demo/pkg/e"
"game_demo/pkg/logging"
"game_demo/pkg/util"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"net/http"
)
type auth struct {
Username string `valid:"Required; MaxSize(50)"`
Password string `valid:"Required; MaxSize(50)"`
}
func GetAuth(c *gin.Context) {
username := c.Query("username")
password := c.Query("password")
valid := validation.Validation{}
a := auth{Username : username, Password : password}
ok, err := valid.Valid(&a)
logging.Info(err)
data := make(map[string]interface{})
code := e.INVALID_PARAMS
if ok {
isExist := models.CheckAuth(username, password)
if isExist {
token, err := util.GenerateToken(username, password)
if err != nil {
code = e.ERROR_AUTH_TOKEN
} else {
data["token"] = token
code = e.SUCCESS
}
} else {
code = e.ERROR_AUTH
}
} else {
for _, err := range valid.Errors {
logging.Info(err.Key, err.Message)
}
}
c.JSON(http.StatusOK, gin.H{
"code": code,
"msg" : e.GetMsg(code),
"data" : data,
})
}