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(Controller):用户注销接口和修改密码接口开发 #70

Merged
merged 1 commit into from
Jan 30, 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
45 changes: 45 additions & 0 deletions app/controllers/userController/del.go
Original file line number Diff line number Diff line change
@@ -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)
}
49 changes: 49 additions & 0 deletions app/controllers/userController/repass.go
Original file line number Diff line number Diff line change
@@ -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)
}
31 changes: 31 additions & 0 deletions app/services/userCenterServices/del.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions app/services/userCenterServices/repass.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 0 additions & 5 deletions app/services/userCenterServices/userCenterService.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package userCenterServices

import (
"encoding/json"
"fmt"
"wejh-go/app/apiException"
"wejh-go/app/utils/fetch"
"wejh-go/config/api/userCenterApi"
Expand All @@ -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
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions app/services/userServices/del.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions app/services/userServices/repass.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions config/api/userCenterApi/userCenterApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
4 changes: 4 additions & 0 deletions config/router/userRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading