aliyun.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package services
  2. import (
  3. "crypto/tls"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "hongze/hongze_web_mfyx/models"
  8. "hongze/hongze_web_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. // 阿里云名片识别更换2024-01-02
  91. func AliyunOcrBusinessCard(imageurl string) (result *OcrBusinessCardResult, err error) {
  92. postUrl := "https://bizcard.market.alicloudapi.com/rest/160601/ocr/ocr_business_card.json"
  93. //method := "POST"
  94. item := new(OcrItem)
  95. item.Image = imageurl
  96. jsonBody, err := json.Marshal(item)
  97. if err != nil {
  98. fmt.Println("json.Marshal Err:" + err.Error())
  99. return
  100. }
  101. tr := &http.Transport{
  102. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  103. }
  104. client := &http.Client{Transport: tr}
  105. req, err := http.NewRequest("POST", postUrl, strings.NewReader(string(jsonBody)))
  106. req.Header.Add("Authorization", "APPCODE "+AliyunOcrBusinessCardCode)
  107. req.Header.Add("Content-Type", "application/json")
  108. res, err := client.Do(req)
  109. if err != nil {
  110. fmt.Println(err)
  111. return
  112. }
  113. defer res.Body.Close()
  114. body, err := io.ReadAll(res.Body)
  115. if err != nil {
  116. fmt.Println(err)
  117. return
  118. }
  119. err = json.Unmarshal(body, &result)
  120. if err != nil {
  121. fmt.Println(err)
  122. return
  123. }
  124. //处理返回的手机号格式,并进行数据合并
  125. var telCell []string
  126. if len(result.TelCell) > 0 {
  127. for _, v := range result.TelCell {
  128. telCell = append(telCell, utils.GetArabicNumbers(v))
  129. }
  130. }
  131. if len(result.TelWork) > 0 {
  132. for _, v := range result.TelWork {
  133. telCell = append(telCell, utils.GetArabicNumbers(v))
  134. }
  135. }
  136. logItem := new(models.AliyunOcrLog)
  137. logItem.ImgUrl = imageurl
  138. logItem.Result = string(body)
  139. logItem.CreateTime = time.Now()
  140. err = models.AddAliyunOcrLog(logItem)
  141. return
  142. }