You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.0 KiB

package aliyun
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"recook/configs"
"strings"
)
const (
actionOcrUrl = "https://dm-58.data.aliyun.com/rest/160601/ocr/ocr_business_license.json"
NoTFindErr = "FailInRecognition"
appCode = configs.Config_Aliyun_Ocr_AppCode
)
type ocr struct {
}
var (
Ocr = &ocr{}
client = &http.Client{}
)
type BusinessLicense struct {
ConfigStr string `json:"config_str"`
Angle float64 `json:"angle"`
RegNum string `json:"reg_num"`
Name string `json:"name"`
Type string `json:"type"`
Person string `json:"person"`
EstablishDate string `json:"establish_date"`
ValidPeriod string `json:"valid_period"`
Address string `json:"address"`
Capital string `json:"capital"`
Business string `json:"business"`
IsFake bool `json:"is_fake"`
Success bool `json:"success"`
RequestId string `json:"request_id"`
}
// @Style 营业执照识别
func (o *ocr) BusinessLicense(imgUrl string) (result BusinessLicense, err error) {
data := map[string]string{
"image": imgUrl,
}
jsonData, _ := json.Marshal(data)
resByte, err := o.exec(actionOcrUrl, string(jsonData))
log.Println(string(resByte))
if err != nil {
return
}
err = json.Unmarshal(resByte, &result)
if err != nil {
return
}
return
}
// @Style 执行请求
func (o *ocr) exec(action, data string) ([]byte, error) {
return request("POST", action, data, map[string]string{
"Authorization": "APPCODE " + appCode,
})
}
// @Style 请求
func request(method, url, data string, headers ...map[string]string) ([]byte, error) {
reqest, err := http.NewRequest(method, url, strings.NewReader(data))
if err != nil {
return nil, err
}
if len(headers) > 0 {
for key, value := range headers[0] {
reqest.Header.Add(key, value)
}
}
response, err := client.Do(reqest)
if err != nil {
return nil, err
}
defer response.Body.Close()
result, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return result, nil
}