소스 검색

fix:乱码处理

jimmyyem 1 개월 전
부모
커밋
b7accfa7d3
4개의 변경된 파일98개의 추가작업 그리고 106개의 파일을 삭제
  1. 0 4
      go.sum
  2. 2 79
      internal/logic/agent/upload_agent_data_logic.go
  3. 11 23
      internal/logic/contact/import_whatsapp_contact_logic.go
  4. 85 0
      internal/utils/charset.go

+ 0 - 4
go.sum

@@ -447,7 +447,6 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
 github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
-github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
 github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
 github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
 github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
@@ -481,7 +480,6 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
 github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
 github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
 github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
-github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
 github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
 github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
 github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
@@ -580,8 +578,6 @@ github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
 github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
 github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
 github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
-github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
-github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
 github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
 github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
 github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

+ 2 - 79
internal/logic/agent/upload_agent_data_logic.go

@@ -1,32 +1,20 @@
 package agent
 
 import (
-	"bytes"
 	"context"
 	"encoding/csv"
 	"fmt"
-	"github.com/saintfish/chardet"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
-	"golang.org/x/text/encoding"
-	"golang.org/x/text/encoding/charmap"
-	"golang.org/x/text/encoding/japanese"
-	"golang.org/x/text/encoding/korean"
-	"golang.org/x/text/encoding/traditionalchinese"
-	"golang.org/x/text/encoding/unicode"
-	"io"
 	"mime/multipart"
-	"strings"
 	agentModel "wechat-api/ent/agent"
 	"wechat-api/hook/fastgpt"
+	"wechat-api/internal/utils"
 	"wechat-api/internal/utils/dberrorhandler"
 
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
 
 	"github.com/zeromicro/go-zero/core/logx"
-
-	"golang.org/x/text/encoding/simplifiedchinese"
-	"golang.org/x/text/transform"
 )
 
 type UploadAgentDataLogic struct {
@@ -116,72 +104,7 @@ func (l *UploadAgentDataLogic) UploadAgentData(req *types.UploadDataReq, file mu
 	return resp, nil
 }
 
-func trim(s string) string {
-	s = strings.TrimLeft(s, " \r\n\t")
-	s = strings.TrimRight(s, " \r\n\t")
-	return s
-}
-
 // transCharset 自动检测编码并转换为 UTF-8
 func transCharset(s string) string {
-	// 1. 自动检测编码
-	detector := chardet.NewTextDetector()
-	result, err := detector.DetectBest([]byte(s))
-	if err != nil {
-		fmt.Println("Encoding detection failed:", err)
-		return s
-	}
-
-	// 2. 找到相应的编码
-	fmt.Println("result.Charset:", result.Charset)
-	enc := getEncoding(result.Charset)
-	fmt.Println("enc:", enc)
-	if enc == nil {
-		// 直接返回原始字符串
-		fmt.Println("Unsupported charset:", result.Charset)
-		return s
-	}
-
-	// 3. 转换为 UTF-8
-	rd := transform.NewReader(bytes.NewReader([]byte(s)), enc.NewDecoder())
-	utf8Bytes, err := io.ReadAll(rd)
-	if err != nil {
-		fmt.Println("Encoding conversion failed:", err)
-		return s
-	}
-
-	// 4. 返回转换后的 UTF-8 字符串
-	return string(utf8Bytes)
-}
-
-// 根据字符集名称获取 `encoding.Encoding`
-func getEncoding(charset string) encoding.Encoding {
-	switch charset {
-	case "UTF-8", "ASCII":
-		return encoding.Nop // 无需转换
-	case "ISO-8859-1":
-		return charmap.ISO8859_1
-	case "ISO-8859-2":
-		return charmap.ISO8859_2
-	case "ISO-8859-15":
-		return charmap.ISO8859_15
-	case "Windows-1252":
-		return charmap.Windows1252
-	case "Big5":
-		return traditionalchinese.Big5
-	case "GB-2312", "GBK", "GB-18030":
-		return simplifiedchinese.GBK
-	case "Shift_JIS":
-		return japanese.ShiftJIS
-	case "EUC-JP":
-		return japanese.EUCJP
-	case "EUC-KR":
-		return korean.EUCKR
-	case "UTF-16LE":
-		return unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
-	case "UTF-16BE":
-		return unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
-	default:
-		return nil
-	}
+	return utils.TransCharset(s)
 }

+ 11 - 23
internal/logic/contact/import_whatsapp_contact_logic.go

@@ -1,15 +1,11 @@
 package contact
 
 import (
-	"bytes"
 	"context"
 	"encoding/csv"
 	"fmt"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
 	"github.com/suyuan32/simple-admin-common/utils/uuidx"
-	"golang.org/x/text/encoding/simplifiedchinese"
-	"golang.org/x/text/transform"
-	"io"
 	"mime/multipart"
 	"strconv"
 	"strings"
@@ -19,6 +15,7 @@ import (
 	"wechat-api/ent/label"
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
+	"wechat-api/internal/utils"
 
 	"github.com/zeromicro/go-zero/core/logx"
 )
@@ -59,8 +56,8 @@ func (l *ImportWhatsappContactLogic) ImportWhatsappContact(req *types.ImportWhat
 		_, err := l.svcCtx.DB.Contact.Query().Where(
 			contact.OrganizationID(organizationId),
 			contact.Ctype(2),
-			contact.Cc(trim(record[0])),
-			contact.Phone(trim(record[1])),
+			contact.Cc(utils.Trim(record[0])),
+			contact.Phone(utils.Trim(record[1])),
 		).First(l.ctx)
 		if err == nil || !ent.IsNotFound(err) {
 			fail++
@@ -68,16 +65,16 @@ func (l *ImportWhatsappContactLogic) ImportWhatsappContact(req *types.ImportWhat
 		}
 
 		uuidstr := uuidx.NewUUID().String()
-		sex, _ := strconv.Atoi(trim(record[5]))
-		cage, _ := strconv.Atoi(trim(record[7]))
+		sex, _ := strconv.Atoi(utils.Trim(record[5]))
+		cage, _ := strconv.Atoi(utils.Trim(record[7]))
 		var cbirthday string
-		t, err := time.Parse("2006/01/02", trim(record[8]))
+		t, err := time.Parse("2006/01/02", utils.Trim(record[8]))
 		if err == nil {
 			cbirthday = t.Format("2006-01-02")
 		}
 		newContact, err := l.svcCtx.DB.Contact.Create().SetCtype(2).
-			SetCc(trim(record[0])).
-			SetPhone(trim(record[1])).
+			SetCc(utils.Trim(record[0])).
+			SetPhone(utils.Trim(record[1])).
 			SetType(5).
 			SetWxWxid(uuidstr).
 			SetWxid(uuidstr).
@@ -89,7 +86,7 @@ func (l *ImportWhatsappContactLogic) ImportWhatsappContact(req *types.ImportWhat
 			SetCarea(transCharset(record[9])).
 			SetCbirthday(cbirthday).
 			SetCbirtharea(transCharset(record[10])).
-			SetCidcardNo(trim(record[11])).
+			SetCidcardNo(utils.Trim(record[11])).
 			SetOrganizationID(organizationId).
 			Save(l.ctx)
 		if err != nil {
@@ -100,7 +97,7 @@ func (l *ImportWhatsappContactLogic) ImportWhatsappContact(req *types.ImportWhat
 			success++
 		}
 
-		labels := trim(record[4])
+		labels := utils.Trim(record[4])
 		if labels != "" {
 			for _, value := range strings.Split(labels, "+") {
 				_, err = l.svcCtx.DB.Label.Query().Where(
@@ -140,15 +137,6 @@ func (l *ImportWhatsappContactLogic) ImportWhatsappContact(req *types.ImportWhat
 	return &resp, nil
 }
 
-func trim(s string) string {
-	return strings.Trim(s, " \r\n\t")
-}
-
 func transCharset(s string) string {
-	s = trim(s)
-	return s
-	rd := transform.NewReader(bytes.NewReader([]byte(s)), simplifiedchinese.GBK.NewDecoder())
-	bytes, err := io.ReadAll(rd)
-	fmt.Printf("bytes=%s err=%v\n", bytes, err)
-	return string(bytes)
+	return utils.TransCharset(s)
 }

+ 85 - 0
internal/utils/charset.go

@@ -0,0 +1,85 @@
+package utils
+
+import (
+	"bytes"
+	"fmt"
+	"github.com/saintfish/chardet"
+	"golang.org/x/text/encoding"
+	"golang.org/x/text/encoding/charmap"
+	"golang.org/x/text/encoding/japanese"
+	"golang.org/x/text/encoding/korean"
+	"golang.org/x/text/encoding/simplifiedchinese"
+	"golang.org/x/text/encoding/traditionalchinese"
+	"golang.org/x/text/encoding/unicode"
+	"golang.org/x/text/transform"
+	"io"
+	"strings"
+)
+
+// TransCharset 自动检测编码并转换为 UTF-8
+func TransCharset(s string) string {
+	// 1. 自动检测编码
+	detector := chardet.NewTextDetector()
+	result, err := detector.DetectBest([]byte(s))
+	if err != nil {
+		fmt.Println("Encoding detection failed:", err)
+		return s
+	}
+
+	// 2. 找到相应的编码
+	fmt.Println("result.Charset:", result.Charset)
+	enc := getEncoding(result.Charset)
+	fmt.Println("enc:", enc)
+	if enc == nil {
+		// 直接返回原始字符串
+		fmt.Println("Unsupported charset:", result.Charset)
+		return s
+	}
+
+	// 3. 转换为 UTF-8
+	rd := transform.NewReader(bytes.NewReader([]byte(s)), enc.NewDecoder())
+	utf8Bytes, err := io.ReadAll(rd)
+	if err != nil {
+		fmt.Println("Encoding conversion failed:", err)
+		return s
+	}
+
+	// 4. 返回转换后的 UTF-8 字符串
+	return string(utf8Bytes)
+}
+
+// 根据字符集名称获取 `encoding.Encoding`
+func getEncoding(charset string) encoding.Encoding {
+	switch charset {
+	case "UTF-8", "ASCII":
+		return encoding.Nop // 无需转换
+	case "ISO-8859-1":
+		return charmap.ISO8859_1
+	case "ISO-8859-2":
+		return charmap.ISO8859_2
+	case "ISO-8859-15":
+		return charmap.ISO8859_15
+	case "Windows-1252":
+		return charmap.Windows1252
+	case "Big5":
+		return traditionalchinese.Big5
+	case "GB-2312", "GBK", "GB-18030":
+		return simplifiedchinese.GBK
+	case "Shift_JIS":
+		return japanese.ShiftJIS
+	case "EUC-JP":
+		return japanese.EUCJP
+	case "EUC-KR":
+		return korean.EUCKR
+	case "UTF-16LE":
+		return unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
+	case "UTF-16BE":
+		return unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
+	default:
+		return nil
+	}
+}
+
+func Trim(s string) string {
+	return strings.Trim(s, " \r\n\t")
+}