diff --git a/app/controllers/userController/update.go b/app/controllers/userController/update.go new file mode 100644 index 0000000..9af8aec --- /dev/null +++ b/app/controllers/userController/update.go @@ -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) +} diff --git a/app/services/userCenterService/update.go b/app/services/userCenterService/update.go new file mode 100644 index 0000000..de651b3 --- /dev/null +++ b/app/services/userCenterService/update.go @@ -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 + } +} diff --git a/config/router/router.go b/config/router/router.go index 66778a1..d51ab3a 100644 --- a/config/router/router.go +++ b/config/router/router.go @@ -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")