Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 普通用户修改密码接口 #18

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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