From d44d88d77f3517b7682213fa23c427856b4213f4 Mon Sep 17 00:00:00 2001 From: zhanluxianshen Date: Wed, 19 Jun 2024 20:08:07 +0800 Subject: [PATCH] drop ioutil and other cleans. Signed-off-by: zhanluxianshen --- app/admin/models/datascope.go | 3 ++- app/admin/models/initdb.go | 9 +++++---- app/admin/models/sys_api.go | 6 +++--- app/other/apis/file.go | 4 ++-- cmd/migrate/migration/models/initdb.go | 5 ++--- common/middleware/logger.go | 5 ++--- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/admin/models/datascope.go b/app/admin/models/datascope.go index 8ccd047ca..6bc7e7084 100644 --- a/app/admin/models/datascope.go +++ b/app/admin/models/datascope.go @@ -2,6 +2,7 @@ package models import ( "errors" + "github.com/go-admin-team/go-admin-core/sdk/pkg" "gorm.io/gorm" @@ -31,7 +32,7 @@ func (e *DataPermission) GetDataScope(tableName string, db *gorm.DB) (*gorm.DB, } err = db.Find(role, user.RoleId).Error if err != nil { - return nil, errors.New("获取用户数据出错 msg:" + err.Error()) + return nil, errors.New("获取角色数据出错 msg:" + err.Error()) } if role.DataScope == "2" { db = db.Where(tableName+".create_by in (select sys_user.user_id from sys_role_dept left join sys_user on sys_user.dept_id=sys_role_dept.dept_id where sys_role_dept.role_id = ?)", user.RoleId) diff --git a/app/admin/models/initdb.go b/app/admin/models/initdb.go index cdec260f3..8c7340dbf 100644 --- a/app/admin/models/initdb.go +++ b/app/admin/models/initdb.go @@ -3,10 +3,11 @@ package models import ( "fmt" "go-admin/common/global" - "gorm.io/gorm" - "io/ioutil" "log" + "os" "strings" + + "gorm.io/gorm" ) func InitDb(db *gorm.DB) (err error) { @@ -44,10 +45,10 @@ func ExecSql(db *gorm.DB, filePath string) error { } func Ioutil(filePath string) (string, error) { - if contents, err := ioutil.ReadFile(filePath); err == nil { + if contents, err := os.ReadFile(filePath); err == nil { //因为contents是[]byte类型,直接转换成string类型后会多一行空格,需要使用strings.Replace替换换行符 result := strings.Replace(string(contents), "\n", "", 1) - fmt.Println("Use ioutil.ReadFile to read a file:", result) + fmt.Println("Use os.ReadFile to read a file:", result) return result, nil } else { return "", err diff --git a/app/admin/models/sys_api.go b/app/admin/models/sys_api.go index 0b78e4942..3a851a831 100644 --- a/app/admin/models/sys_api.go +++ b/app/admin/models/sys_api.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "os" "regexp" "strings" @@ -65,7 +65,7 @@ func SaveSysApi(message storage.Messager) (err error) { // 根据接口方法注释里的@Summary填充接口名称,适用于代码生成器 // 可在此处增加配置路径前缀的if判断,只对代码生成的自建应用进行定向的接口名称填充 - jsonFile, _ := ioutil.ReadFile("docs/swagger.json") + jsonFile, _ := os.ReadFile("docs/swagger.json") jsonData, _ := simplejson.NewFromReader(bytes.NewReader(jsonFile)) urlPath := v.RelativePath idPatten := "(.*)/:(\\w+)" // 正则替换,把:id换成{id} @@ -81,7 +81,7 @@ func SaveSysApi(message storage.Messager) (err error) { //Update("handle", v.Handler). Error if err != nil { - err := fmt.Errorf("Models SaveSysApi error: %s \r\n ", err.Error()) + err := fmt.Errorf("models SaveSysApi error: %s \r\n ", err.Error()) return err } } diff --git a/app/other/apis/file.go b/app/other/apis/file.go index e0d77f422..7b73c299b 100644 --- a/app/other/apis/file.go +++ b/app/other/apis/file.go @@ -4,7 +4,7 @@ import ( "encoding/base64" "errors" "fmt" - "io/ioutil" + "os" "strings" "github.com/gin-gonic/gin" @@ -86,7 +86,7 @@ func (e File) baseImg(c *gin.Context, fileResponse FileResponse, urlPerfix strin e.Error(500, errors.New(""), "初始化文件路径失败") } base64File := path + fileName - _ = ioutil.WriteFile(base64File, ddd, 0666) + _ = os.WriteFile(base64File, ddd, 0666) typeStr := strings.Replace(strings.Replace(file2list[0], "data:", "", -1), ";base64", "", -1) fileResponse = FileResponse{ Size: pkg.GetFileSize(base64File), diff --git a/cmd/migrate/migration/models/initdb.go b/cmd/migrate/migration/models/initdb.go index ba7f4ca76..2dba6e5f7 100644 --- a/cmd/migrate/migration/models/initdb.go +++ b/cmd/migrate/migration/models/initdb.go @@ -3,7 +3,6 @@ package models import ( "fmt" "go-admin/common/global" - "io/ioutil" "log" "strings" @@ -61,10 +60,10 @@ func ExecSql(db *gorm.DB, filePath string) error { } func Ioutil(filePath string) (string, error) { - if contents, err := ioutil.ReadFile(filePath); err == nil { + if contents, err := os.ReadFil(filePath); err == nil { //因为contents是[]byte类型,直接转换成string类型后会多一行空格,需要使用strings.Replace替换换行符 result := strings.Replace(string(contents), "\n", "", 1) - fmt.Println("Use ioutil.ReadFile to read a file:", result) + fmt.Println("Use os.ReadFil to read a file:", result) return result, nil } else { return "", err diff --git a/common/middleware/logger.go b/common/middleware/logger.go index 89c2c070c..f3d4d65f2 100644 --- a/common/middleware/logger.go +++ b/common/middleware/logger.go @@ -7,7 +7,6 @@ import ( "go-admin/app/admin/service/dto" "go-admin/common" "io" - "io/ioutil" "net/http" "strings" "time" @@ -38,8 +37,8 @@ func LoggerToFile() gin.HandlerFunc { log.Warnf("copy body error, %s", err.Error()) err = nil } - rb, _ := ioutil.ReadAll(bf) - c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rb)) + rb, _ := io.ReadAll(bf) + c.Request.Body = io.NopCloser(bytes.NewBuffer(rb)) body = string(rb) }