From 405be5c1493ab80caa49281cb7ecb085434e6106 Mon Sep 17 00:00:00 2001 From: cbluebird Date: Tue, 30 Jan 2024 17:54:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(Controller):=E7=94=A8=E6=88=B7=E6=B3=A8?= =?UTF-8?q?=E9=94=80=E6=8E=A5=E5=8F=A3=E5=92=8C=E4=BF=AE=E6=94=B9=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E6=8E=A5=E5=8F=A3=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cbluebird --- app/controllers/userController/del.go | 45 +++++++++++++++++ app/controllers/userController/repass.go | 49 +++++++++++++++++++ app/services/userCenterServices/del.go | 31 ++++++++++++ app/services/userCenterServices/repass.go | 34 +++++++++++++ .../userCenterServices/userCenterService.go | 5 -- app/services/userServices/del.go | 17 +++++++ app/services/userServices/repass.go | 27 ++++++++++ config/api/userCenterApi/userCenterApi.go | 2 + config/router/userRouter.go | 4 ++ 9 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 app/controllers/userController/del.go create mode 100644 app/controllers/userController/repass.go create mode 100644 app/services/userCenterServices/del.go create mode 100644 app/services/userCenterServices/repass.go create mode 100644 app/services/userServices/del.go create mode 100644 app/services/userServices/repass.go diff --git a/app/controllers/userController/del.go b/app/controllers/userController/del.go new file mode 100644 index 0000000..bea1db1 --- /dev/null +++ b/app/controllers/userController/del.go @@ -0,0 +1,45 @@ +package userController + +import ( + "github.com/gin-gonic/gin" + "wejh-go/app/apiException" + "wejh-go/app/services/sessionServices" + "wejh-go/app/services/userServices" + "wejh-go/app/utils" +) + +type DelForm struct { + IDCard string `json:"iid" binding:"required"` + StudentID string `json:"stuid" binding:"required"` +} + +func DelAccount(c *gin.Context) { + var postForm DelForm + err := c.ShouldBindJSON(&postForm) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + user, err := sessionServices.GetUserSession(c) + + if err != nil { + _ = c.AbortWithError(200, apiException.NotLogin) + return + } + + if user.Username != postForm.StudentID { + _ = c.AbortWithError(200, apiException.StudentIdError) + return + } + + if err = userServices.DelAccount(user, postForm.IDCard); err != nil { + if err == apiException.StudentNumAndIidError { + _ = c.AbortWithError(200, apiException.StudentNumAndIidError) + return + } + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, nil) +} diff --git a/app/controllers/userController/repass.go b/app/controllers/userController/repass.go new file mode 100644 index 0000000..9938bff --- /dev/null +++ b/app/controllers/userController/repass.go @@ -0,0 +1,49 @@ +package userController + +import ( + "github.com/gin-gonic/gin" + "wejh-go/app/apiException" + "wejh-go/app/services/sessionServices" + "wejh-go/app/services/userServices" + "wejh-go/app/utils" +) + +type RePassForm struct { + IDCard string `json:"iid" binding:"required"` + StudentID string `json:"stuid" binding:"required"` + Password string `json:"password" binding:"required"` +} + +func ResetPass(c *gin.Context) { + var postForm RePassForm + err := c.ShouldBindJSON(&postForm) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + user, err := sessionServices.GetUserSession(c) + + if err != nil { + _ = c.AbortWithError(200, apiException.NotLogin) + return + } + + if user.Username != postForm.StudentID { + _ = c.AbortWithError(200, apiException.StudentIdError) + return + } + + if err = userServices.ResetPass(user, postForm.IDCard, postForm.Password); err != nil { + if err == apiException.StudentNumAndIidError { + _ = c.AbortWithError(200, apiException.StudentNumAndIidError) + return + } else if err == apiException.PwdError { + _ = c.AbortWithError(200, apiException.PwdError) + return + } + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, nil) +} diff --git a/app/services/userCenterServices/del.go b/app/services/userCenterServices/del.go new file mode 100644 index 0000000..4230e47 --- /dev/null +++ b/app/services/userCenterServices/del.go @@ -0,0 +1,31 @@ +package userCenterServices + +import ( + "net/url" + "wejh-go/app/apiException" + "wejh-go/config/api/userCenterApi" +) + +// DelAccount delete the account in user-center +func DelAccount(stuID, iid string) error { + params := url.Values{} + Url, err := url.Parse(string(userCenterApi.DelAccount)) + if err != nil { + return err + } + Url.RawQuery = params.Encode() + urlPath := Url.String() + regMap := make(map[string]string) + regMap["stuid"] = stuID + regMap["iid"] = iid + resp, err := FetchHandleOfPost(regMap, userCenterApi.UserCenterApi(urlPath)) + if err != nil { + return err + } + if resp.Code == 400 { + return apiException.StudentNumAndIidError + } else if resp.Code != 200 { + return apiException.ServerError + } + return nil +} diff --git a/app/services/userCenterServices/repass.go b/app/services/userCenterServices/repass.go new file mode 100644 index 0000000..cc1d2bd --- /dev/null +++ b/app/services/userCenterServices/repass.go @@ -0,0 +1,34 @@ +package userCenterServices + +import ( + "net/url" + "wejh-go/app/apiException" + "wejh-go/config/api/userCenterApi" +) + +// ResetPass reset the password in user-center +func ResetPass(stuID, iid, password string) error { + params := url.Values{} + Url, err := url.Parse(string(userCenterApi.RePassWithoutEmail)) + if err != nil { + return err + } + Url.RawQuery = params.Encode() + urlPath := Url.String() + regMap := make(map[string]string) + regMap["stuid"] = stuID + regMap["iid"] = iid + regMap["pwd"] = password + resp, err := FetchHandleOfPost(regMap, userCenterApi.UserCenterApi(urlPath)) + if err != nil { + return err + } + if resp.Code == 400 { + return apiException.StudentNumAndIidError + } else if resp.Code == 401 { + return apiException.PwdError + } else if resp.Code != 200 { + return apiException.ServerError + } + return nil +} diff --git a/app/services/userCenterServices/userCenterService.go b/app/services/userCenterServices/userCenterService.go index f1769ae..062220c 100644 --- a/app/services/userCenterServices/userCenterService.go +++ b/app/services/userCenterServices/userCenterService.go @@ -2,7 +2,6 @@ package userCenterServices import ( "encoding/json" - "fmt" "wejh-go/app/apiException" "wejh-go/app/utils/fetch" "wejh-go/config/api/userCenterApi" @@ -19,13 +18,11 @@ func FetchHandleOfPost(form map[string]string, url userCenterApi.UserCenterApi) f.Init() res, err := f.PostJsonForm(userCenterApi.UserCenterHost+string(url), form) if err != nil { - fmt.Println(err) return nil, apiException.RequestError } rc := UserCenterResponse{} err = json.Unmarshal(res, &rc) if err != nil { - fmt.Println(err) return nil, apiException.RequestError } return &rc, nil @@ -36,13 +33,11 @@ func FetchHandleOfGet(url userCenterApi.UserCenterApi) (*UserCenterResponse, err f.Init() res, err := f.Get(userCenterApi.UserCenterHost + string(url)) if err != nil { - fmt.Println(err) return nil, apiException.RequestError } rc := UserCenterResponse{} err = json.Unmarshal(res, &rc) if err != nil { - fmt.Println(err) return nil, apiException.RequestError } return &rc, nil diff --git a/app/services/userServices/del.go b/app/services/userServices/del.go new file mode 100644 index 0000000..0e44b3d --- /dev/null +++ b/app/services/userServices/del.go @@ -0,0 +1,17 @@ +package userServices + +import ( + "wejh-go/app/models" + "wejh-go/app/services/userCenterServices" + "wejh-go/config/database" +) + +func DelAccount(user *models.User, iid string) error { + + if err := userCenterServices.DelAccount(user.Username, iid); err != nil { + return err + } + + result := database.DB.Delete(user) + return result.Error +} diff --git a/app/services/userServices/repass.go b/app/services/userServices/repass.go new file mode 100644 index 0000000..19a103b --- /dev/null +++ b/app/services/userServices/repass.go @@ -0,0 +1,27 @@ +package userServices + +import ( + "crypto/sha256" + "encoding/hex" + "wejh-go/app/models" + "wejh-go/app/services/userCenterServices" + "wejh-go/config/database" +) + +func ResetPass(user *models.User, iid, password string) error { + if err := userCenterServices.ResetPass(user.Username, iid, password); err != nil { + return err + } + + h := sha256.New() + h.Write([]byte(password)) + pass := hex.EncodeToString(h.Sum(nil)) + user.JHPassword = pass + EncryptUserKeyInfo(user) + err := database.DB.Model(models.User{}).Where( + models.User{ + Username: user.Username, + ID: user.ID, + }).Updates(user).Error + return err +} diff --git a/config/api/userCenterApi/userCenterApi.go b/config/api/userCenterApi/userCenterApi.go index 7708e55..f44901e 100755 --- a/config/api/userCenterApi/userCenterApi.go +++ b/config/api/userCenterApi/userCenterApi.go @@ -13,4 +13,6 @@ const ( ReSendEmail UserCenterApi = "api/email" Auth UserCenterApi = "api/auth" RePass UserCenterApi = "api/changePwd" + RePassWithoutEmail UserCenterApi = "api/repass" + DelAccount UserCenterApi = "api/del" ) diff --git a/config/router/userRouter.go b/config/router/userRouter.go index 2878a48..ea467e7 100644 --- a/config/router/userRouter.go +++ b/config/router/userRouter.go @@ -18,6 +18,10 @@ func userRouterInit(r *gin.RouterGroup) { user.POST("/login/session", userController.AuthBySession) user.POST("/info", midwares.CheckLogin, userController.GetUserInfo) + + user.POST("/del", midwares.CheckLogin, userController.DelAccount) + user.POST("/repass", midwares.CheckLogin, userController.ResetPass) + bind := user.Group("/bind", midwares.CheckLogin) { bind.POST("/zf", userController.BindZFPassword)