-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from cbluebird/main
feat(Controller):用户注销接口和修改密码接口开发
- Loading branch information
Showing
9 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters