create_admin_user.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package admin_user
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "sghgogs.com/micro/auth-client/response"
  6. "sghgogs.com/micro/auth-client/utils"
  7. pb "sghgogs.com/micro/auth-service/proto"
  8. "sghgogs.com/micro/common"
  9. )
  10. type UserRequest struct {
  11. Username string `json:"username" binding:"required,max=15,min=5"` // 用户名
  12. Password string `json:"password" binding:"required,max=18,min=6"` // 密码
  13. PhoneNumber string `json:"phone_number" binding:"required"` // 手机号
  14. Email string `json:"email"` // 邮箱
  15. Avatar string `json:"avatar"` // 头像
  16. Roles []int64 `json:"roles"` // 角色ID
  17. Teams []int64 `json:"teams"` // 团队ID
  18. }
  19. // CreateAdminUser
  20. // @summary 新增用户
  21. // @Description 创建用户
  22. // @Tags User 用户管理
  23. // @Accept json
  24. // @Produce json
  25. // @Param Authorization header string true "Bearer 用户令牌"
  26. // @Param body body UserRequest true "请求body"
  27. // @Success 200 {object} response.ApiResponse "成功"
  28. // @Failure 400 {object} response.ApiResponse "请求错误"
  29. // @Failure 500 {object} response.ApiResponse "内部错误"
  30. // @Router /v1/api/admin/user [post]
  31. func (svc *ApiAdminUser) CreateAdminUser(c *gin.Context) {
  32. var req UserRequest
  33. if err := c.ShouldBindJSON(&req); err != nil {
  34. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
  35. return
  36. }
  37. if common.IsValidPhoneNumber(req.PhoneNumber) {
  38. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "手机号格式有误"))
  39. return
  40. }
  41. if !common.IsValidEmail(req.Email) {
  42. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "邮箱格式不对"))
  43. return
  44. }
  45. if !common.ValidateUsername(req.Username) {
  46. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "4到16位(字母,数字,下划线,减号)"))
  47. return
  48. }
  49. if len(req.Roles) > 0 {
  50. if isBool, err := common.ValidateNumericInt64(req.Roles); !isBool {
  51. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
  52. return
  53. }
  54. }
  55. if len(req.Teams) > 0 {
  56. if isBool, err := common.ValidateNumericInt64(req.Teams); !isBool {
  57. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
  58. return
  59. }
  60. }
  61. ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminUserService.CreateAdminUser")
  62. _, err := svc.Service.CreateAdminUser(ctx, &pb.CreateAdminUserRequest{
  63. Username: req.Username,
  64. Password: req.Password,
  65. PhoneNumber: req.PhoneNumber,
  66. Email: req.Email,
  67. Avatar: req.Avatar,
  68. Roles: req.Roles,
  69. Teams: req.Teams,
  70. })
  71. if err != nil {
  72. code, mgs := response.MicroErrorRequest(err)
  73. c.JSON(code, response.ErrorResponse(code, mgs))
  74. return
  75. }
  76. c.JSON(http.StatusOK, response.SuccessResponse(nil))
  77. }