From 6466292524b596c706a118d616f4e1385cf45983 Mon Sep 17 00:00:00 2001 From: tianjun Date: Wed, 18 Jan 2023 18:02:58 +0800 Subject: [PATCH] fix: import cycle --- modules/core/api/v1/cron_job_api.go | 5 +- modules/core/api/v1/login_api.go | 3 +- modules/core/api/v1/user_api.go | 4 +- modules/core/cache/config_cache.go | 5 +- modules/core/cache/dict_cache.go | 3 +- modules/core/dao/config_dao.go | 4 +- modules/core/dao/dict_data_dao.go | 4 +- pkg/cache/index.go | 52 +++++++++++++--- pkg/config/index.go | 66 -------------------- pkg/middleware/logger/logger.go | 8 +-- pkg/util/my_util.go | 94 +++++++++++++++++++---------- 11 files changed, 122 insertions(+), 126 deletions(-) diff --git a/modules/core/api/v1/cron_job_api.go b/modules/core/api/v1/cron_job_api.go index 713a434..c209c81 100644 --- a/modules/core/api/v1/cron_job_api.go +++ b/modules/core/api/v1/cron_job_api.go @@ -5,7 +5,6 @@ import ( "cutego/modules/core/api/v1/response" "cutego/modules/core/dataobject" "cutego/modules/core/service" - "cutego/pkg/common" "cutego/pkg/cronjob" "cutego/pkg/resp" "cutego/pkg/util" @@ -36,7 +35,7 @@ func (a CronJobApi) List(c *gin.Context) { // GetById 根据id获取任务详情 func (a CronJobApi) GetById(c *gin.Context) { jobId := c.Param("jobId") - info := a.cronJobService.GetInfo(common.StringToInt64(jobId)) + info := a.cronJobService.GetInfo(util.StringToInt64(jobId)) resp.OK(c, util.DeepCopy(info, &response.CronJobResponse{})) } @@ -73,7 +72,7 @@ func (a CronJobApi) Add(c *gin.Context) { // Remove 删除定时任务 func (a CronJobApi) Remove(c *gin.Context) { - jobId := common.StringToInt64(c.Param("jobId")) + jobId := util.StringToInt64(c.Param("jobId")) funcAlias := c.Param("funcAlias") if a.cronJobService.GetInfo(jobId).Status == "1" { diff --git a/modules/core/api/v1/login_api.go b/modules/core/api/v1/login_api.go index 8cffaae..88f28bb 100644 --- a/modules/core/api/v1/login_api.go +++ b/modules/core/api/v1/login_api.go @@ -5,7 +5,6 @@ import ( cache2 "cutego/modules/core/cache" "cutego/modules/core/dataobject" "cutego/modules/core/service" - "cutego/pkg/common" "cutego/pkg/page" "cutego/pkg/resp" "cutego/pkg/tree/tree_menu" @@ -46,7 +45,7 @@ func (a LoginApi) Login(c *gin.Context) { } func (a LoginApi) handleLoginInfo(c *gin.Context, body request2.LoginBody, loginStatus bool) { - status := common.If(loginStatus, "1", "0") + status := util.IF(loginStatus, "1", "0") clientIp := a.loginInfoService.GetRequestClientIp(c) var location string diff --git a/modules/core/api/v1/user_api.go b/modules/core/api/v1/user_api.go index 731aa17..893af71 100644 --- a/modules/core/api/v1/user_api.go +++ b/modules/core/api/v1/user_api.go @@ -5,7 +5,7 @@ import ( "cutego/modules/core/api/v1/response" "cutego/modules/core/dataobject" "cutego/modules/core/service" - "cutego/pkg/common" + "cutego/pkg/config" "cutego/pkg/excels" "cutego/pkg/logging" "cutego/pkg/page" @@ -287,7 +287,7 @@ func (a UserApi) UpdatePwd(c *gin.Context) { // Avatar 修改头像 func (a UserApi) Avatar(c *gin.Context) { - dirPath := common.GetDirPath("avatar") + dirPath := config.GetDirPath("avatar") file, _, err := c.Request.FormFile("avatarFile") fileName := gotool.IdUtils.IdUUIDToRan(true) + ".jpg" filePath := dirPath + fileName diff --git a/modules/core/cache/config_cache.go b/modules/core/cache/config_cache.go index 9dc234d..d9930e2 100644 --- a/modules/core/cache/config_cache.go +++ b/modules/core/cache/config_cache.go @@ -3,7 +3,6 @@ package cache import ( models2 "cutego/modules/core/dataobject" "cutego/pkg/cache" - "cutego/pkg/common" "cutego/pkg/constant" ) @@ -13,13 +12,13 @@ import ( func GetRedisConfig(key string) *models2.SysConfig { val := cache.GetCache(constant.RedisConst{}.GetRedisConfigKey() + key) s := new(models2.SysConfig) - return common.JsonToStruct(val, s).(*models2.SysConfig) + return cache.JsonToStruct(val, s).(*models2.SysConfig) } // SetRedisConfig 将配置存入缓存 // @Param config models2.SysConfig func SetRedisConfig(config models2.SysConfig) { - cache.SetCache(config.ConfigKey, common.StructToJson(config)) + cache.SetCache(config.ConfigKey, cache.StructToJson(config)) } // RemoveRedisConfig 从缓存中删除配置 diff --git a/modules/core/cache/dict_cache.go b/modules/core/cache/dict_cache.go index b55c916..d00272f 100644 --- a/modules/core/cache/dict_cache.go +++ b/modules/core/cache/dict_cache.go @@ -3,7 +3,6 @@ package cache import ( models2 "cutego/modules/core/dataobject" "cutego/pkg/cache" - "cutego/pkg/common" "cutego/pkg/constant" ) @@ -13,7 +12,7 @@ import ( func GetRedisDict(key string) interface{} { val := cache.GetCache(key) s := make([]interface{}, 0) - return common.JsonToStruct(val, s) + return cache.JsonToStruct(val, s) } // SetRedisDict 保存字典数据 diff --git a/modules/core/dao/config_dao.go b/modules/core/dao/config_dao.go index 3d80530..065e96a 100644 --- a/modules/core/dao/config_dao.go +++ b/modules/core/dao/config_dao.go @@ -3,7 +3,7 @@ package dao import ( "cutego/modules/core/api/v1/request" "cutego/modules/core/dataobject" - "cutego/pkg/common" + "cutego/pkg/cache" "cutego/pkg/constant" "cutego/pkg/logging" "cutego/pkg/page" @@ -160,7 +160,7 @@ func init() { return } for _, sysConfig := range configs { - refs.RedisDB.SET(constant.RedisConst{}.GetRedisConfigKey()+sysConfig.ConfigKey, common.StructToJson(map[string]interface{}{ + refs.RedisDB.SET(constant.RedisConst{}.GetRedisConfigKey()+sysConfig.ConfigKey, cache.StructToJson(map[string]interface{}{ "configId": sysConfig.ConfigId, "configName": sysConfig.ConfigName, "configKey": sysConfig.ConfigKey, diff --git a/modules/core/dao/dict_data_dao.go b/modules/core/dao/dict_data_dao.go index 1c66369..3e163e0 100644 --- a/modules/core/dao/dict_data_dao.go +++ b/modules/core/dao/dict_data_dao.go @@ -3,7 +3,7 @@ package dao import ( "cutego/modules/core/api/v1/request" "cutego/modules/core/dataobject" - "cutego/pkg/common" + "cutego/pkg/cache" "cutego/pkg/constant" "cutego/pkg/logging" "cutego/pkg/page" @@ -152,6 +152,6 @@ func init() { }) } } - refs.RedisDB.SET(constant.RedisConst{}.GetRedisDictKey()+dictType.DictType, common.StructToJson(dictData)) + refs.RedisDB.SET(constant.RedisConst{}.GetRedisDictKey()+dictType.DictType, cache.StructToJson(dictData)) } } diff --git a/pkg/cache/index.go b/pkg/cache/index.go index 1bcdb12..bedc09d 100644 --- a/pkg/cache/index.go +++ b/pkg/cache/index.go @@ -2,9 +2,9 @@ package cache import ( "cutego/pkg/constant" - "cutego/pkg/logging" - "cutego/pkg/util" "cutego/refs" + "encoding/json" + "fmt" ) // RemoveList 批量根据Key删除数据 @@ -19,7 +19,7 @@ func RemoveList(list []string) { func RemoveCache(key string) int { del, err := refs.RedisDB.DEL(key) if err != nil { - logging.ErrorLog(err) + fmt.Println(err.Error()) } return del } @@ -30,7 +30,7 @@ func RemoveCache(key string) int { func GetCache(key string) string { val, err := refs.RedisDB.GET(key) if err != nil { - logging.ErrorLog(constant.RedisConst{}.GetRedisError(), err.Error()) + fmt.Println(constant.RedisConst{}.GetRedisError(), err.Error()) return "" } return val @@ -41,9 +41,9 @@ func GetCache(key string) string { // @Param value 值 // @Return 新增的行数 func SetCache(key string, value interface{}) int { - n, err := refs.RedisDB.SET(key, util.StructToJson(value)) + n, err := refs.RedisDB.SET(key, StructToJson(value)) if err != nil { - logging.ErrorLog(constant.RedisConst{}.GetRedisError(), err.Error()) + fmt.Println(constant.RedisConst{}.GetRedisError(), err.Error()) return 0 } return int(n) @@ -55,5 +55,43 @@ func SetCache(key string, value interface{}) int { // @Param sec 过期时间(单位: 秒) // @Return 新增的行数 func SetCacheTTL(key string, value interface{}, sec int) { - refs.RedisDB.SETEX(key, sec, util.StructToJson(value)) + refs.RedisDB.SETEX(key, sec, StructToJson(value)) +} + +// 结构体、Map等转Json字符串 +// @Param v interface{} +// @Return Json字符串 +func StructToJson(v interface{}) string { + jsonBytes, err := json.Marshal(&v) + if err != nil { + fmt.Println(err.Error()) + return "" + } + s := string(jsonBytes) + //DebugLogf("StructToJson, json=%s", s) + return s +} + +// Json字符串转结构体、Map等 +// +// 单个对象 +// s := new(models2.SysConfig) +// return common.JsonToStruct(get, s).(*models2.SysConfig) +// +// 切片(interface{}.(期望类型)) +// s := make([]interface {}, 0) +// target := common.JsonToStruct(get, s) +// target.([]dataobject.SysDictData) +// +// @Param data Json字符串 +// @Param s 容器(结构体、Map等) +// @Return interface{} +func JsonToStruct(data string, s interface{}) interface{} { + err := json.Unmarshal([]byte(data), &s) + if err != nil { + fmt.Println(err.Error()) + return nil + } + //common.DebugLogf("JsonToStruct, obj=%v", s) + return s } diff --git a/pkg/config/index.go b/pkg/config/index.go index 6e8b9f2..5eb73b9 100644 --- a/pkg/config/index.go +++ b/pkg/config/index.go @@ -3,13 +3,11 @@ package config import ( config "cutego/pkg/config/models" "cutego/pkg/logging" - "encoding/json" "fmt" "gopkg.in/yaml.v2" "io/ioutil" "os" "runtime" - "strconv" ) var ( @@ -84,44 +82,6 @@ func readAppYmlFile(resourcePath string) { LoadYamlFile(applicationEnvFilePath, AppEnvConfig) } -// IntToString int转string -func IntToString(n int) string { - return strconv.Itoa(n) -} - -// StringToInt string转int -func StringToInt(s string) int { - i, err := strconv.Atoi(s) - if err != nil { - panic(err) - } - return i -} - -// StringToInt64 string转int64 -func StringToInt64(s string) int64 { - i, err := strconv.ParseInt(s, 10, 64) - if err != nil { - panic(err) - } - return i -} - -// Int64ToString int64转string -func Int64ToString(n int64) string { - return strconv.FormatInt(n, 10) -} - -func mapToBytes(data map[string]interface{}) []byte { - bytes, _ := json.Marshal(data) - return bytes -} - -// MapToStruct map转struct -func MapToStruct(data map[string]interface{}, v interface{}) { - _ = json.Unmarshal(mapToBytes(data), v) -} - // GetDirPath 获取目录路径 func GetDirPath(resType string) string { sysType := runtime.GOOS @@ -165,32 +125,6 @@ func GetDirPath(resType string) string { return AppCoreConfig.CuteGoConfig.File.Linux.Logs } -// CreateAllDir 递归创建文件夹 -func CreateAllDir(filePath string) error { - if !IsFileOrDirExist(filePath) { - err := os.MkdirAll(filePath, os.ModePerm) - if err != nil { - fmt.Println("创建文件夹失败, error info:", err) - return err - } - return err - } - return nil -} - -// IsFileOrDirExist 判断所给路径文件/文件夹是否存在(返回true是存在) -func IsFileOrDirExist(path string) bool { - // os.Stat获取文件信息 - _, err := os.Stat(path) - if err != nil { - if os.IsExist(err) { - return true - } - return false - } - return true -} - func init() { // 资源文件所在的路径 resourcePath := GetRootPath() diff --git a/pkg/middleware/logger/logger.go b/pkg/middleware/logger/logger.go index 5a495a7..1e19a42 100644 --- a/pkg/middleware/logger/logger.go +++ b/pkg/middleware/logger/logger.go @@ -1,9 +1,9 @@ package logger import ( - "cutego/pkg/common" "cutego/pkg/config" "cutego/pkg/logging" + "cutego/pkg/util" "fmt" "github.com/druidcaesa/gotool" "github.com/gin-gonic/gin" @@ -17,10 +17,10 @@ import ( // LoggerToFile 日志记录到文件 func LoggerToFile() gin.HandlerFunc { - dirPath := common.GetDirPath("logging") + dirPath := config.GetDirPath("logging") fileName := path.Join(dirPath, "application.logging") - if !common.IsFileOrDirExist(dirPath) { - err := common.CreateAllDir(dirPath) + if !util.IsFileOrDirExist(dirPath) { + err := util.CreateAllDir(dirPath) if err != nil { logging.ErrorLog(err) } diff --git a/pkg/util/my_util.go b/pkg/util/my_util.go index dc4a044..07743ad 100644 --- a/pkg/util/my_util.go +++ b/pkg/util/my_util.go @@ -1,8 +1,10 @@ package util import ( - "cutego/pkg/logging" "encoding/json" + "fmt" + "os" + "strconv" ) // IF 三元表达式 @@ -13,40 +15,66 @@ func IF(condition bool, trueValue interface{}, falseValue interface{}) interface return falseValue } -// 结构体、Map等转Json字符串 -// @Param v interface{} -// @Return Json字符串 -func StructToJson(v interface{}) string { - jsonBytes, err := json.Marshal(&v) - if err != nil { - logging.ErrorLog(err) - return "" - } - s := string(jsonBytes) - //DebugLogf("StructToJson, json=%s", s) - return s +// IntToString int转string +func IntToString(n int) string { + return strconv.Itoa(n) } -// Json字符串转结构体、Map等 -// -// 单个对象 -// s := new(models2.SysConfig) -// return common.JsonToStruct(get, s).(*models2.SysConfig) -// -// 切片(interface{}.(期望类型)) -// s := make([]interface {}, 0) -// target := common.JsonToStruct(get, s) -// target.([]dataobject.SysDictData) -// -// @Param data Json字符串 -// @Param s 容器(结构体、Map等) -// @Return interface{} -func JsonToStruct(data string, s interface{}) interface{} { - err := json.Unmarshal([]byte(data), &s) +// StringToInt string转int +func StringToInt(s string) int { + i, err := strconv.Atoi(s) if err != nil { - logging.ErrorLog(err) - return nil + panic(err) } - //common.DebugLogf("JsonToStruct, obj=%v", s) - return s + return i +} + +// StringToInt64 string转int64 +func StringToInt64(s string) int64 { + i, err := strconv.ParseInt(s, 10, 64) + if err != nil { + panic(err) + } + return i +} + +// Int64ToString int64转string +func Int64ToString(n int64) string { + return strconv.FormatInt(n, 10) +} + +func mapToBytes(data map[string]interface{}) []byte { + bytes, _ := json.Marshal(data) + return bytes +} + +// MapToStruct map转struct +func MapToStruct(data map[string]interface{}, v interface{}) { + _ = json.Unmarshal(mapToBytes(data), v) +} + +// CreateAllDir 递归创建文件夹 +func CreateAllDir(filePath string) error { + if !IsFileOrDirExist(filePath) { + err := os.MkdirAll(filePath, os.ModePerm) + if err != nil { + fmt.Println("创建文件夹失败, error info:", err) + return err + } + return err + } + return nil +} + +// IsFileOrDirExist 判断所给路径文件/文件夹是否存在(返回true是存在) +func IsFileOrDirExist(path string) bool { + // os.Stat获取文件信息 + _, err := os.Stat(path) + if err != nil { + if os.IsExist(err) { + return true + } + return false + } + return true }