fix: import cycle

This commit is contained in:
骑着蜗牛追导弹 2023-01-18 18:02:58 +08:00
parent 26f46f1eb6
commit 6466292524
11 changed files with 122 additions and 126 deletions

View File

@ -5,7 +5,6 @@ import (
"cutego/modules/core/api/v1/response" "cutego/modules/core/api/v1/response"
"cutego/modules/core/dataobject" "cutego/modules/core/dataobject"
"cutego/modules/core/service" "cutego/modules/core/service"
"cutego/pkg/common"
"cutego/pkg/cronjob" "cutego/pkg/cronjob"
"cutego/pkg/resp" "cutego/pkg/resp"
"cutego/pkg/util" "cutego/pkg/util"
@ -36,7 +35,7 @@ func (a CronJobApi) List(c *gin.Context) {
// GetById 根据id获取任务详情 // GetById 根据id获取任务详情
func (a CronJobApi) GetById(c *gin.Context) { func (a CronJobApi) GetById(c *gin.Context) {
jobId := c.Param("jobId") 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{})) resp.OK(c, util.DeepCopy(info, &response.CronJobResponse{}))
} }
@ -73,7 +72,7 @@ func (a CronJobApi) Add(c *gin.Context) {
// Remove 删除定时任务 // Remove 删除定时任务
func (a CronJobApi) Remove(c *gin.Context) { func (a CronJobApi) Remove(c *gin.Context) {
jobId := common.StringToInt64(c.Param("jobId")) jobId := util.StringToInt64(c.Param("jobId"))
funcAlias := c.Param("funcAlias") funcAlias := c.Param("funcAlias")
if a.cronJobService.GetInfo(jobId).Status == "1" { if a.cronJobService.GetInfo(jobId).Status == "1" {

View File

@ -5,7 +5,6 @@ import (
cache2 "cutego/modules/core/cache" cache2 "cutego/modules/core/cache"
"cutego/modules/core/dataobject" "cutego/modules/core/dataobject"
"cutego/modules/core/service" "cutego/modules/core/service"
"cutego/pkg/common"
"cutego/pkg/page" "cutego/pkg/page"
"cutego/pkg/resp" "cutego/pkg/resp"
"cutego/pkg/tree/tree_menu" "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) { 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) clientIp := a.loginInfoService.GetRequestClientIp(c)
var location string var location string

View File

@ -5,7 +5,7 @@ import (
"cutego/modules/core/api/v1/response" "cutego/modules/core/api/v1/response"
"cutego/modules/core/dataobject" "cutego/modules/core/dataobject"
"cutego/modules/core/service" "cutego/modules/core/service"
"cutego/pkg/common" "cutego/pkg/config"
"cutego/pkg/excels" "cutego/pkg/excels"
"cutego/pkg/logging" "cutego/pkg/logging"
"cutego/pkg/page" "cutego/pkg/page"
@ -287,7 +287,7 @@ func (a UserApi) UpdatePwd(c *gin.Context) {
// Avatar 修改头像 // Avatar 修改头像
func (a UserApi) Avatar(c *gin.Context) { func (a UserApi) Avatar(c *gin.Context) {
dirPath := common.GetDirPath("avatar") dirPath := config.GetDirPath("avatar")
file, _, err := c.Request.FormFile("avatarFile") file, _, err := c.Request.FormFile("avatarFile")
fileName := gotool.IdUtils.IdUUIDToRan(true) + ".jpg" fileName := gotool.IdUtils.IdUUIDToRan(true) + ".jpg"
filePath := dirPath + fileName filePath := dirPath + fileName

View File

@ -3,7 +3,6 @@ package cache
import ( import (
models2 "cutego/modules/core/dataobject" models2 "cutego/modules/core/dataobject"
"cutego/pkg/cache" "cutego/pkg/cache"
"cutego/pkg/common"
"cutego/pkg/constant" "cutego/pkg/constant"
) )
@ -13,13 +12,13 @@ import (
func GetRedisConfig(key string) *models2.SysConfig { func GetRedisConfig(key string) *models2.SysConfig {
val := cache.GetCache(constant.RedisConst{}.GetRedisConfigKey() + key) val := cache.GetCache(constant.RedisConst{}.GetRedisConfigKey() + key)
s := new(models2.SysConfig) s := new(models2.SysConfig)
return common.JsonToStruct(val, s).(*models2.SysConfig) return cache.JsonToStruct(val, s).(*models2.SysConfig)
} }
// SetRedisConfig 将配置存入缓存 // SetRedisConfig 将配置存入缓存
// @Param config models2.SysConfig // @Param config models2.SysConfig
func SetRedisConfig(config models2.SysConfig) { func SetRedisConfig(config models2.SysConfig) {
cache.SetCache(config.ConfigKey, common.StructToJson(config)) cache.SetCache(config.ConfigKey, cache.StructToJson(config))
} }
// RemoveRedisConfig 从缓存中删除配置 // RemoveRedisConfig 从缓存中删除配置

View File

@ -3,7 +3,6 @@ package cache
import ( import (
models2 "cutego/modules/core/dataobject" models2 "cutego/modules/core/dataobject"
"cutego/pkg/cache" "cutego/pkg/cache"
"cutego/pkg/common"
"cutego/pkg/constant" "cutego/pkg/constant"
) )
@ -13,7 +12,7 @@ import (
func GetRedisDict(key string) interface{} { func GetRedisDict(key string) interface{} {
val := cache.GetCache(key) val := cache.GetCache(key)
s := make([]interface{}, 0) s := make([]interface{}, 0)
return common.JsonToStruct(val, s) return cache.JsonToStruct(val, s)
} }
// SetRedisDict 保存字典数据 // SetRedisDict 保存字典数据

View File

@ -3,7 +3,7 @@ package dao
import ( import (
"cutego/modules/core/api/v1/request" "cutego/modules/core/api/v1/request"
"cutego/modules/core/dataobject" "cutego/modules/core/dataobject"
"cutego/pkg/common" "cutego/pkg/cache"
"cutego/pkg/constant" "cutego/pkg/constant"
"cutego/pkg/logging" "cutego/pkg/logging"
"cutego/pkg/page" "cutego/pkg/page"
@ -160,7 +160,7 @@ func init() {
return return
} }
for _, sysConfig := range configs { 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, "configId": sysConfig.ConfigId,
"configName": sysConfig.ConfigName, "configName": sysConfig.ConfigName,
"configKey": sysConfig.ConfigKey, "configKey": sysConfig.ConfigKey,

View File

@ -3,7 +3,7 @@ package dao
import ( import (
"cutego/modules/core/api/v1/request" "cutego/modules/core/api/v1/request"
"cutego/modules/core/dataobject" "cutego/modules/core/dataobject"
"cutego/pkg/common" "cutego/pkg/cache"
"cutego/pkg/constant" "cutego/pkg/constant"
"cutego/pkg/logging" "cutego/pkg/logging"
"cutego/pkg/page" "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))
} }
} }

52
pkg/cache/index.go vendored
View File

@ -2,9 +2,9 @@ package cache
import ( import (
"cutego/pkg/constant" "cutego/pkg/constant"
"cutego/pkg/logging"
"cutego/pkg/util"
"cutego/refs" "cutego/refs"
"encoding/json"
"fmt"
) )
// RemoveList 批量根据Key删除数据 // RemoveList 批量根据Key删除数据
@ -19,7 +19,7 @@ func RemoveList(list []string) {
func RemoveCache(key string) int { func RemoveCache(key string) int {
del, err := refs.RedisDB.DEL(key) del, err := refs.RedisDB.DEL(key)
if err != nil { if err != nil {
logging.ErrorLog(err) fmt.Println(err.Error())
} }
return del return del
} }
@ -30,7 +30,7 @@ func RemoveCache(key string) int {
func GetCache(key string) string { func GetCache(key string) string {
val, err := refs.RedisDB.GET(key) val, err := refs.RedisDB.GET(key)
if err != nil { if err != nil {
logging.ErrorLog(constant.RedisConst{}.GetRedisError(), err.Error()) fmt.Println(constant.RedisConst{}.GetRedisError(), err.Error())
return "" return ""
} }
return val return val
@ -41,9 +41,9 @@ func GetCache(key string) string {
// @Param value 值 // @Param value 值
// @Return 新增的行数 // @Return 新增的行数
func SetCache(key string, value interface{}) int { 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 { if err != nil {
logging.ErrorLog(constant.RedisConst{}.GetRedisError(), err.Error()) fmt.Println(constant.RedisConst{}.GetRedisError(), err.Error())
return 0 return 0
} }
return int(n) return int(n)
@ -55,5 +55,43 @@ func SetCache(key string, value interface{}) int {
// @Param sec 过期时间(单位: 秒) // @Param sec 过期时间(单位: 秒)
// @Return 新增的行数 // @Return 新增的行数
func SetCacheTTL(key string, value interface{}, sec int) { 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
} }

View File

@ -3,13 +3,11 @@ package config
import ( import (
config "cutego/pkg/config/models" config "cutego/pkg/config/models"
"cutego/pkg/logging" "cutego/pkg/logging"
"encoding/json"
"fmt" "fmt"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"os" "os"
"runtime" "runtime"
"strconv"
) )
var ( var (
@ -84,44 +82,6 @@ func readAppYmlFile(resourcePath string) {
LoadYamlFile(applicationEnvFilePath, AppEnvConfig) 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 获取目录路径 // GetDirPath 获取目录路径
func GetDirPath(resType string) string { func GetDirPath(resType string) string {
sysType := runtime.GOOS sysType := runtime.GOOS
@ -165,32 +125,6 @@ func GetDirPath(resType string) string {
return AppCoreConfig.CuteGoConfig.File.Linux.Logs 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() { func init() {
// 资源文件所在的路径 // 资源文件所在的路径
resourcePath := GetRootPath() resourcePath := GetRootPath()

View File

@ -1,9 +1,9 @@
package logger package logger
import ( import (
"cutego/pkg/common"
"cutego/pkg/config" "cutego/pkg/config"
"cutego/pkg/logging" "cutego/pkg/logging"
"cutego/pkg/util"
"fmt" "fmt"
"github.com/druidcaesa/gotool" "github.com/druidcaesa/gotool"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -17,10 +17,10 @@ import (
// LoggerToFile 日志记录到文件 // LoggerToFile 日志记录到文件
func LoggerToFile() gin.HandlerFunc { func LoggerToFile() gin.HandlerFunc {
dirPath := common.GetDirPath("logging") dirPath := config.GetDirPath("logging")
fileName := path.Join(dirPath, "application.logging") fileName := path.Join(dirPath, "application.logging")
if !common.IsFileOrDirExist(dirPath) { if !util.IsFileOrDirExist(dirPath) {
err := common.CreateAllDir(dirPath) err := util.CreateAllDir(dirPath)
if err != nil { if err != nil {
logging.ErrorLog(err) logging.ErrorLog(err)
} }

View File

@ -1,8 +1,10 @@
package util package util
import ( import (
"cutego/pkg/logging"
"encoding/json" "encoding/json"
"fmt"
"os"
"strconv"
) )
// IF 三元表达式 // IF 三元表达式
@ -13,40 +15,66 @@ func IF(condition bool, trueValue interface{}, falseValue interface{}) interface
return falseValue return falseValue
} }
// 结构体、Map等转Json字符串 // IntToString int转string
// @Param v interface{} func IntToString(n int) string {
// @Return Json字符串 return strconv.Itoa(n)
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
} }
// Json字符串转结构体、Map等 // StringToInt string转int
// func StringToInt(s string) int {
// 单个对象 i, err := strconv.Atoi(s)
// 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 { if err != nil {
logging.ErrorLog(err) panic(err)
return nil
} }
//common.DebugLogf("JsonToStruct, obj=%v", s) return i
return s }
// 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
} }