diff --git a/app/controllers/userController/auth.go b/app/controllers/userController/auth.go index 9165da4..ac077f6 100644 --- a/app/controllers/userController/auth.go +++ b/app/controllers/userController/auth.go @@ -2,6 +2,7 @@ package userController import ( "wejh-go/app/apiException" + "wejh-go/app/models" "wejh-go/app/services/sessionServices" "wejh-go/app/services/userServices" "wejh-go/app/utils" @@ -12,23 +13,21 @@ import ( ) type autoLoginForm struct { - Code string `json:"code" binding:"required"` - LoginType string `json:"type"` + Code string `json:"code" binding:"required"` } type passwordLoginForm struct { - Username string `json:"username" binding:"required"` - Password string `json:"password" binding:"required"` - LoginType string `json:"type"` + Username string `json:"username" binding:"required"` + Password string `json:"password" binding:"required"` } func AuthByPassword(c *gin.Context) { var postForm passwordLoginForm err := c.ShouldBindJSON(&postForm) - if err != nil { _ = c.AbortWithError(200, apiException.ParamError) return } + user, err := userServices.GetUserByUsername(postForm.Username) if err == gorm.ErrRecordNotFound { _ = c.AbortWithError(200, apiException.UserNotFind) @@ -39,7 +38,11 @@ func AuthByPassword(c *gin.Context) { return } - err = userServices.CheckLogin(postForm.Username,postForm.Password) + if user.Type != models.Postgraduate && user.Type != models.Undergraduate { + err = userServices.CheckLocalLogin(user, postForm.Password) + } else { + err = userServices.CheckLogin(postForm.Username, postForm.Password) + } if err != nil { _ = c.AbortWithError(200, err) return diff --git a/app/services/userServices/check.go b/app/services/userServices/check.go index ea5ff5a..fc41e18 100644 --- a/app/services/userServices/check.go +++ b/app/services/userServices/check.go @@ -1,17 +1,34 @@ package userServices +import ( + "crypto/sha256" + "encoding/hex" + "wejh-go/app/apiException" + "wejh-go/app/models" + "wejh-go/app/services/userCenterServices" +) + func CheckUsername(username string) bool { user, _ := GetUserByUsername(username) - if user != nil { - return true - } - return false + return user != nil } func CheckWechatOpenID(wechatOpenID string) bool { user := GetUserByWechatOpenID(wechatOpenID) - if user != nil { - return false + return user != nil +} + +func CheckLogin(username, password string) error { + return userCenterServices.Login(username, password) +} + +func CheckLocalLogin(user *models.User, password string) error { + h := sha256.New() + h.Write([]byte(password)) + pass := hex.EncodeToString(h.Sum(nil)) + + if user.JHPassword != pass { + return apiException.NoThatPasswordOrWrong } - return true + return nil } diff --git a/app/services/userServices/create.go b/app/services/userServices/create.go index 514699a..2147407 100755 --- a/app/services/userServices/create.go +++ b/app/services/userServices/create.go @@ -73,7 +73,3 @@ func CreateAdmin(userName, password string, adminType int) error { res := database.DB.Create(&admin) return res.Error } - -func CheckLogin(username, password string) error { - return userCenterServices.Login(username, password) -} \ No newline at end of file