package gateway import ( "confrontation-training/common" "confrontation-training/constant" errors "confrontation-training/err" "confrontation-training/models" "confrontation-training/response" "confrontation-training/service" "fmt" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" ) type UserService struct { service.UserService } func GetUser() *UserService { return &UserService{} } // UseRegister // PingExample confrontation-training // @Summary 用户注册 // @Schemes // @Description 用户注册 // @Tags 用户管理 // @Param user body string true "userName:用户名;password:密码;role:角色" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/user/register [post] func (u *UserService) UseRegister(c *gin.Context) { var param models.UserRegister err := c.ShouldBindJSON(¶m) if err != nil { fmt.Printf("参数格式化异常:%s", err.Error()) log.Infof("参数格式化异常:%s", err.Error()) response.Failed(errors.ParamInvalid, c) return } if _, count := u.FindUserByPhone(param.Phone); count > 0 { response.Failed(errors.UserAlreadyExists, c) return } if result := u.CreateUser(param); result.Error == nil { response.Success(errors.UserRegisterSuccess, result.RowsAffected, c) return } else { response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c) return } } // UserLogin // PingExample confrontation-training // @Summary 用户登录 // @Schemes // @Description 用户登录 // @Tags 用户管理 // @Param user body string true "username:编号;password:密码;role:角色;" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/user/login [post] func (u *UserService) UserLogin(c *gin.Context) { var param models.UserLogin err := c.ShouldBindJSON(¶m) if err != nil { fmt.Printf("参数格式化异常:%s", err.Error()) log.Infof("参数格式化异常:%s", err.Error()) response.Failed(errors.ParamInvalid, c) return } user := u.Login(param) if user != nil { //生成token toke, _ := common.GenerateToke(*user) resultMap := map[string]string{ "token": toke, } response.Success(errors.UserLoginSuccess, resultMap, c) } else { response.Failed(errors.UserLoginFailed, c) } } // ResetPassword // PingExample confrontation-training // @Summary 管理员重置普通用户密码 // @Schemes // @Description 管理员重置普通用户密码 // @Tags 用户管理 // @Param user body string true "username:管理员编号;usernameInit:被重置用户编号" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/user/reset [post] func (u *UserService) ResetPassword(c *gin.Context) { var param models.ResetPassword err := c.ShouldBindJSON(¶m) if err != nil { fmt.Printf("参数格式化异常:%s", err.Error()) log.Infof("参数格式化异常:%s", err.Error()) response.Failed(errors.ParamInvalid, c) return } if admin := u.FindUserByPhoneAndRole(param.Phone, constant.RoleAdmin); admin == nil { fmt.Printf("管理员"+errors.UserNotExists+"%s", param.Phone) log.Infof("管理员"+errors.UserNotExists+"%s", param.Phone) response.Failed("管理员"+errors.UserNotExists+param.Phone, c) return } if normalUser := u.FindUserByPhoneAndRole(param.PhoneInit, constant.RoleNormal); normalUser == nil { fmt.Printf(errors.UserNotExists+"%s", param.PhoneInit) log.Infof(errors.UserNotExists+"%s", param.PhoneInit) response.Failed(errors.UserNotExists+param.PhoneInit, c) return } result := u.RestPassword(param.PhoneInit) if result == nil { response.Failed(errors.UserPasswordResetError, c) return } if result.Error != nil { response.Failed(errors.UserPasswordResetError+result.Error.Error(), c) return } if result.RowsAffected >= 0 { response.Success(errors.UserPasswordResetSuccess, result.RowsAffected, c) return } } // UserList // PingExample confrontation-training // @Summary 用户列表查询 // @Schemes // @Description 用户列表查询 // @Tags 用户管理 // @Param user body string true "role:角色;userName:用户名;pageNum:页数;pageSize:每页记录数" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/user/find [post] func (u *UserService) UserList(c *gin.Context) { var param models.UserListParam err := c.ShouldBindJSON(¶m) if err != nil { fmt.Printf(errors.ParamInvalid+"%s", err.Error()) log.Infof(errors.ParamInvalid+"%s", err.Error()) response.Failed(errors.ParamInvalid, c) return } userList, rows := u.GetUserList(param) response.SuccessPage(errors.FindSuccess, userList, rows, c) } // ModePass // PingExample confrontation-training // @Summary 用户修改密码 // @Schemes // @Description 用户修改密码 // @Tags 用户管理 // @Param user body string true "userName:用户名 password:密码 newPassword:新密码" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/user/change/password [post] func (u *UserService) ModePass(c *gin.Context) { var param models.ChangePassword err := c.ShouldBindJSON(¶m) if err != nil { response.Failed(errors.ParamInvalid, c) return } user, count := u.FindUserByPhone(param.Phone) if count <= 0 { response.Failed(errors.UserNotExists, c) return } if user.Password != param.Password { response.Failed(errors.UserOldPasswordError, c) return } count = u.ChangePassword(user.ID, param.NewPassword) if count <= 0 { response.Failed(errors.UserPasswordChangeError, c) return } response.Success(errors.UserPasswordChangeSuccess, count, c) }