douyinApi.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package youngee_talent_service
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/tidwall/gjson"
  7. "io/ioutil"
  8. "net/http"
  9. "youngmini_server/app/model/youngee_talent_model"
  10. )
  11. const (
  12. moreApiPrefix = "http://120.46.92.62:6888"
  13. )
  14. func getDouyinInfo(shareText string) *youngee_talent_model.DouyinInfoResp {
  15. // 1. 构造请求体
  16. reqBody := map[string]string{
  17. "share_text": shareText,
  18. }
  19. jsonBytes, err := json.Marshal(reqBody)
  20. if err != nil {
  21. fmt.Println("请求体编码失败:", err)
  22. return nil
  23. }
  24. // 2. 发起 POST 请求
  25. url := moreApiPrefix + "/api/douyin/user_data"
  26. resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonBytes))
  27. if err != nil {
  28. fmt.Println("请求失败:", err)
  29. return nil
  30. }
  31. defer resp.Body.Close()
  32. // 3. 读取响应
  33. respBytes, err := ioutil.ReadAll(resp.Body)
  34. if err != nil {
  35. fmt.Println("读取响应失败:", err)
  36. return nil
  37. }
  38. jsonStr := string(respBytes)
  39. // 4. 判断状态码
  40. if gjson.Get(jsonStr, "data.status_code").Int() != 0 {
  41. fmt.Println("接口返回错误:", gjson.Get(jsonStr, "data.status_msg").String())
  42. return nil
  43. }
  44. // 5. 解析字段
  45. return &youngee_talent_model.DouyinInfoResp{
  46. FollowerCount: gjson.Get(jsonStr, "data.user.follower_count").Int(),
  47. TotalFavorited: gjson.Get(jsonStr, "data.user.total_favorited").Int(),
  48. AwemeCount: gjson.Get(jsonStr, "data.user.aweme_count").Int(),
  49. Gender: int(gjson.Get(jsonStr, "data.user.gender").Int()),
  50. IPLocation: gjson.Get(jsonStr, "data.user.ip_location").String(),
  51. }
  52. }