Skip to content

Commit

Permalink
[fix]根据IP获取地址问题
Browse files Browse the repository at this point in the history
  • Loading branch information
PandaX-Go committed Mar 13, 2024
1 parent 800ef60 commit e4dbd84
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 13 deletions.
12 changes: 8 additions & 4 deletions apps/device/api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,25 @@ func (p *ProductApi) InsertProduct(rc *restfulx.ReqCtx) {
data.RuleChainId = root.Id
}

p.ProductApp.Insert(data)
_, err := p.ProductApp.Insert(data)
biz.ErrIsNilAppendErr(err, "")
}

// UpdateProduct 修改Product
func (p *ProductApi) UpdateProduct(rc *restfulx.ReqCtx) {
var data entity.Product
restfulx.BindQuery(rc, &data)

p.ProductApp.Update(data)
_, err := p.ProductApp.Update(data)
biz.ErrIsNilAppendErr(err, "")
}

func (p *ProductApi) UpdateProductStatue(rc *restfulx.ReqCtx) {
var data entity.Product
restfulx.BindJsonAndValid(rc, &data)

p.ProductApp.Update(data)
_, err := p.ProductApp.Update(data)
biz.ErrIsNilAppendErr(err, "")
}

// DeleteProduct 删除Product
Expand All @@ -143,7 +146,8 @@ func (p *ProductApi) DeleteProduct(rc *restfulx.ReqCtx) {
biz.IsTrue(!(list != nil && len(*list) > 0), fmt.Sprintf("产品ID%s下存在设备,不能被删除", id))
}
// 删除产品
p.ProductApp.Delete(ids)
err := p.ProductApp.Delete(ids)
biz.ErrIsNil(err, "删除失败")
for _, id := range ids {
// 删除所有模型,固件
p.TemplateApp.Delete([]string{id})
Expand Down
79 changes: 79 additions & 0 deletions kit/utils/charset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package utils

import (
"bytes"
"errors"
"fmt"
"io"

"golang.org/x/text/encoding"
"golang.org/x/text/encoding/ianaindex"
"golang.org/x/text/transform"
)

var (
// Alias for charsets.
charsetAlias = map[string]string{
"HZGB2312": "HZ-GB-2312",
"hzgb2312": "HZ-GB-2312",
"GB2312": "HZ-GB-2312",
"gb2312": "HZ-GB-2312",
}
)

func Convert(dstCharset string, srcCharset string, src string) (dst string, err error) {
if dstCharset == srcCharset {
return src, nil
}
dst = src
// Converting `src` to UTF-8.
e, err := getEncoding(srcCharset)
if err != nil {
return "", err
}
if srcCharset != "UTF-8" {
if e != nil {
tmp, err := io.ReadAll(
transform.NewReader(bytes.NewReader([]byte(src)), e.NewDecoder()),
)
if err != nil {
return "", errors.New(fmt.Sprintf(`convert string "%s" to utf8 failed`, srcCharset))
}
src = string(tmp)
} else {
return dst, errors.New(fmt.Sprintf(`unsupported srcCharset "%s"`, srcCharset))
}
}
// Do the converting from UTF-8 to `dstCharset`.
if dstCharset != "UTF-8" {
if e != nil {
tmp, err := io.ReadAll(
transform.NewReader(bytes.NewReader([]byte(src)), e.NewEncoder()),
)
if err != nil {
return "", errors.New(fmt.Sprintf(`convert string from utf8 to "%s" failed`, srcCharset))
}
dst = string(tmp)
} else {
return dst, errors.New(fmt.Sprintf(`unsupported dstCharset "%s"`, srcCharset))
}
} else {
dst = src
}
return dst, nil
}

func ToUTF8(srcCharset string, src string) (dst string, err error) {
return Convert("UTF-8", srcCharset, src)
}

func UTF8To(dstCharset string, src string) (dst string, err error) {
return Convert(dstCharset, "UTF-8", src)
}

func getEncoding(charset string) (encoding.Encoding, error) {
if c, ok := charsetAlias[charset]; ok {
charset = c
}
return ianaindex.MIB.Encoding(charset)
}
12 changes: 4 additions & 8 deletions kit/utils/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@ import (
"pandax/kit/httpclient"
)

const ipurl = "https://ip.cn/api/index"

const UNKNOWN = "XX XX"

// GetRealAddressByIP 获取真实地址
func GetRealAddressByIP(ip string) string {
if ip == "127.0.0.1" || ip == "localhost" {
return "内部IP"
}
url := fmt.Sprintf("%s?ip=%s&type=1", ipurl, ip)
url := fmt.Sprintf("http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=%s", ip)

res := httpclient.NewRequest(url).Get()
if res == nil || res.StatusCode != 200 {
return UNKNOWN
}
toMap, err := res.BodyToMap()
if err != nil {
return UNKNOWN
}
return toMap["address"].(string)
dst, _ := ToUTF8("GBK", string(res.Body))
toMap := Json2Map(dst)
return fmt.Sprintf("%s %s", toMap["pro"].(string), toMap["city"].(string))
}

// 获取局域网ip地址
Expand Down
2 changes: 1 addition & 1 deletion kit/utils/str_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestIdsStrToIdsIntGroup(t *testing.T) {
}

func TestGetRealAddressByIP(t *testing.T) {
ip := GetRealAddressByIP("10.42.0.1")
ip := GetRealAddressByIP("58.57.107.34")
t.Log(ip)
}

Expand Down

0 comments on commit e4dbd84

Please sign in to comment.