|
@@ -5,9 +5,13 @@ import (
|
|
|
"encoding/base64"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "hongze/hongze_mfyx/models"
|
|
|
+ "hongze/hongze_mfyx/utils"
|
|
|
+ "io"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
|
|
|
rhttp "github.com/rdlucklib/rdluck_tools/http"
|
|
|
)
|
|
@@ -18,6 +22,11 @@ const (
|
|
|
AlAppCode = "22553c4ba74545568aba70ac6cfd441d"
|
|
|
)
|
|
|
|
|
|
+var (
|
|
|
+ //文档 https://market.aliyun.com/apimarket/detail/cmapi013591?spm=5176.product-detail.detail.4.6d4f3dcaS0qPFI&endpoint=a3dd145f3865492ba6f02d0c09dd00a1
|
|
|
+ AliyunOcrBusinessCardCode = "22553c4ba74545568aba70ac6cfd441d" // 阿里云OCR名片识别的code
|
|
|
+)
|
|
|
+
|
|
|
type OcrItem struct {
|
|
|
Image string `json:"image"`
|
|
|
}
|
|
@@ -29,7 +38,7 @@ type OcrResult struct {
|
|
|
TelCell []string `json:"tel_cell"`
|
|
|
}
|
|
|
|
|
|
-///ocr_business_card.json
|
|
|
+// /ocr_business_card.json
|
|
|
func AliyunBusinessCard(imgUrl string) (result *OcrResult, err error) {
|
|
|
postUrl := `https://dm-57.data.aliyun.com/rest/160601/ocr/ocr_business_card.json`
|
|
|
|
|
@@ -74,3 +83,79 @@ func AliyunBusinessCard(imgUrl string) (result *OcrResult, err error) {
|
|
|
err = json.Unmarshal(body, &result)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// 阿里云名片识别返回类
|
|
|
+type OcrBusinessCardResult struct {
|
|
|
+ TelCell []string `json:"tel_cell"`
|
|
|
+ Success bool `json:"success"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ Company []string `json:"company"`
|
|
|
+ Addr []string `json:"addr"`
|
|
|
+ Department []string `json:"department"`
|
|
|
+ Title []string `json:"title"`
|
|
|
+ RequestID string `json:"request_id"`
|
|
|
+ Email []string `json:"email"`
|
|
|
+ TelWork []string `json:"tel_work"`
|
|
|
+}
|
|
|
+
|
|
|
+//func init() {
|
|
|
+// AliyunOcrBusinessCard("https://hzstatic.hzinsights.com/static/images/202403/20240327/kjafdja3BWwThD1ZJexE0S4Skqn2.jpg")
|
|
|
+//}
|
|
|
+
|
|
|
+// 阿里云名片识别更换2024-01-02
|
|
|
+func AliyunOcrBusinessCard(imageurl string) (result *OcrBusinessCardResult, err error) {
|
|
|
+ postUrl := "https://bizcard.market.alicloudapi.com/rest/160601/ocr/ocr_business_card.json"
|
|
|
+ //method := "POST"
|
|
|
+
|
|
|
+ item := new(OcrItem)
|
|
|
+ item.Image = imageurl
|
|
|
+ jsonBody, err := json.Marshal(item)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("json.Marshal Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tr := &http.Transport{
|
|
|
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
+ }
|
|
|
+ client := &http.Client{Transport: tr}
|
|
|
+ req, err := http.NewRequest("POST", postUrl, strings.NewReader(string(jsonBody)))
|
|
|
+ req.Header.Add("Authorization", "APPCODE "+AliyunOcrBusinessCardCode)
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+
|
|
|
+ res, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer res.Body.Close()
|
|
|
+
|
|
|
+ body, err := io.ReadAll(res.Body)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = json.Unmarshal(body, &result)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //处理返回的手机号格式,并进行数据合并
|
|
|
+ var telCell []string
|
|
|
+ if len(result.TelCell) > 0 {
|
|
|
+ for _, v := range result.TelCell {
|
|
|
+ telCell = append(telCell, utils.GetArabicNumbers(v))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(result.TelWork) > 0 {
|
|
|
+ for _, v := range result.TelWork {
|
|
|
+ telCell = append(telCell, utils.GetArabicNumbers(v))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ logItem := new(models.AliyunOcrLog)
|
|
|
+ logItem.ImgUrl = imageurl
|
|
|
+ logItem.Result = string(body)
|
|
|
+ logItem.CreateTime = time.Now()
|
|
|
+ err = models.AddAliyunOcrLog(logItem)
|
|
|
+ return
|
|
|
+}
|