1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package admin_user
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- "sghgogs.com/micro/auth-client/response"
- "sghgogs.com/micro/auth-client/utils"
- pb "sghgogs.com/micro/auth-service/proto"
- "sghgogs.com/micro/common"
- )
- type UserRequest struct {
- Username string `json:"username" binding:"required,max=15,min=5"` // 用户名
- Password string `json:"password" binding:"required,max=18,min=6"` // 密码
- PhoneNumber string `json:"phone_number" binding:"required"` // 手机号
- Email string `json:"email"` // 邮箱
- Avatar string `json:"avatar"` // 头像
- Roles []int64 `json:"roles"` // 角色ID
- Teams []int64 `json:"teams"` // 团队ID
- }
- // CreateAdminUser
- // @summary 新增用户
- // @Description 创建用户
- // @Tags User 用户管理
- // @Accept json
- // @Produce json
- // @Param Authorization header string true "Bearer 用户令牌"
- // @Param body body UserRequest true "请求body"
- // @Success 200 {object} response.ApiResponse "成功"
- // @Failure 400 {object} response.ApiResponse "请求错误"
- // @Failure 500 {object} response.ApiResponse "内部错误"
- // @Router /v1/api/admin/user [post]
- func (svc *ApiAdminUser) CreateAdminUser(c *gin.Context) {
- var req UserRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
- return
- }
- if common.IsValidPhoneNumber(req.PhoneNumber) {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "手机号格式有误"))
- return
- }
- if !common.IsValidEmail(req.Email) {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "邮箱格式不对"))
- return
- }
- if !common.ValidateUsername(req.Username) {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "4到16位(字母,数字,下划线,减号)"))
- return
- }
- if len(req.Roles) > 0 {
- if isBool, err := common.ValidateNumericInt64(req.Roles); !isBool {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
- return
- }
- }
- if len(req.Teams) > 0 {
- if isBool, err := common.ValidateNumericInt64(req.Teams); !isBool {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
- return
- }
- }
- ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminUserService.CreateAdminUser")
- _, err := svc.Service.CreateAdminUser(ctx, &pb.CreateAdminUserRequest{
- Username: req.Username,
- Password: req.Password,
- PhoneNumber: req.PhoneNumber,
- Email: req.Email,
- Avatar: req.Avatar,
- Roles: req.Roles,
- Teams: req.Teams,
- })
- if err != nil {
- code, mgs := response.MicroErrorRequest(err)
- c.JSON(code, response.ErrorResponse(code, mgs))
- return
- }
- c.JSON(http.StatusOK, response.SuccessResponse(nil))
- }
|