diff --git a/server/api/v1/example/exa_breakpoint_continue.go b/server/api/v1/example/exa_breakpoint_continue.go index fef244fb9..715d7f08d 100644 --- a/server/api/v1/example/exa_breakpoint_continue.go +++ b/server/api/v1/example/exa_breakpoint_continue.go @@ -2,7 +2,7 @@ package example import ( "fmt" - "io/ioutil" + "io" "mime/multipart" "strconv" @@ -48,7 +48,7 @@ func (b *FileUploadAndDownloadApi) BreakpointContinue(c *gin.Context) { fmt.Println(err) } }(f) - cen, _ := ioutil.ReadAll(f) + cen, _ := io.ReadAll(f) if !utils.CheckMd5(cen, chunkMd5) { global.GVA_LOG.Error("检查md5失败!", zap.Error(err)) response.FailWithMessage("检查md5失败", c) diff --git a/server/config.yaml b/server/config.yaml index 6851e4b75..ee0de0c85 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -2,43 +2,43 @@ # jwt configuration jwt: - signing-key: 'qmPlus' - expires-time: 604800 - buffer-time: 86400 - issuer: 'qmPlus' + signing-key: qmPlus + expires-time: 7d + buffer-time: 1d + issuer: qmPlus # zap logger configuration zap: - level: 'info' - format: 'console' - prefix: '[github.com/flipped-aurora/gin-vue-admin/server]' - director: 'log' + level: info + format: console + prefix: [github.com/flipped-aurora/gin-vue-admin/server] + director: log show-line: true - encode-level: 'LowercaseColorLevelEncoder' - stacktrace-key: 'stacktrace' + encode-level: LowercaseColorLevelEncoder + stacktrace-key: stacktrace log-in-console: true # redis configuration redis: db: 0 - addr: '127.0.0.1:6379' - password: '' + addr: 127.0.0.1:6379 + password: "" # email configuration email: - to: 'xxx@qq.com' + to: xxx@qq.com port: 465 - from: 'xxx@163.com' - host: 'smtp.163.com' + from: xxx@163.com + host: smtp.163.com is-ssl: true - secret: 'xxx' - nickname: 'test' + secret: xxx + nickname: test # system configuration system: - env: 'public' # Change to "develop" to skip authentication for development mode + env: public # Change to "develop" to skip authentication for development mode addr: 8888 - db-type: 'mysql' - oss-type: 'local' # 控制oss选择走本地还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置 + db-type: mysql + oss-type: local # 控制oss选择走本地还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置 use-redis: false # 使用redis use-multipoint: false # IP限制次数 一个小时15000次 @@ -55,12 +55,12 @@ captcha: # mysql connect configuration # 未初始化之前请勿手动修改数据库信息!!!如果一定要手动初始化请看(https://www.github.com/flipped-aurora/gin-vue-admin/server.com/docs/first) mysql: - path: '' - port: '' - config: '' - db-name: '' - username: '' - password: '' + path: "" + port: "" + config: "" + db-name: "" + username: "" + password: "" max-idle-conns: 10 max-open-conns: 100 log-mode: "" @@ -69,12 +69,12 @@ mysql: # pgsql connect configuration # 未初始化之前请勿手动修改数据库信息!!!如果一定要手动初始化请看(https://www.github.com/flipped-aurora/gin-vue-admin/server.com/docs/first) pgsql: - path: '' - port: '' - config: '' - db-name: '' - username: '' - password: '' + path: "" + port: "" + config: "" + db-name: "" + username: "" + password: "" max-idle-conns: 10 max-open-conns: 100 log-mode: "" @@ -84,12 +84,12 @@ db-list: - disabled: true # 是否启用 type: "" # 数据库的类型,目前支持mysql、pgsql alias-name: "" # 数据库的名称,注意: alias-name 需要在db-list中唯一 - path: '' - port: '' - config: '' - db-name: '' - username: '' - password: '' + path: "" + port: "" + config: "" + db-name: "" + username: "" + password: "" max-idle-conns: 10 max-open-conns: 100 log-mode: "" @@ -98,8 +98,8 @@ db-list: # local configuration local: - path: 'uploads/file' # 访问路径 - store-path: 'uploads/file' # 存储路径 + path: uploads/file + store-path: uploads/file # autocode configuration autocode: @@ -122,67 +122,67 @@ autocode: # qiniu configuration (请自行七牛申请对应的 公钥 私钥 bucket 和 域名地址) qiniu: - zone: 'ZoneHuaDong' - bucket: '' - img-path: '' + zone: ZoneHuaDong + bucket: "" + img-path: "" use-https: false - access-key: '' - secret-key: '' + access-key: "" + secret-key: "" use-cdn-domains: false # aliyun oss configuration aliyun-oss: - endpoint: 'yourEndpoint' - access-key-id: 'yourAccessKeyId' - access-key-secret: 'yourAccessKeySecret' - bucket-name: 'yourBucketName' - bucket-url: 'yourBucketUrl' - base-path: 'yourBasePath' + endpoint: yourEndpoint + access-key-id: yourAccessKeyId + access-key-secret: yourAccessKeySecret + bucket-name: yourBucketName + bucket-url: yourBucketUrl + base-path: yourBasePath # tencent cos configuration tencent-cos: - bucket: 'xxxxx-10005608' - region: 'ap-shanghai' - secret-id: 'xxxxxxxx' - secret-key: 'xxxxxxxx' - base-url: 'https://gin.vue.admin' - path-prefix: 'github.com/flipped-aurora/gin-vue-admin/server' + bucket: xxxxx-10005608 + region: ap-shanghai + secret-id: your-secret-id + secret-key: your-secret-key + base-url: https://gin.vue.admin + path-prefix: github.com/flipped-aurora/gin-vue-admin/server # aws s3 configuration (minio compatible) aws-s3: bucket: xxxxx-10005608 region: ap-shanghai - endpoint: '' + endpoint: "" s3-force-path-style: false disable-ssl: false - secret-id: xxxxxxxx - secret-key: xxxxxxxx + secret-id: your-secret-id + secret-key: your-secret-key base-url: https://gin.vue.admin path-prefix: github.com/flipped-aurora/gin-vue-admin/server # huawei obs configuration hua-wei-obs: - path: 'you-path' - bucket: 'you-bucket' - endpoint: 'you-endpoint' - access-key: 'you-access-key' - secret-key: 'you-secret-key' + path: you-path + bucket: you-bucket + endpoint: you-endpoint + access-key: you-access-key + secret-key: you-secret-key # excel configuration excel: - dir: './resource/excel/' + dir: ./resource/excel/ # timer task db clear table Timer: start: true spec: "@daily" # 定时任务详细配置参考 https://pkg.go.dev/github.com/robfig/cron/v3 detail: - - tableName: "sys_operation_records" - compareField: "created_at" - interval: "2160h" - - tableName: "jwt_blacklists" - compareField: "created_at" - interval: "168h" + - tableName: sys_operation_records + compareField: created_at + interval: 2160h + - tableName: jwt_blacklists + compareField: created_at + interval: 168h # 跨域配置 # 需要配合 server/initialize/router.go#L32 使用 diff --git a/server/config/jwt.go b/server/config/jwt.go index 51979b6a2..c95d30dc1 100644 --- a/server/config/jwt.go +++ b/server/config/jwt.go @@ -2,7 +2,7 @@ package config type JWT struct { SigningKey string `mapstructure:"signing-key" json:"signing-key" yaml:"signing-key"` // jwt签名 - ExpiresTime int64 `mapstructure:"expires-time" json:"expires-time" yaml:"expires-time"` // 过期时间 - BufferTime int64 `mapstructure:"buffer-time" json:"buffer-time" yaml:"buffer-time"` // 缓冲时间 + ExpiresTime string `mapstructure:"expires-time" json:"expires-time" yaml:"expires-time"` // 过期时间 + BufferTime string `mapstructure:"buffer-time" json:"buffer-time" yaml:"buffer-time"` // 缓冲时间 Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` // 签发者 } diff --git a/server/core/viper.go b/server/core/viper.go index 963717d40..3f67f9633 100644 --- a/server/core/viper.go +++ b/server/core/viper.go @@ -7,14 +7,12 @@ import ( "github.com/gin-gonic/gin" "os" "path/filepath" - "time" - "github.com/songzhibin97/gkit/cache/local_cache" + "github.com/fsnotify/fsnotify" + "github.com/spf13/viper" "github.com/flipped-aurora/gin-vue-admin/server/global" _ "github.com/flipped-aurora/gin-vue-admin/server/packfile" - "github.com/fsnotify/fsnotify" - "github.com/spf13/viper" ) // Viper // @@ -72,8 +70,5 @@ func Viper(path ...string) *viper.Viper { // root 适配性 根据root位置去找到对应迁移位置,保证root路径有效 global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..") - global.BlackCache = local_cache.NewCache( - local_cache.SetDefaultExpire(time.Second * time.Duration(global.GVA_CONFIG.JWT.ExpiresTime)), - ) return v } diff --git a/server/initialize/outer.go b/server/initialize/outer.go new file mode 100644 index 000000000..5d23aeb7f --- /dev/null +++ b/server/initialize/outer.go @@ -0,0 +1,23 @@ +package initialize + +import ( + "github.com/songzhibin97/gkit/cache/local_cache" + + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/utils" +) + +func OtherInit() { + dr, err := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime) + if err != nil { + panic(err) + } + _, err = utils.ParseDuration(global.GVA_CONFIG.JWT.BufferTime) + if err != nil { + panic(err) + } + + global.BlackCache = local_cache.NewCache( + local_cache.SetDefaultExpire(dr), + ) +} diff --git a/server/main.go b/server/main.go index 147dffeb5..fdf1a40d3 100644 --- a/server/main.go +++ b/server/main.go @@ -1,6 +1,8 @@ package main import ( + "go.uber.org/zap" + "github.com/flipped-aurora/gin-vue-admin/server/core" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/initialize" @@ -22,7 +24,8 @@ import ( // @BasePath / func main() { global.GVA_VP = core.Viper() // 初始化Viper - global.GVA_LOG = core.Zap() // 初始化zap日志库 + initialize.OtherInit() + global.GVA_LOG = core.Zap() // 初始化zap日志库 zap.ReplaceGlobals(global.GVA_LOG) global.GVA_DB = initialize.Gorm() // gorm连接数据库 initialize.Timer() diff --git a/server/middleware/email.go b/server/middleware/email.go index b3e0fc366..4a07561c9 100644 --- a/server/middleware/email.go +++ b/server/middleware/email.go @@ -2,7 +2,7 @@ package middleware import ( "bytes" - "io/ioutil" + "io" "strconv" "time" @@ -32,9 +32,9 @@ func ErrorToEmail() gin.HandlerFunc { } username = user.Username } - body, _ := ioutil.ReadAll(c.Request.Body) + body, _ := io.ReadAll(c.Request.Body) // 再重新写回请求体body中,ioutil.ReadAll会清空c.Request.Body中的数据 - c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) record := system.SysOperationRecord{ Ip: c.ClientIP(), Method: c.Request.Method, diff --git a/server/middleware/jwt.go b/server/middleware/jwt.go index a7075d021..25806e63f 100644 --- a/server/middleware/jwt.go +++ b/server/middleware/jwt.go @@ -54,7 +54,8 @@ func JWTAuth() gin.HandlerFunc { // c.Abort() //} if claims.ExpiresAt-time.Now().Unix() < claims.BufferTime { - claims.ExpiresAt = time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime + dr, _ := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime) + claims.ExpiresAt = time.Now().Add(dr).Unix() newToken, _ := j.CreateTokenByOldToken(token, *claims) newClaims, _ := j.ParseToken(newToken) c.Header("new-token", newToken) diff --git a/server/middleware/logger.go b/server/middleware/logger.go index 5bfda9a54..400aa2c8f 100644 --- a/server/middleware/logger.go +++ b/server/middleware/logger.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "strings" "time" @@ -47,7 +47,7 @@ func (l Logger) SetLoggerMiddleware() gin.HandlerFunc { if l.Filter != nil && !l.Filter(c) { body, _ = c.GetRawData() // 将原body塞回去 - c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) } c.Next() cost := time.Since(start) diff --git a/server/middleware/operation.go b/server/middleware/operation.go index ba79a43d8..6303f0469 100644 --- a/server/middleware/operation.go +++ b/server/middleware/operation.go @@ -3,7 +3,7 @@ package middleware import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" "net/url" "strconv" @@ -36,11 +36,11 @@ func OperationRecord() gin.HandlerFunc { var userId int if c.Request.Method != http.MethodGet { var err error - body, err = ioutil.ReadAll(c.Request.Body) + body, err = io.ReadAll(c.Request.Body) if err != nil { global.GVA_LOG.Error("read body from request error:", zap.Error(err)) } else { - c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) } } else { query := c.Request.URL.RawQuery @@ -75,7 +75,7 @@ func OperationRecord() gin.HandlerFunc { } // 上传文件时候 中间件日志进行裁断操作 - if strings.Index(c.GetHeader("Content-Type"), "multipart/form-data") > -1 { + if strings.Contains(c.GetHeader("Content-Type"), "multipart/form-data") { if len(record.Body) > 1024 { // 截断 newBody := respPool.Get().([]byte) @@ -100,15 +100,15 @@ func OperationRecord() gin.HandlerFunc { record.Latency = latency record.Resp = writer.body.String() - if strings.Index(c.Writer.Header().Get("Pragma"), "public") > -1 || - strings.Index(c.Writer.Header().Get("Expires"), "0") > -1 || - strings.Index(c.Writer.Header().Get("Cache-Control"), "must-revalidate, post-check=0, pre-check=0") > -1 || - strings.Index(c.Writer.Header().Get("Content-Type"), "application/force-download") > -1 || - strings.Index(c.Writer.Header().Get("Content-Type"), "application/octet-stream") > -1 || - strings.Index(c.Writer.Header().Get("Content-Type"), "application/vnd.ms-excel") > -1 || - strings.Index(c.Writer.Header().Get("Content-Type"), "application/download") > -1 || - strings.Index(c.Writer.Header().Get("Content-Disposition"), "attachment") > -1 || - strings.Index(c.Writer.Header().Get("Content-Transfer-Encoding"), "binary") > -1 { + if strings.Contains(c.Writer.Header().Get("Pragma"), "public") || + strings.Contains(c.Writer.Header().Get("Expires"), "0") || + strings.Contains(c.Writer.Header().Get("Cache-Control"), "must-revalidate, post-check=0, pre-check=0") || + strings.Contains(c.Writer.Header().Get("Content-Type"), "application/force-download") || + strings.Contains(c.Writer.Header().Get("Content-Type"), "application/octet-stream") || + strings.Contains(c.Writer.Header().Get("Content-Type"), "application/vnd.ms-excel") || + strings.Contains(c.Writer.Header().Get("Content-Type"), "application/download") || + strings.Contains(c.Writer.Header().Get("Content-Disposition"), "attachment") || + strings.Contains(c.Writer.Header().Get("Content-Transfer-Encoding"), "binary") { if len(record.Resp) > 1024 { // 截断 newBody := respPool.Get().([]byte) diff --git a/server/model/system/request/jwt.go b/server/model/system/request/jwt.go index ee702ef97..5a7c78aab 100644 --- a/server/model/system/request/jwt.go +++ b/server/model/system/request/jwt.go @@ -1,7 +1,7 @@ package request import ( - "github.com/golang-jwt/jwt/v4" + jwt "github.com/golang-jwt/jwt/v4" uuid "github.com/satori/go.uuid" ) diff --git a/server/packfile/usePackFile.go b/server/packfile/usePackFile.go index bb51bac3f..7820af999 100644 --- a/server/packfile/usePackFile.go +++ b/server/packfile/usePackFile.go @@ -5,7 +5,6 @@ package packfile import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -24,7 +23,7 @@ func writeFile(path string, data []byte) { // 已存在的文件,不应该覆盖重写,可能在前端更改了配置文件等 if _, err := os.Stat(path); os.IsNotExist(err) { - if err2 := ioutil.WriteFile(path, data, os.ModePerm); err2 != nil { + if err2 := os.WriteFile(path, data, os.ModePerm); err2 != nil { fmt.Printf("Write file failed: %s\n", path) } } else { diff --git a/server/resource/autocode_template/server/request.go.tpl b/server/resource/autocode_template/server/request.go.tpl index b6c2e3d10..d2fdfd85c 100644 --- a/server/resource/autocode_template/server/request.go.tpl +++ b/server/resource/autocode_template/server/request.go.tpl @@ -3,9 +3,18 @@ package request import ( "github.com/flipped-aurora/gin-vue-admin/server/model/{{.Package}}" "github.com/flipped-aurora/gin-vue-admin/server/model/common/request" + "time" ) type {{.StructName}}Search struct{ {{.Package}}.{{.StructName}} + StartCreatedAt *time.Time `json:"startCreatedAt" form:"startCreatedAt"` + EndCreatedAt *time.Time `json:"endCreatedAt" form:"endCreatedAt"` + {{- range .Fields}} + {{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} + Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"` + End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"` + {{- end }} + {{- end }} request.PageInfo } diff --git a/server/resource/autocode_template/server/service.go.tpl b/server/resource/autocode_template/server/service.go.tpl index b578d31d6..610871158 100644 --- a/server/resource/autocode_template/server/service.go.tpl +++ b/server/resource/autocode_template/server/service.go.tpl @@ -54,29 +54,24 @@ func ({{.Abbreviation}}Service *{{.StructName}}Service)Get{{.StructName}}InfoLis db := global.GVA_DB.Model(&{{.Package}}.{{.StructName}}{}) var {{.Abbreviation}}s []{{.Package}}.{{.StructName}} // 如果有条件搜索 下方会自动创建搜索语句 + if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil { + db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt) + } {{- range .Fields}} {{- if .FieldSearchType}} {{- if eq .FieldType "string" }} if info.{{.FieldName}} != "" { db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) } - {{- else if eq .FieldType "bool" }} + {{- else if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} + if info.Start{{.FieldName}} != nil && info.End{{.FieldName}} != nil { + db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ? AND ? ",info.Start{{.FieldName}},info.End{{.FieldName}}) + } + {{- else}} if info.{{.FieldName}} != nil { db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) } - {{- else if eq .FieldType "int" }} - if info.{{.FieldName}} != nil { - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) - } - {{- else if eq .FieldType "float64" }} - if info.{{.FieldName}} != nil { - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) - } - {{- else if eq .FieldType "time.Time" }} - if info.{{.FieldName}} != nil { - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) - } - {{- end }} + {{- end }} {{- end }} {{- end }} err = db.Count(&total).Error diff --git a/server/resource/autocode_template/web/form.vue.tpl b/server/resource/autocode_template/web/form.vue.tpl index dd4a5b0d6..763f48ea0 100644 --- a/server/resource/autocode_template/web/form.vue.tpl +++ b/server/resource/autocode_template/web/form.vue.tpl @@ -33,8 +33,8 @@ {{- end }} - {{ "{{ t('general.save') }}" }} - {{ "{{ t('general.back') }}" }} + {{ "{{ t('general.save') }}" }} + {{ "{{ t('general.back') }}" }} diff --git a/server/resource/autocode_template/web/table.vue.tpl b/server/resource/autocode_template/web/table.vue.tpl index 3ae510819..343027aaf 100644 --- a/server/resource/autocode_template/web/table.vue.tpl +++ b/server/resource/autocode_template/web/table.vue.tpl @@ -2,6 +2,11 @@