diff --git a/apps/device/api/product.go b/apps/device/api/product.go index f030bce..fbbb20d 100644 --- a/apps/device/api/product.go +++ b/apps/device/api/product.go @@ -116,7 +116,8 @@ 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 @@ -124,14 +125,16 @@ 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 @@ -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}) diff --git a/kit/utils/charset.go b/kit/utils/charset.go new file mode 100644 index 0000000..c878a4e --- /dev/null +++ b/kit/utils/charset.go @@ -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) +} diff --git a/kit/utils/ip.go b/kit/utils/ip.go index 6216122..15c3825 100644 --- a/kit/utils/ip.go +++ b/kit/utils/ip.go @@ -6,8 +6,6 @@ import ( "pandax/kit/httpclient" ) -const ipurl = "https://ip.cn/api/index" - const UNKNOWN = "XX XX" // GetRealAddressByIP 获取真实地址 @@ -15,17 +13,15 @@ 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地址 diff --git a/kit/utils/str_utils_test.go b/kit/utils/str_utils_test.go index 9f42174..be9751e 100644 --- a/kit/utils/str_utils_test.go +++ b/kit/utils/str_utils_test.go @@ -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) }