From 47ae9c16592977d8c74b5ff19eae71fabc88b6a9 Mon Sep 17 00:00:00 2001 From: TianJun Date: Thu, 29 Apr 2021 16:13:13 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=86=E8=8A=82fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/base_do.go | 15 +++++++------- src/base/base_dto.go | 2 +- src/config/datasource_config.go | 21 +++++++++++++++++--- src/main.go | 2 +- src/middleware/gin_router.go | 9 ++++----- src/middleware/jwt_engine.go | 2 +- src/{ => middleware}/urls.go | 2 +- src/modules/system/dao/user_dao.go | 4 ++-- src/modules/system/entity/user.go | 23 ---------------------- src/modules/system/rest/user_rest.go | 6 +++--- src/modules/system/service/user_service.go | 4 ++-- src/util/util.go | 4 ++-- 12 files changed, 42 insertions(+), 52 deletions(-) rename src/{ => middleware}/urls.go (99%) delete mode 100644 src/modules/system/entity/user.go diff --git a/src/base/base_do.go b/src/base/base_do.go index 07ea58b..c7e2c55 100644 --- a/src/base/base_do.go +++ b/src/base/base_do.go @@ -6,16 +6,15 @@ import ( type LingYeDO struct { // 自增ID - ID uint `gorm:"column:id;type:bigint(20);not null;primary_key;AUTO_INCREMENT" json:"id" form:"id"` + ID uint64 `gorm:"column:id;type:bigint(20);not null;primary_key;AUTO_INCREMENT;comment:'自增ID'"` // 创建时间 - CreatedAt time.Time + CreatedAt time.Time `gorm:"comment:'创建时间'"` // 更新时间 - UpdatedAt time.Time + UpdatedAt time.Time `gorm:"comment:'更新时间'"` // 删除时间 - DeletedAt *time.Time `sql:"index"` - // 扩展字段 + DeletedAt *time.Time `gorm:"comment:'删除时间'" sql:"index"` // 创建者 - CreatedBy string - // 删除者 - UpdatedBy string + CreatedBy string `gorm:"comment:'创建者'"` + // 更新者 + UpdatedBy string `gorm:"comment:'更新者'"` } diff --git a/src/base/base_dto.go b/src/base/base_dto.go index 7c1c403..7d90c87 100644 --- a/src/base/base_dto.go +++ b/src/base/base_dto.go @@ -1,5 +1,5 @@ package base type LingYeDTO struct { - ID uint `json:"id" form:"id"` + ID uint64 `json:"id" form:"id"` } diff --git a/src/config/datasource_config.go b/src/config/datasource_config.go index 33cd67f..d7240ca 100644 --- a/src/config/datasource_config.go +++ b/src/config/datasource_config.go @@ -6,8 +6,8 @@ import ( _ "github.com/jinzhu/gorm/dialects/mssql" _ "github.com/jinzhu/gorm/dialects/mysql" _ "github.com/jinzhu/gorm/dialects/postgres" - //_ "github.com/jinzhu/gorm/dialects/sqlite" "lingye-gin/src/modules/system/entity" + "strings" ) @@ -50,10 +50,25 @@ func (v DataSourcePool) Connect() DataSourcePool { return v } -// 统一在这里注册数据表 +// 注册数据表 func (DataSourcePool) LoadEntity() { // 自动迁移模式 if SqlExcutor != nil { - SqlExcutor.AutoMigrate(&entity.User{}) + SqlExcutor.AutoMigrate( + &entity.Dept{}, + &entity.DictData{}, + &entity.DictType{}, + &entity.Job{}, + &entity.Menu{}, + &entity.Notice{}, + &entity.OperateLog{}, + &entity.Position{}, + &entity.Role{}, + &entity.RoleDept{}, + &entity.RoleMenu{}, + &entity.User{}, + &entity.UserPosition{}, + &entity.UserRole{}, + ) } } diff --git a/src/main.go b/src/main.go index 2c402cb..b20b1d5 100644 --- a/src/main.go +++ b/src/main.go @@ -10,7 +10,7 @@ func main() { new(config.ApplicationProperties).Init() // 初始化redis new(middleware.RedisPool).Init() - // 初始化gorm + // 初始化gorm, 注册表 new(config.DataSourcePool).Connect().LoadEntity() // 延时调用函数 defer config.SqlExcutor.Close() diff --git a/src/middleware/gin_router.go b/src/middleware/gin_router.go index 3fbe04a..598ff42 100644 --- a/src/middleware/gin_router.go +++ b/src/middleware/gin_router.go @@ -3,7 +3,6 @@ package middleware import ( "fmt" "github.com/gin-gonic/gin" - "lingye-gin/src" "lingye-gin/src/config" "strings" ) @@ -19,11 +18,11 @@ func (v GinRouter) Init(r *gin.Engine) { // 一般路由映射关系 groupMap := make(map[string]*gin.RouterGroup) // api路由映射关系 - apiUrls := make([]main.RequestApi, 0) + apiUrls := make([]RequestApi, 0) // api组名称 apiGroupName := "api" - for _, normalRa := range main.Urls { + for _, normalRa := range Urls { // 判断是否在某一组下 if strings.Compare(normalRa.GroupName, "") == 0 { // 批量处理 @@ -68,7 +67,7 @@ func (v GinRouter) Init(r *gin.Engine) { config.Logger.Info("GinRouter Ok") } -func handle(request main.RequestApi, engine *gin.Engine) bool { +func handle(request RequestApi, engine *gin.Engine) bool { // get if strings.Compare(request.Mode, "get") == 0 { engine.GET(request.RelativePath, request.HandleFunction) @@ -92,7 +91,7 @@ func handle(request main.RequestApi, engine *gin.Engine) bool { return false } -func handleGroup(request main.RequestApi, group *gin.RouterGroup) bool { +func handleGroup(request RequestApi, group *gin.RouterGroup) bool { // get if strings.Compare(request.Mode, "get") == 0 { group.GET(request.RelativePath, request.HandleFunction) diff --git a/src/middleware/jwt_engine.go b/src/middleware/jwt_engine.go index a43d5ad..6048d16 100644 --- a/src/middleware/jwt_engine.go +++ b/src/middleware/jwt_engine.go @@ -22,7 +22,7 @@ const HeadLingYe = "LingYe" // 继承jwt提供给载荷,扩展自己所需字段 type JwtClaims struct { jwt.StandardClaims - UserId uint `json:"id"` // 用户ID + UserId uint64 `json:"id"` // 用户ID UserName string `json:"username"` // 用户名 NickName string `json:"nickname"` // 用户昵称 Email string `json:"email"` // 邮箱账号 diff --git a/src/urls.go b/src/middleware/urls.go similarity index 99% rename from src/urls.go rename to src/middleware/urls.go index 0e4971e..88a7327 100644 --- a/src/urls.go +++ b/src/middleware/urls.go @@ -1,4 +1,4 @@ -package main +package middleware import ( "github.com/gin-gonic/gin" diff --git a/src/modules/system/dao/user_dao.go b/src/modules/system/dao/user_dao.go index 78a8b81..55ae989 100644 --- a/src/modules/system/dao/user_dao.go +++ b/src/modules/system/dao/user_dao.go @@ -15,7 +15,7 @@ func (dao UserDAO) Insert(resource *entity.User) { } // ID查询 -func (dao UserDAO) SelectOne(id uint) entity.User { +func (dao UserDAO) SelectOne(id uint64) entity.User { var user entity.User config.SqlExcutor.First(&user, id) return user @@ -65,7 +65,7 @@ func (UserDAO) UpdateById(user *entity.User) { } // 删除 -func (UserDAO) DeleteById(id uint) { +func (UserDAO) DeleteById(id uint64) { user := entity.User{} user.ID = id config.SqlExcutor.Delete(&user) diff --git a/src/modules/system/entity/user.go b/src/modules/system/entity/user.go deleted file mode 100644 index a3a0405..0000000 --- a/src/modules/system/entity/user.go +++ /dev/null @@ -1,23 +0,0 @@ -package entity - -import ( - "lingye-gin/src/base" -) - -type User struct { - base.LingYeDO - UserName string `gorm:"column:user_name;type:varchar(255);not null;index:idx_username"` - NickName string `gorm:"column:nick_name;type:varchar(255)"` - Email string `gorm:"column:email;type:varchar(255)"` - PhoneNumber string `gorm:"column:phone_number;type:varchar(255)"` - Sex string `gorm:"column:sex;type:varchar(255)"` - Avatar string `gorm:"column:avatar;type:varchar(255)"` - Password string `gorm:"column:password;type:varchar(255);not null"` - Status string `gorm:"column:status;type:varchar(255);not null"` - Remark string `gorm:"column:remark;type:text(0)"` -} - -// 设置User的表名为`sys_user` -func (User) TableName() string { - return "sys_user" -} diff --git a/src/modules/system/rest/user_rest.go b/src/modules/system/rest/user_rest.go index d644b66..e33854e 100644 --- a/src/modules/system/rest/user_rest.go +++ b/src/modules/system/rest/user_rest.go @@ -26,7 +26,7 @@ func DescribeUsers(c *gin.Context) { func DescribeUserById(c *gin.Context) { id := c.Params.ByName("id") - user := userService.DescribeUserById(util.StringToUInt(id)) + user := userService.DescribeUserById(util.StringToUInt64(id)) if user.ID == 0 { c.JSON(http.StatusNotFound, gin.H{ "code": http.StatusNotFound, @@ -54,7 +54,7 @@ func CreateUser(c *gin.Context) { func ModifyUserById(c *gin.Context) { id := c.Params.ByName("id") - localUser := userService.DescribeUserById(util.StringToUInt(id)) + localUser := userService.DescribeUserById(util.StringToUInt64(id)) if localUser.ID == 0 { c.JSON(http.StatusNotFound, gin.H{ "code": http.StatusNotFound, @@ -74,7 +74,7 @@ func ModifyUserById(c *gin.Context) { func DeleteUserById(c *gin.Context) { id := c.Params.ByName("id") - uid := util.StringToUInt(id) + uid := util.StringToUInt64(id) localUser := userService.DescribeUserById(uid) if localUser.ID == 0 { c.JSON(http.StatusNotFound, gin.H{ diff --git a/src/modules/system/service/user_service.go b/src/modules/system/service/user_service.go index 78e4a0a..a93914f 100644 --- a/src/modules/system/service/user_service.go +++ b/src/modules/system/service/user_service.go @@ -21,7 +21,7 @@ func (service UserService) Save(resource *dto.UserDTO) { service.userDao.Insert(record) } -func (service UserService) RemoveById(id uint) { +func (service UserService) RemoveById(id uint64) { if id == 0 { panic("id is zero") } @@ -41,6 +41,6 @@ func (service UserService) DescribeUsers(condition query.UserQuery) ([]entity.Us return service.userDao.SelectPage(condition) } -func (service UserService) DescribeUserById(id uint) entity.User { +func (service UserService) DescribeUserById(id uint64) entity.User { return service.userDao.SelectOne(id) } diff --git a/src/util/util.go b/src/util/util.go index 80a6cc9..c9ec764 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -129,7 +129,7 @@ func StructCopy(source interface{}, target interface{}) { } // 字符串转uint -func StringToUInt(number string) uint { +func StringToUInt64(number string) uint64 { result, _ := strconv.Atoi(number) - return uint(result) + return uint64(result) }