aliyun.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package services
  2. import (
  3. "crypto/tls"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "hongze/hongze_mfyx/models"
  8. "hongze/hongze_mfyx/utils"
  9. "io"
  10. "io/ioutil"
  11. "net/http"
  12. "strings"
  13. "time"
  14. rhttp "github.com/rdlucklib/rdluck_tools/http"
  15. )
  16. const (
  17. AlAppKey = "203889624"
  18. AlAppSecret = "voampGGl0yGNx5xA1sZdmZlV2EiBct2P"
  19. AlAppCode = "22553c4ba74545568aba70ac6cfd441d"
  20. )
  21. var (
  22. //文档 https://market.aliyun.com/apimarket/detail/cmapi013591?spm=5176.product-detail.detail.4.6d4f3dcaS0qPFI&endpoint=a3dd145f3865492ba6f02d0c09dd00a1
  23. AliyunOcrBusinessCardCode = "22553c4ba74545568aba70ac6cfd441d" // 阿里云OCR名片识别的code
  24. )
  25. type OcrItem struct {
  26. Image string `json:"image"`
  27. }
  28. type OcrResult struct {
  29. Addr []string `json:"addr"`
  30. Company []string `json:"company"`
  31. Success bool `json:"success"`
  32. TelCell []string `json:"tel_cell"`
  33. }
  34. // /ocr_business_card.json
  35. func AliyunBusinessCard(imgUrl string) (result *OcrResult, err error) {
  36. postUrl := `https://dm-57.data.aliyun.com/rest/160601/ocr/ocr_business_card.json`
  37. imgBody, err := rhttp.Get(imgUrl)
  38. if err != nil {
  39. return
  40. }
  41. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  42. item := new(OcrItem)
  43. item.Image = imageBase64
  44. jsonBody, err := json.Marshal(item)
  45. if err != nil {
  46. fmt.Println("json.Marshal Err:" + err.Error())
  47. return
  48. }
  49. tr := &http.Transport{
  50. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  51. }
  52. client := &http.Client{Transport: tr}
  53. req, err := http.NewRequest("POST", postUrl, strings.NewReader(string(jsonBody)))
  54. if err != nil {
  55. fmt.Println("err:" + err.Error())
  56. return
  57. }
  58. req.Header.Set("Content-Type", "application/json")
  59. req.Header.Set("Authorization", "APPCODE "+AlAppCode)
  60. resp, err := client.Do(req)
  61. if err != nil {
  62. fmt.Println("client.Do Err:" + err.Error())
  63. return
  64. }
  65. fmt.Println("resp")
  66. fmt.Println(resp)
  67. defer resp.Body.Close()
  68. body, err := ioutil.ReadAll(resp.Body)
  69. if err != nil {
  70. fmt.Println("err:" + err.Error())
  71. return
  72. }
  73. fmt.Println("body:", string(body))
  74. err = json.Unmarshal(body, &result)
  75. return
  76. }
  77. // 阿里云名片识别返回类
  78. type OcrBusinessCardResult struct {
  79. TelCell []string `json:"tel_cell"`
  80. Success bool `json:"success"`
  81. Name string `json:"name"`
  82. Company []string `json:"company"`
  83. Addr []string `json:"addr"`
  84. Department []string `json:"department"`
  85. Title []string `json:"title"`
  86. RequestID string `json:"request_id"`
  87. Email []string `json:"email"`
  88. TelWork []string `json:"tel_work"`
  89. }
  90. //func init() {
  91. // AliyunOcrBusinessCard("https://hzstatic.hzinsights.com/static/images/202403/20240327/kjafdja3BWwThD1ZJexE0S4Skqn2.jpg")
  92. //}
  93. // 阿里云名片识别更换2024-01-02
  94. func AliyunOcrBusinessCard(imageurl string) (result *OcrBusinessCardResult, err error) {
  95. postUrl := "https://bizcard.market.alicloudapi.com/rest/160601/ocr/ocr_business_card.json"
  96. //method := "POST"
  97. item := new(OcrItem)
  98. item.Image = imageurl
  99. jsonBody, err := json.Marshal(item)
  100. if err != nil {
  101. fmt.Println("json.Marshal Err:" + err.Error())
  102. return
  103. }
  104. tr := &http.Transport{
  105. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  106. }
  107. client := &http.Client{Transport: tr}
  108. req, err := http.NewRequest("POST", postUrl, strings.NewReader(string(jsonBody)))
  109. req.Header.Add("Authorization", "APPCODE "+AliyunOcrBusinessCardCode)
  110. req.Header.Add("Content-Type", "application/json")
  111. res, err := client.Do(req)
  112. if err != nil {
  113. fmt.Println(err)
  114. return
  115. }
  116. defer res.Body.Close()
  117. body, err := io.ReadAll(res.Body)
  118. if err != nil {
  119. fmt.Println(err)
  120. return
  121. }
  122. err = json.Unmarshal(body, &result)
  123. if err != nil {
  124. fmt.Println(err)
  125. return
  126. }
  127. //处理返回的手机号格式,并进行数据合并
  128. var telCell []string
  129. if len(result.TelCell) > 0 {
  130. for _, v := range result.TelCell {
  131. telCell = append(telCell, utils.GetArabicNumbers(v))
  132. }
  133. }
  134. if len(result.TelWork) > 0 {
  135. for _, v := range result.TelWork {
  136. telCell = append(telCell, utils.GetArabicNumbers(v))
  137. }
  138. }
  139. logItem := new(models.AliyunOcrLog)
  140. logItem.ImgUrl = imageurl
  141. logItem.Result = string(body)
  142. logItem.CreateTime = time.Now()
  143. err = models.AddAliyunOcrLog(logItem)
  144. return
  145. }