From a769d05bcddd35e37961fd6ddb64acecd2413590 Mon Sep 17 00:00:00 2001 From: SugarMGP <2350745751@qq.com> Date: Fri, 22 Nov 2024 16:24:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A8=E6=88=B7=E6=B3=A8=E9=94=80?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/userController/delete.go | 51 ++++++++++++++++++++++++ app/controllers/userController/update.go | 18 +++------ app/midwares/checkInit.go | 7 +--- app/services/userCenterService/delete.go | 41 +++++++++++++++++++ app/services/userCenterService/update.go | 6 +-- app/services/userService/delete.go | 18 +++++++++ config/router/router.go | 3 +- 7 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 app/controllers/userController/delete.go create mode 100644 app/services/userCenterService/delete.go create mode 100644 app/services/userService/delete.go diff --git a/app/controllers/userController/delete.go b/app/controllers/userController/delete.go new file mode 100644 index 0000000..d27e464 --- /dev/null +++ b/app/controllers/userController/delete.go @@ -0,0 +1,51 @@ +package userController + +import ( + "errors" + + "4u-go/app/apiException" + "4u-go/app/models" + "4u-go/app/services/userService" + "4u-go/app/utils" + "github.com/gin-gonic/gin" +) + +type deleteAccountData struct { + StudentID string `json:"student_id" binding:"required"` + IdentityID string `json:"identity_id" binding:"required"` +} + +// DeleteAccount 注销账户 +func DeleteAccount(c *gin.Context) { + var data deleteAccountData + err := c.ShouldBindJSON(&data) + if err != nil { + apiException.AbortWithException(c, apiException.ParamError, err) + return + } + + user := utils.GetUser(c) + if user.StudentID != data.StudentID { + apiException.AbortWithException(c, apiException.NotPermission, err) + return + } + + // 若不是普通用户则提示不存在 + if user.Type != models.Undergraduate && user.Type != models.Postgraduate { + apiException.AbortWithException(c, apiException.UserNotFound, nil) + return + } + + err = userService.DeleteAccount(user, data.IdentityID) + 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/controllers/userController/update.go b/app/controllers/userController/update.go index 9af8aec..0439bbf 100644 --- a/app/controllers/userController/update.go +++ b/app/controllers/userController/update.go @@ -6,15 +6,13 @@ import ( "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"` + StudentID string `json:"student_id" binding:"required"` + IdentityID string `json:"identity_id" binding:"required"` Password string `json:"password" binding:"required"` } @@ -27,13 +25,9 @@ func ChangePassword(c *gin.Context) { 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) + user := utils.GetUser(c) + if user.StudentID != data.StudentID { + apiException.AbortWithException(c, apiException.NotPermission, err) return } @@ -43,7 +37,7 @@ func ChangePassword(c *gin.Context) { return } - err = userCenterService.RePassWithoutEmail(data.StudentId, data.IdentityId, data.Password) + err = userCenterService.RePassWithoutEmail(data.StudentID, data.IdentityID, data.Password) if err != nil { var apiErr *apiException.Error if errors.As(err, &apiErr) { diff --git a/app/midwares/checkInit.go b/app/midwares/checkInit.go index e5a8573..2004184 100644 --- a/app/midwares/checkInit.go +++ b/app/midwares/checkInit.go @@ -1,8 +1,6 @@ package midwares import ( - "log" - "4u-go/app/apiException" "4u-go/app/config" "github.com/gin-gonic/gin" @@ -12,10 +10,7 @@ import ( func CheckInit(c *gin.Context) { inited := config.GetInit() if !inited { - err := c.AbortWithError(200, apiException.NotInit) - if err != nil { - log.Println("CheckInitFailed:", err) // 记录错误日志,而不是退出程序 - } + apiException.AbortWithException(c, apiException.NotInit, nil) return } c.Next() diff --git a/app/services/userCenterService/delete.go b/app/services/userCenterService/delete.go new file mode 100644 index 0000000..1b932f8 --- /dev/null +++ b/app/services/userCenterService/delete.go @@ -0,0 +1,41 @@ +package userCenterService + +import ( + "net/url" + + "4u-go/app/apiException" + "4u-go/config/api/userCenterApi" +) + +// DeleteAccount 注销账户 +func DeleteAccount(stuid, iid string) error { + deleteUrl, err := url.Parse(userCenterApi.DelAccount) + if err != nil { + return err + } + urlPath := deleteUrl.String() + regMap := map[string]any{ + "iid": iid, + "stuid": stuid, + "bound_system": 1, + } + resp, err := FetchHandleOfPost(regMap, urlPath) + if err != nil { + return err + } + return handleDeleteErrors(resp.Code) +} + +// handleDeleteErrors 根据响应码处理不同的错误 +func handleDeleteErrors(code int) error { + switch code { + case 400: + return apiException.StudentNumAndIidError + case 404: + return apiException.UserNotFound + case 200: + return nil + default: + return apiException.ServerError + } +} diff --git a/app/services/userCenterService/update.go b/app/services/userCenterService/update.go index de651b3..9b1aaba 100644 --- a/app/services/userCenterService/update.go +++ b/app/services/userCenterService/update.go @@ -8,12 +8,12 @@ import ( ) // RePassWithoutEmail 不通过邮箱修改密码 -func RePassWithoutEmail(stuid string, iid string, pwd string) error { - userUrl, err := url.Parse(userCenterApi.RePassWithoutEmail) +func RePassWithoutEmail(stuid, iid, pwd string) error { + repassUrl, err := url.Parse(userCenterApi.RePassWithoutEmail) if err != nil { return err } - urlPath := userUrl.String() + urlPath := repassUrl.String() regMap := map[string]any{ "stuid": stuid, "iid": iid, diff --git a/app/services/userService/delete.go b/app/services/userService/delete.go new file mode 100644 index 0000000..991e15c --- /dev/null +++ b/app/services/userService/delete.go @@ -0,0 +1,18 @@ +package userService + +import ( + "4u-go/app/models" + "4u-go/app/services/userCenterService" + "4u-go/config/database" +) + +// DeleteAccount 注销账户 +func DeleteAccount(user *models.User, iid string) error { + err := userCenterService.DeleteAccount(user.StudentID, iid) + if err != nil { + return err + } + + result := database.DB.Delete(user) + return result.Error +} diff --git a/config/router/router.go b/config/router/router.go index d51ab3a..0cf6f65 100644 --- a/config/router/router.go +++ b/config/router/router.go @@ -29,7 +29,8 @@ func Init(r *gin.Engine) { user.POST("/attachment", objectController.UploadFile) - user.POST("/repass", userController.ChangePassword) + user.POST("/repass", midwares.CheckLogin, userController.ChangePassword) + user.DELETE("/delete", midwares.CheckLogin, userController.DeleteAccount) } admin := api.Group("/admin")