add
wangpengfei
2023-07-18 5fac03fb857cf9a160e1736a25de2c5f95f5e44f
add

add User Module
9个文件已修改
391 ■■■■■ 已修改文件
api/v1/user.go 53 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 74 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 74 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/request/user.go 27 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/user.go 78 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/container_docker.go 29 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/user.go 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
source/user.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/user.go
@@ -58,13 +58,6 @@
        ctx.Fail(errCode)
        return
    }
    if !user.Enable {
        logx.Errorf("登陆失败! 用户被禁止登录!")
        // 验证码次数+1
        _ = constvar.BlackCache.Increment(key, 1)
        ctx.Fail(ecode.UserForbidden)
        return
    }
    // 赋值菜单ID列表
    //user.MenuIds, _ = menuService.GetUserMenuIds(user.ID, user.UserType)
    slf.TokenNext(ctx, *user)
@@ -83,7 +76,6 @@
    claims := j.CreateClaims(request.BaseClaims{
        UserId:      user.ID,
        Username:    user.Username,
        ParentId:    user.ParentName,
        UserType:    user.UserType,
        AuthorityId: user.AuthorityId,
    })
@@ -156,7 +148,7 @@
        return
    }
    if len(params.Username) == 0 || len(params.Password) == 0 || len(params.Phone) == 0 || len(params.NickName) == 0 {
    if len(params.Username) == 0 || len(params.Password) == 0 || len(params.NickName) == 0 || params.AuthorityId == 0 || len(params.RePassword) == 0 || params.DepartmentId == 0 {
        ctx.Fail(ecode.ParamsErr)
        return
    }
@@ -164,41 +156,28 @@
    var userId = fmt.Sprintf("u%v", snowflake.GenerateId())
    var passWord = encrypt.BcryptHash(params.Password)
    var userType constvar.UserType
    var parentId string
    var parentName string
    if userInfo.UserType == constvar.UserTypeSuper {
        userType = constvar.UserTypePrimary // 主账户的父用户ID是自己
        parentId = userId
        parentName = params.Username
    } else if userInfo.UserType == constvar.UserTypePrimary {
        userType = constvar.UserTypeSub
        parentId = userInfo.UserId
        parentName = userInfo.ParentId
    } else {
        ctx.Fail(ecode.NoPowerErr)
        return
    }
    user := &model.User{ID: userId, Username: params.Username, UserType: userType, NickName: params.NickName, Password: passWord, HeaderImg: params.HeaderImg, Enable: true, ParentId: parentId, ParentName: parentName, Phone: params.Phone, Email: params.Email}
    user := &model.User{ID: userId, Username: params.Username, UserType: userType, NickName: params.NickName, Password: passWord, HeaderImg: params.HeaderImg, Phone: params.Phone, Email: params.Email, DepartmentId: params.DepartmentId, AuthorityId: params.AuthorityId}
    userReturn, errCode := userService.Register(user)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    if user.UserType == constvar.UserTypePrimary { // 主账户创建对应的数据库用户和排程数据库
        err := model.NewMysql().CreateDatabase(user.Username)
        if err != nil {
            ctx.Fail(ecode.CreateDatabaseErr)
            return
        }
        defaultPwd := fmt.Sprintf("%v@Basic2023", user.Username)
        err = model.NewMysql().CreateUser(user.Username, defaultPwd, user.Username)
        if err != nil {
            ctx.Fail(ecode.CreateDatabaseUserErr)
            return
        }
    }
    //if user.UserType == constvar.UserTypePrimary { // 主账户创建对应的数据库用户和排程数据库
    //    err := model.NewMysql().CreateDatabase(user.Username)
    //    if err != nil {
    //        ctx.Fail(ecode.CreateDatabaseErr)
    //        return
    //    }
    //
    //    defaultPwd := fmt.Sprintf("%v@Basic2023", user.Username)
    //    err = model.NewMysql().CreateUser(user.Username, defaultPwd, user.Username)
    //    if err != nil {
    //        ctx.Fail(ecode.CreateDatabaseUserErr)
    //        return
    //    }
    //}
    ctx.OkWithDetailed(response.UserResponse{User: *userReturn})
}
docs/docs.go
@@ -6292,26 +6292,16 @@
                    "description": "用户角色ID",
                    "type": "integer"
                },
                "companyName": {
                    "type": "string"
                },
                "createAt": {
                    "description": "创建时间",
                    "type": "string"
                "departmentId": {
                    "type": "integer"
                },
                "email": {
                    "type": "string"
                },
                "enable": {
                    "type": "boolean"
                },
                "headerImg": {
                    "type": "string"
                },
                "id": {
                    "type": "string"
                },
                "ip": {
                    "type": "string"
                },
                "menuIds": {
@@ -6324,26 +6314,13 @@
                "nickName": {
                    "type": "string"
                },
                "parentId": {
                    "type": "string"
                },
                "parentName": {
                    "type": "string"
                },
                "phone": {
                    "type": "string"
                },
                "port": {
                    "type": "string"
                },
                "pos": {
                    "type": "string"
                },
                "status": {
                    "type": "integer"
                },
                "updateAt": {
                    "description": "更新时间",
                "realName": {
                    "type": "string"
                },
                "userType": {
@@ -7526,29 +7503,46 @@
        "request.Register": {
            "type": "object",
            "properties": {
                "authorityId": {
                    "description": "用户角色ID",
                    "type": "integer"
                },
                "departmentId": {
                    "type": "integer"
                },
                "email": {
                    "type": "string",
                    "example": "电子邮箱"
                    "type": "string"
                },
                "headerImg": {
                    "type": "string",
                    "example": "头像链接"
                    "type": "string"
                },
                "id": {
                    "type": "string"
                },
                "nickName": {
                    "type": "string",
                    "example": "昵称"
                    "type": "string"
                },
                "passWord": {
                    "type": "string",
                    "example": "密码"
                "password": {
                    "type": "string"
                },
                "phone": {
                    "type": "string",
                    "example": "电话号码"
                    "type": "string"
                },
                "userName": {
                    "type": "string",
                    "example": "用户名"
                "pos": {
                    "type": "string"
                },
                "rePassword": {
                    "description": "确认密码",
                    "type": "string"
                },
                "realName": {
                    "type": "string"
                },
                "userType": {
                    "$ref": "#/definitions/constvar.UserType"
                },
                "username": {
                    "type": "string"
                }
            }
        },
docs/swagger.json
@@ -6280,26 +6280,16 @@
                    "description": "用户角色ID",
                    "type": "integer"
                },
                "companyName": {
                    "type": "string"
                },
                "createAt": {
                    "description": "创建时间",
                    "type": "string"
                "departmentId": {
                    "type": "integer"
                },
                "email": {
                    "type": "string"
                },
                "enable": {
                    "type": "boolean"
                },
                "headerImg": {
                    "type": "string"
                },
                "id": {
                    "type": "string"
                },
                "ip": {
                    "type": "string"
                },
                "menuIds": {
@@ -6312,26 +6302,13 @@
                "nickName": {
                    "type": "string"
                },
                "parentId": {
                    "type": "string"
                },
                "parentName": {
                    "type": "string"
                },
                "phone": {
                    "type": "string"
                },
                "port": {
                    "type": "string"
                },
                "pos": {
                    "type": "string"
                },
                "status": {
                    "type": "integer"
                },
                "updateAt": {
                    "description": "更新时间",
                "realName": {
                    "type": "string"
                },
                "userType": {
@@ -7514,29 +7491,46 @@
        "request.Register": {
            "type": "object",
            "properties": {
                "authorityId": {
                    "description": "用户角色ID",
                    "type": "integer"
                },
                "departmentId": {
                    "type": "integer"
                },
                "email": {
                    "type": "string",
                    "example": "电子邮箱"
                    "type": "string"
                },
                "headerImg": {
                    "type": "string",
                    "example": "头像链接"
                    "type": "string"
                },
                "id": {
                    "type": "string"
                },
                "nickName": {
                    "type": "string",
                    "example": "昵称"
                    "type": "string"
                },
                "passWord": {
                    "type": "string",
                    "example": "密码"
                "password": {
                    "type": "string"
                },
                "phone": {
                    "type": "string",
                    "example": "电话号码"
                    "type": "string"
                },
                "userName": {
                    "type": "string",
                    "example": "用户名"
                "pos": {
                    "type": "string"
                },
                "rePassword": {
                    "description": "确认密码",
                    "type": "string"
                },
                "realName": {
                    "type": "string"
                },
                "userType": {
                    "$ref": "#/definitions/constvar.UserType"
                },
                "username": {
                    "type": "string"
                }
            }
        },
docs/swagger.yaml
@@ -867,20 +867,13 @@
      authorityId:
        description: 用户角色ID
        type: integer
      companyName:
        type: string
      createAt:
        description: 创建时间
        type: string
      departmentId:
        type: integer
      email:
        type: string
      enable:
        type: boolean
      headerImg:
        type: string
      id:
        type: string
      ip:
        type: string
      menuIds:
        description: 菜单ID列表
@@ -889,20 +882,11 @@
        type: array
      nickName:
        type: string
      parentId:
        type: string
      parentName:
        type: string
      phone:
        type: string
      port:
        type: string
      pos:
        type: string
      status:
        type: integer
      updateAt:
        description: 更新时间
      realName:
        type: string
      userType:
        $ref: '#/definitions/constvar.UserType'
@@ -1708,23 +1692,33 @@
    type: object
  request.Register:
    properties:
      authorityId:
        description: 用户角色ID
        type: integer
      departmentId:
        type: integer
      email:
        example: 电子邮箱
        type: string
      headerImg:
        example: 头像链接
        type: string
      id:
        type: string
      nickName:
        example: 昵称
        type: string
      passWord:
        example: 密码
      password:
        type: string
      phone:
        example: 电话号码
        type: string
      userName:
        example: 用户名
      pos:
        type: string
      rePassword:
        description: 确认密码
        type: string
      realName:
        type: string
      userType:
        $ref: '#/definitions/constvar.UserType'
      username:
        type: string
    type: object
  request.SalesDetails:
model/request/user.go
@@ -1,6 +1,8 @@
package request
import "aps_crm/constvar"
import (
    "aps_crm/constvar"
)
type (
    Login struct {
@@ -11,12 +13,23 @@
    }
    Register struct {
        Username  string `json:"userName" example:"用户名"`
        Password  string `json:"passWord" example:"密码"`
        NickName  string `json:"nickName" example:"昵称"`
        HeaderImg string `json:"headerImg" example:"头像链接"`
        Phone     string `json:"phone" example:"电话号码"`
        Email     string `json:"email" example:"电子邮箱"`
        User
        RePassword string `json:"rePassword"` // 确认密码
    }
    User struct {
        ID           string            `json:"id"`
        Username     string            `json:"username"`
        UserType     constvar.UserType `json:"userType"`
        Password     string            `json:"password"`
        NickName     string            `json:"nickName"`
        RealName     string            `json:"realName"`
        DepartmentId int               `json:"departmentId"`
        HeaderImg    string            `json:"headerImg"`
        Phone        string            `json:"phone" `
        Email        string            `json:"email" `
        Pos          string            `json:"pos"`
        AuthorityId  uint              `json:"authorityId"` // 用户角色ID
    }
    ChangePasswordReq struct {
model/user.go
@@ -5,37 +5,27 @@
    "aps_crm/pkg/mysqlx"
    "fmt"
    "gorm.io/gorm"
    "time"
)
type (
    // User token里边把用户ID、父用户ID、角色都带上
    User struct {
        ID          string            `json:"id" gorm:"primaryKey;type:varchar(255);comment:用户ID"`
        Username    string            `json:"username" gorm:"index;type:varchar(255);comment:用户登录名"`
        UserType    constvar.UserType `json:"userType" gorm:"type:int(11);comment:用户类型 1超级管理员 2主账户 3子账户"`
        Password    string            `json:"-"  gorm:"type:varchar(255);comment:用户登录密码"`
        NickName    string            `json:"nickName" gorm:"type:varchar(255);default:系统用户;comment:用户昵称"`
        HeaderImg   string            `json:"headerImg" gorm:"type:varchar(255);comment:用户头像"`
        Phone       string            `json:"phone"  gorm:"type:varchar(255);comment:用户手机号"`
        Email       string            `json:"email"  gorm:"type:varchar(255);comment:用户邮箱"`
        Enable      bool              `json:"enable" gorm:"type:tinyint(1);comment:用户是否被冻结"`
        ParentId    string            `json:"parentId" gorm:"type:varchar(255);comment:父用户ID"`
        ParentName  string            `json:"parentName" gorm:"type:varchar(255);comment:父用户名称"`
        CompanyName string            `json:"companyName" gorm:"type:varchar(255);comment:公司名称"`
        Pos         string            `json:"pos" gorm:"type:varchar(255);comment:岗位"`
        ModifiedPwd bool              `json:"-" gorm:"type:tinyint(1);comment:是否改过密码"`
        CreateTime  int64             `json:"-"  gorm:"type:bigint(20);comment:创建时间"`
        UpdateTime  int64             `json:"-"  gorm:"type:bigint(20);comment:更新时间"`
        Menus       []Menu            `json:"-" gorm:"many2many:user_menu;"` // 用户菜单
        CreateAt    string            `json:"createAt" gorm:"-"`             // 创建时间
        UpdateAt    string            `json:"updateAt" gorm:"-"`             // 更新时间
        MenuIds     []uint            `json:"menuIds" gorm:"-"`              // 菜单ID列表
        Ip          string            `json:"ip" gorm:"type:varchar(255);comment:集群Ip"`
        Port        string            `json:"port" gorm:"type:varchar(255);comment:端口号"`
        Status      int               `json:"status" gorm:"type:int(11);comment:用户审核状态 0:禁用; 1:正常; 2:审核中"`
        AuthorityId uint              `json:"authorityId" gorm:"default:888;comment:用户角色ID"` // 用户角色ID
        Authority   Authority         `json:"authority" gorm:"foreignKey:AuthorityId"`
        ID           string            `json:"id" gorm:"primaryKey;type:varchar(255);comment:用户ID"`
        Username     string            `json:"username" gorm:"index;type:varchar(255);comment:用户登录名"`
        UserType     constvar.UserType `json:"userType" gorm:"type:int(11);comment:用户类型 1超级管理员 2主账户 3子账户"`
        Password     string            `json:"-"  gorm:"type:varchar(255);comment:用户登录密码"`
        NickName     string            `json:"nickName" gorm:"type:varchar(255);default:系统用户;comment:用户昵称"`
        RealName     string            `json:"realName" gorm:"type:varchar(255);comment:用户真实姓名"`
        DepartmentId int               `json:"departmentId" gorm:"type:int(11);comment:用户部门ID"`
        HeaderImg    string            `json:"headerImg" gorm:"type:varchar(255);comment:用户头像"`
        Phone        string            `json:"phone"  gorm:"type:varchar(255);comment:用户手机号"`
        Email        string            `json:"email"  gorm:"type:varchar(255);comment:用户邮箱"`
        Pos          string            `json:"pos" gorm:"type:varchar(255);comment:岗位"`
        Menus        []Menu            `json:"-" gorm:"many2many:user_menu;"`                 // 用户菜单
        MenuIds      []uint            `json:"menuIds" gorm:"-"`                              // 菜单ID列表
        AuthorityId  uint              `json:"authorityId" gorm:"default:888;comment:用户角色ID"` // 用户角色ID
        Authority    Authority         `json:"authority" gorm:"foreignKey:AuthorityId"`
        gorm.Model   `json:"-"`
    }
    UserSearch struct {
@@ -50,28 +40,6 @@
func (slf User) TableName() string {
    return "user"
}
func (slf *User) BeforeCreate(tx *gorm.DB) error {
    slf.CreateTime = time.Now().Unix()
    slf.UpdateTime = slf.CreateTime
    return nil
}
func (slf *User) BeforeSave(tx *gorm.DB) error {
    slf.UpdateTime = time.Now().Unix()
    return nil
}
func (slf *User) BeforeUpdate(tx *gorm.DB) error {
    slf.UpdateTime = time.Now().Unix()
    return nil
}
func (slf *User) AfterFind(tx *gorm.DB) error {
    slf.CreateAt = time.Unix(slf.CreateTime, 0).Format("2006-01-02 15:04:05")
    slf.UpdateAt = time.Unix(slf.UpdateTime, 0).Format("2006-01-02 15:04:05")
    return nil
}
func NewUserSearch(db *gorm.DB) *UserSearch {
@@ -101,16 +69,6 @@
    return slf
}
func (slf *UserSearch) SetParentId(parentId string) *UserSearch {
    slf.ParentId = parentId
    return slf
}
func (slf *UserSearch) SetParentName(parentName string) *UserSearch {
    slf.ParentName = parentName
    return slf
}
func (slf *UserSearch) SetUserName(username string) *UserSearch {
    slf.Username = username
    return slf
@@ -130,10 +88,6 @@
    if slf.Username != "" {
        db = db.Where("username = ?", slf.Username)
    }
    if slf.ParentName != "" {
        db = db.Where("parent_name = ?", slf.ParentName)
    }
    if slf.Keyword != "" {
service/container_docker.go
@@ -2,7 +2,6 @@
import (
    "aps_crm/conf"
    "aps_crm/model"
    "aps_crm/pkg/logx"
    "aps_crm/utils"
    "context"
@@ -232,18 +231,18 @@
}
func (slf *DockerImpl) initContainerNotFound() {
    users, _ := model.NewUserSearch(nil).FindNotTotal()
    for _, u := range users {
        if u.ParentId == "" { //是主账号
            //判断容器是否存在时是否需要加锁?用户注册主账号时是异步请求,创建docker容器没有冲突,因为容器的id不同。
            //拉起容器和用户主动启动容器也没有时间冲突
            _, e := slf.Exist(u.ID)
            if e != ContainerNotFound {
                continue
            }
            if err := slf.Init(u.ID); err != nil {
                logx.Errorf("init ContainerNotFound err:", err)
            }
        }
    }
    //users, _ := model.NewUserSearch(nil).FindNotTotal()
    //for _, u := range users {
    //    if u.ParentId == "" { //是主账号
    //        //判断容器是否存在时是否需要加锁?用户注册主账号时是异步请求,创建docker容器没有冲突,因为容器的id不同。
    //        //拉起容器和用户主动启动容器也没有时间冲突
    //        _, e := slf.Exist(u.ID)
    //        if e != ContainerNotFound {
    //            continue
    //        }
    //        if err := slf.Init(u.ID); err != nil {
    //            logx.Errorf("init ContainerNotFound err:", err)
    //        }
    //    }
    //}
}
service/user.go
@@ -57,9 +57,7 @@
    if err != nil {
        return err
    }
    if user.Ip != "" {
        return errors.New("该用户已配置集群,无法删除")
    }
    if user.UserType != constvar.UserTypeSub {
        return errors.New("该用户非子账户,无法删除")
    }
source/user.go
@@ -38,8 +38,8 @@
            Password: adminPassword,
            NickName: "admin",
            UserType: constvar.UserTypeSuper,
            ParentId: "basic", // 超级管理员账户的父亲为空字符串,或者起个名字
            Enable:   true,
            //ParentId: "basic", // 超级管理员账户的父亲为空字符串,或者起个名字
            //Enable:   true,
        },
    }
    if err = model.NewUserSearch(nil).CreateBatch(entities); err != nil {