package services import ( "crypto/tls" "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" ) const ( AlAppKey = "203889624" AlAppSecret = "voampGGl0yGNx5xA1sZdmZlV2EiBct2P" 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"` } type OcrResult struct { Addr []string `json:"addr"` Company []string `json:"company"` Success bool `json:"success"` TelCell []string `json:"tel_cell"` } // /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` imgBody, err := rhttp.Get(imgUrl) if err != nil { return } imageBase64 := base64.StdEncoding.EncodeToString(imgBody) item := new(OcrItem) item.Image = imageBase64 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))) if err != nil { fmt.Println("err:" + err.Error()) return } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "APPCODE "+AlAppCode) resp, err := client.Do(req) if err != nil { fmt.Println("client.Do Err:" + err.Error()) return } fmt.Println("resp") fmt.Println(resp) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("err:" + err.Error()) return } fmt.Println("body:", string(body)) 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 }