package api import ( "confrontation-training/common" "confrontation-training/constant" errmsg "confrontation-training/err" "confrontation-training/models" "confrontation-training/response" "confrontation-training/service" "fmt" "github.com/gin-gonic/gin" ) 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()) response.Failed(errmsg.ParamInvalid, c) return } if _, count := u.FindUserByPhone(param.Phone); count > 0 { response.Failed(errmsg.UserAlreadyExists, c) return } if result := u.CreateUser(param); result.Error == nil { response.Success(errmsg.UserRegisterSuccess, result.RowsAffected, c) return } else { response.Failed(errmsg.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()) response.Failed(errmsg.ParamInvalid, c) return } user := u.Login(param) if user != nil { //生成token toke, _ := common.GenerateToke(*user) resultMap := map[string]string{ "token": toke, } response.Success(errmsg.UserLoginSuccess, resultMap, c) } else { response.Failed(errmsg.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()) response.Failed(errmsg.ParamInvalid, c) return } if admin := u.FindUserByPhoneAndRole(param.Phone, constant.RoleAdmin); admin == nil { fmt.Printf("管理员"+errmsg.UserNotExists+"%s", param.Phone) response.Failed("管理员"+errmsg.UserNotExists+param.Phone, c) return } if normalUser := u.FindUserByPhoneAndRole(param.PhoneInit, constant.RoleNormal); normalUser == nil { fmt.Printf(errmsg.UserNotExists+"%s", param.PhoneInit) response.Failed(errmsg.UserNotExists+param.PhoneInit, c) return } result := u.RestPassword(param.PhoneInit) if result == nil { response.Failed(errmsg.UserPasswordResetError, c) return } if result.Error != nil { response.Failed(errmsg.UserPasswordResetError+result.Error.Error(), c) return } if result.RowsAffected >= 0 { response.Success(errmsg.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(errmsg.ParamInvalid+"%s", err.Error()) response.Failed(errmsg.ParamInvalid, c) return } userList, rows := u.GetUserList(param) response.SuccessPage(errmsg.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(errmsg.ParamInvalid, c) return } user, count := u.FindUserByPhone(param.Phone) if count <= 0 { response.Failed(errmsg.UserNotExists, c) return } if user.Password != param.Password { response.Failed(errmsg.UserOldPasswordError, c) return } count = u.ChangePassword(user.ID, param.NewPassword) if count <= 0 { response.Failed(errmsg.UserPasswordChangeError, c) return } response.Success(errmsg.UserPasswordChangeSuccess, count, c) }