qichacha.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hz_crm_api/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func GetCompanyInfo() {
  14. qccKey := `efd43bfceaa74cf1811079dbcec25c33`
  15. qccSecretKey := `2985D128630B085C1BFE8F88630B5DA8`
  16. getUrl := `http://api.qichacha.com/ECIV4/Search?key=` + qccKey + `&keyword=913300007125582210`
  17. timespan := time.Now().Unix()
  18. timespanStr := strconv.FormatInt(timespan, 10)
  19. token := utils.MD5(qccKey + timespanStr + qccSecretKey)
  20. client := &http.Client{}
  21. //提交请求
  22. reqest, err := http.NewRequest("GET", getUrl, nil)
  23. //增加header选项
  24. reqest.Header.Add("Token", token)
  25. reqest.Header.Add("Timespan", timespanStr)
  26. if err != nil {
  27. panic(err)
  28. }
  29. //处理返回结果
  30. response, _ := client.Do(reqest)
  31. defer response.Body.Close()
  32. body, err := ioutil.ReadAll(response.Body)
  33. fmt.Println("body:", string(body))
  34. utils.FileLog.Info("%s", string(body))
  35. }
  36. type QCCResp struct {
  37. Paging Paging `json:"Paging"`
  38. Result []Result `json:"Result"`
  39. Status string `json:"Status"`
  40. Message string `json:"Message"`
  41. OrderNumber string `json:"OrderNumber"`
  42. }
  43. type Paging struct {
  44. PageSize int `json:"PageSize"`
  45. PageIndex int `json:"PageIndex"`
  46. TotalRecords int `json:"TotalRecords"`
  47. }
  48. type Result struct {
  49. KeyNo string `json:"KeyNo"`
  50. Name string `json:"Name"`
  51. CreditCode string `json:"CreditCode"`
  52. StartDate string `json:"StartDate"`
  53. OperName string `json:"OperName"`
  54. Status string `json:"Status"`
  55. No string `json:"No"`
  56. }
  57. func QCCFuzzySearch(keyWord string) (results []Result, err error) {
  58. qccKey := `49464d73ae1c46e99ad4eff1efe943c1`
  59. qccSecretKey := `9DE7E83FFE26789815004C5172503290`
  60. getUrl := `http://api.qichacha.com/FuzzySearch/GetList?pageSize=20&key=%s&searchKey=%s `
  61. utils.StrFilterNonChinese(&keyWord)
  62. finalUrl := fmt.Sprintf(getUrl, qccKey, keyWord)
  63. timespan := time.Now().Unix()
  64. timespanStr := strconv.FormatInt(timespan, 10)
  65. token := utils.MD5(qccKey + timespanStr + qccSecretKey)
  66. client := &http.Client{}
  67. //提交请求
  68. reqest, err := http.NewRequest("GET", finalUrl, nil)
  69. if err != nil {
  70. utils.FileLog.Info("NewRequest err:" + err.Error())
  71. err = errors.New("NewRequest err:" + err.Error())
  72. return
  73. }
  74. //增加header选项
  75. reqest.Header.Add("Token", strings.ToUpper(token))
  76. reqest.Header.Add("Timespan", timespanStr)
  77. if err != nil {
  78. return
  79. }
  80. //处理返回结果
  81. response, _ := client.Do(reqest)
  82. defer response.Body.Close()
  83. body, err := ioutil.ReadAll(response.Body)
  84. var qccResp QCCResp
  85. err = json.Unmarshal(body, &qccResp)
  86. if err != nil {
  87. utils.FileLog.Info("Json Unmarshal err:" + err.Error())
  88. err = errors.New("Json Unmarshal err:" + err.Error())
  89. return
  90. }
  91. if qccResp.Status != "200" {
  92. utils.FileLog.Info("QCCFuzzySearch err:" + qccResp.Message)
  93. err = errors.New("QCCFuzzySearch err:" + qccResp.Message)
  94. return
  95. } else {
  96. results = qccResp.Result
  97. return
  98. }
  99. }