From f693ff60b2a40af2672e956b9de4c582c469a479 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Mon, 27 Jul 2020 22:45:47 +0800 Subject: [PATCH] doc : add notes --- models/datascope.go | 8 ++++++++ models/initdb.go | 4 ++-- tools/ip.go | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/models/datascope.go b/models/datascope.go index d1c92741..308d1a25 100644 --- a/models/datascope.go +++ b/models/datascope.go @@ -2,8 +2,10 @@ package models import ( "errors" + "fmt" "github.com/jinzhu/gorm" "go-admin/tools" + "go-admin/tools/config" ) type DataPermission struct { @@ -14,6 +16,12 @@ type DataPermission struct { } func (e *DataPermission) GetDataScope(tbname string, table *gorm.DB) (*gorm.DB, error) { + + if !config.ApplicationConfig.EnableDP { + usageStr := `数据权限已经为您` + tools.Green(`关闭`) + `,如需开启请参考配置文件字段说明` + fmt.Printf("%s\n", usageStr) + return table, nil + } SysUser := new(SysUser) SysRole := new(SysRole) SysUser.UserId = e.UserId diff --git a/models/initdb.go b/models/initdb.go index 417d6463..763ac1ed 100644 --- a/models/initdb.go +++ b/models/initdb.go @@ -40,8 +40,8 @@ func ExecSql(filePath string) error { return nil } -func Ioutil(name string) (string, error) { - if contents, err := ioutil.ReadFile(name); err == nil { +func Ioutil(filePath string) (string, error) { + if contents, err := ioutil.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) diff --git a/tools/ip.go b/tools/ip.go index b28770f6..c120befa 100644 --- a/tools/ip.go +++ b/tools/ip.go @@ -4,9 +4,11 @@ import ( "encoding/json" "fmt" "io/ioutil" + "net" "net/http" ) +// 获取外网ip地址 func GetLocation(ip string) string { if ip == "127.0.0.1" || ip == "localhost" { return "内部IP" @@ -31,3 +33,27 @@ func GetLocation(ip string) string { } return m["province"] + "-" + m["city"] } + +// 获取局域网ip地址 +func GetLocaHonst() string { + netInterfaces, err := net.Interfaces() + if err != nil { + fmt.Println("net.Interfaces failed, err:", err.Error()) + } + + for i := 0; i < len(netInterfaces); i++ { + if (netInterfaces[i].Flags & net.FlagUp) != 0 { + addrs, _ := netInterfaces[i].Addrs() + + for _, address := range addrs { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + return ipnet.IP.String() + } + } + } + } + + } + return "" +}