-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 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,58 @@ | ||
package userController | ||
|
||
import ( | ||
"errors" | ||
|
||
"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"` | ||
Password string `json:"password" binding:"required"` | ||
} | ||
|
||
// ChangePassword 修改密码接口 | ||
func ChangePassword(c *gin.Context) { | ||
var data changePasswordData | ||
err := c.ShouldBindJSON(&data) | ||
if err != nil { | ||
apiException.AbortWithException(c, apiException.ParamError, err) | ||
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) | ||
return | ||
} | ||
|
||
// 若不是普通用户则提示不存在 | ||
if user.Type != models.Undergraduate && user.Type != models.Postgraduate { | ||
apiException.AbortWithException(c, apiException.UserNotFound, nil) | ||
return | ||
} | ||
|
||
err = userCenterService.RePassWithoutEmail(data.StudentId, data.IdentityId, data.Password) | ||
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) | ||
} |
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,43 @@ | ||
package userCenterService | ||
|
||
import ( | ||
"net/url" | ||
|
||
"4u-go/app/apiException" | ||
"4u-go/config/api/userCenterApi" | ||
) | ||
|
||
// RePassWithoutEmail 不通过邮箱修改密码 | ||
func RePassWithoutEmail(stuid string, iid string, pwd string) error { | ||
userUrl, err := url.Parse(userCenterApi.RePassWithoutEmail) | ||
if err != nil { | ||
return err | ||
} | ||
urlPath := userUrl.String() | ||
regMap := map[string]any{ | ||
"stuid": stuid, | ||
"iid": iid, | ||
"pwd": pwd, | ||
} | ||
resp, err := FetchHandleOfPost(regMap, urlPath) | ||
if err != nil { | ||
return err | ||
} | ||
return handleRePassErrors(resp.Code) | ||
} | ||
|
||
// handleRePassErrors 根据响应码处理不同的错误 | ||
func handleRePassErrors(code int) error { | ||
switch code { | ||
case 400: | ||
return apiException.StudentNumAndIidError | ||
case 401: | ||
return apiException.PwdError | ||
case 404: | ||
return apiException.UserNotFound | ||
case 200: | ||
return nil | ||
default: | ||
return apiException.ServerError | ||
} | ||
} |
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