54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package admin
|
|
|
|
import (
|
|
"github.com/astaxie/beego/validation"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-pripro/shop/models"
|
|
"github.com/go-pripro/shop/pkg/app"
|
|
"github.com/go-pripro/shop/pkg/e"
|
|
"github.com/go-pripro/shop/pkg/util"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"net/http"
|
|
)
|
|
|
|
type Employee struct {
|
|
Username string `valid:"Required; MaxSize(50)"`
|
|
Password string `valid:"Required; MaxSize(50)"`
|
|
}
|
|
|
|
// @Summary 后台登录
|
|
// @Produce json
|
|
// @Param username query string true "用户名"
|
|
// @Param password query string true "密码"
|
|
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
|
|
// @Router /admin/login [get]
|
|
func GetLogin(c *gin.Context) {
|
|
appG := app.Gin{C: c}
|
|
username := c.Query("username")
|
|
password := c.Query("password")
|
|
valid := validation.Validation{}
|
|
a := Employee{Username: username, Password: password}
|
|
|
|
if ok, err := valid.Valid(&a); err != nil || !ok {
|
|
app.MarkErrors(valid.Errors)
|
|
appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
|
|
return
|
|
}
|
|
|
|
data := make(map[string]interface{})
|
|
if fEmployee, err := models.GetEmployeeByUsername(username); err != nil {
|
|
appG.Response(http.StatusInternalServerError, e.ErrorAuth, nil)
|
|
} else {
|
|
if err := bcrypt.CompareHashAndPassword([]byte(fEmployee.Password), []byte(password)); err != nil{
|
|
appG.Response(http.StatusInternalServerError, e.ErrorAuth, nil)
|
|
} else {
|
|
token, err := util.GenerateToken(username, password)
|
|
if err != nil {
|
|
appG.Response(http.StatusInternalServerError, e.ErrorAuthToken, nil)
|
|
} else {
|
|
data["token"] = token
|
|
appG.Response(http.StatusOK, e.SUCCESS, data)
|
|
}
|
|
}
|
|
}
|
|
} |