forked from free-lancers/electricity_bill_calc_service
		
	feat(user):增加修改用户可用性状态的功能。
This commit is contained in:
		@@ -30,6 +30,7 @@ func InitializeUserController(router *gin.Engine) {
 | 
			
		||||
	UserController.Router.DELETE("/password/:uid", security.OPSAuthorize, UserController.InvalidUserPassword)
 | 
			
		||||
	UserController.Router.PUT("/password", UserController.ResetUserPassword)
 | 
			
		||||
	UserController.Router.GET("/accounts", security.OPSAuthorize, UserController.ListPagedUser)
 | 
			
		||||
	UserController.Router.PUT("/account/enabled/state", security.OPSAuthorize, UserController.SwitchUserEnabling)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type LoginFormData struct {
 | 
			
		||||
@@ -172,3 +173,25 @@ func (_UserController) ListPagedUser(c *gin.Context) {
 | 
			
		||||
		gin.H{"accounts": users},
 | 
			
		||||
	)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type UserStateFormData struct {
 | 
			
		||||
	UserID  string `json:"uid" form:"uid"`
 | 
			
		||||
	Enabled bool   `json:"enabled" form:"enabled"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (_UserController) SwitchUserEnabling(c *gin.Context) {
 | 
			
		||||
	result := response.NewResult(c)
 | 
			
		||||
	switchForm := new(UserStateFormData)
 | 
			
		||||
	c.BindJSON(switchForm)
 | 
			
		||||
	err := service.UserService.SwitchUserState(switchForm.UserID, switchForm.Enabled)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if nfErr, ok := err.(*exceptions.NotFoundError); ok {
 | 
			
		||||
			result.Error(http.StatusNotFound, nfErr.Message)
 | 
			
		||||
			return
 | 
			
		||||
		} else {
 | 
			
		||||
			result.Error(http.StatusInternalServerError, err.Error())
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	result.Success("用户状态已经更新。")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -169,10 +169,17 @@ func (_UserService) IsUserExists(uid string) (bool, error) {
 | 
			
		||||
	return global.DBConn.ID(uid).Exist(&model.User{})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (_UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) {
 | 
			
		||||
func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) {
 | 
			
		||||
	if len(user.Id) == 0 {
 | 
			
		||||
		user.Id = uuid.New().String()
 | 
			
		||||
	}
 | 
			
		||||
	exists, err := u.IsUserExists(user.Id)
 | 
			
		||||
	if exists {
 | 
			
		||||
		return "", exceptions.NewNotFoundError("user already exists")
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", nil
 | 
			
		||||
	}
 | 
			
		||||
	detail.Id = user.Id
 | 
			
		||||
 | 
			
		||||
	verifyCode := utils.RandStr(10)
 | 
			
		||||
@@ -186,7 +193,7 @@ func (_UserService) CreateUser(user *model.User, detail *model.UserDetail) (stri
 | 
			
		||||
	if err := tx.Begin(); err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	_, err := tx.Insert(user)
 | 
			
		||||
	_, err = tx.Insert(user)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		tx.Rollback()
 | 
			
		||||
		return "", fmt.Errorf("user create failed: %w", err)
 | 
			
		||||
@@ -203,3 +210,17 @@ func (_UserService) CreateUser(user *model.User, detail *model.UserDetail) (stri
 | 
			
		||||
	}
 | 
			
		||||
	return verifyCode, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (u _UserService) SwitchUserState(uid string, enabled bool) error {
 | 
			
		||||
	exists, err := u.IsUserExists(uid)
 | 
			
		||||
	if !exists {
 | 
			
		||||
		return exceptions.NewNotFoundError("user not exists")
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	newStateUser := new(model.User)
 | 
			
		||||
	newStateUser.Enabled = enabled
 | 
			
		||||
	_, err = global.DBConn.ID(uid).Cols("enabled").Update(newStateUser)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user