Skip to content

Commit

Permalink
Merge pull request #83 from SugarMGP/main
Browse files Browse the repository at this point in the history
feat: 根据用户类型切换登陆方式
  • Loading branch information
cbluebird authored Oct 18, 2024
2 parents 54c0d30 + 638d3d8 commit c6d74cf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
17 changes: 10 additions & 7 deletions app/controllers/userController/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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
Expand Down
31 changes: 24 additions & 7 deletions app/services/userServices/check.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 0 additions & 4 deletions app/services/userServices/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit c6d74cf

Please sign in to comment.