Skip to content

Commit

Permalink
feat: 用户注销接口
Browse files Browse the repository at this point in the history
  • Loading branch information
SugarMGP committed Nov 22, 2024
1 parent 2901f31 commit a769d05
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 22 deletions.
51 changes: 51 additions & 0 deletions app/controllers/userController/delete.go
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 6 additions & 12 deletions app/controllers/userController/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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
}

Expand All @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions app/midwares/checkInit.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package midwares

import (
"log"

"4u-go/app/apiException"
"4u-go/app/config"
"github.com/gin-gonic/gin"
Expand All @@ -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()
Expand Down
41 changes: 41 additions & 0 deletions app/services/userCenterService/delete.go
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 3 additions & 3 deletions app/services/userCenterService/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions app/services/userService/delete.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion config/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a769d05

Please sign in to comment.