Skip to content

Commit

Permalink
Merge pull request #18 from SugarMGP/dev
Browse files Browse the repository at this point in the history
feat: 普通用户修改密码接口
  • Loading branch information
Penryn authored Nov 22, 2024
2 parents 6579b1a + 2901f31 commit bcd1284
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/controllers/userController/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package userController

import (
"errors"

"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/userCenterService"
"4u-go/app/services/userService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type changePasswordData struct {
StudentId string `json:"student_id" binding:"required"`
IdentityId string `json:"identity_id" binding:"required"`
Password string `json:"password" binding:"required"`
}

// ChangePassword 修改密码接口
func ChangePassword(c *gin.Context) {
var data changePasswordData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

user, err := userService.GetUserByStudentID(data.StudentId)
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.UserNotFound, err)
return
}
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

// 若不是普通用户则提示不存在
if user.Type != models.Undergraduate && user.Type != models.Postgraduate {
apiException.AbortWithException(c, apiException.UserNotFound, nil)
return
}

err = userCenterService.RePassWithoutEmail(data.StudentId, data.IdentityId, data.Password)
if err != nil {
var apiErr *apiException.Error
if errors.As(err, &apiErr) {
apiException.AbortWithException(c, apiErr, err)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}

utils.JsonSuccessResponse(c, nil)
}
43 changes: 43 additions & 0 deletions app/services/userCenterService/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package userCenterService

import (
"net/url"

"4u-go/app/apiException"
"4u-go/config/api/userCenterApi"
)

// RePassWithoutEmail 不通过邮箱修改密码
func RePassWithoutEmail(stuid string, iid string, pwd string) error {
userUrl, err := url.Parse(userCenterApi.RePassWithoutEmail)
if err != nil {
return err
}
urlPath := userUrl.String()
regMap := map[string]any{
"stuid": stuid,
"iid": iid,
"pwd": pwd,
}
resp, err := FetchHandleOfPost(regMap, urlPath)
if err != nil {
return err
}
return handleRePassErrors(resp.Code)
}

// handleRePassErrors 根据响应码处理不同的错误
func handleRePassErrors(code int) error {
switch code {
case 400:
return apiException.StudentNumAndIidError
case 401:
return apiException.PwdError
case 404:
return apiException.UserNotFound
case 200:
return nil
default:
return apiException.ServerError
}
}
2 changes: 2 additions & 0 deletions config/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func Init(r *gin.Engine) {
user.POST("/login/session", userController.AuthBySession)

user.POST("/attachment", objectController.UploadFile)

user.POST("/repass", userController.ChangePassword)
}

admin := api.Group("/admin")
Expand Down

0 comments on commit bcd1284

Please sign in to comment.