BasicLife.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\extra\dyLife;
  3. use support\think\Cache;
  4. use yzh52521\EasyHttp\Http;
  5. class BasicLife
  6. {
  7. /**
  8. * 网关
  9. * @var string
  10. */
  11. protected string $gateway = "https://open.douyin.com/";
  12. /**
  13. * 配置信息
  14. * @var array
  15. */
  16. protected array $config = [];
  17. /**
  18. * 默认header
  19. * @var string[]
  20. */
  21. protected array $header = [];
  22. /**
  23. * AccessToken前缀
  24. * @var string
  25. */
  26. protected string $prefix = "dy_[appid]_access_token";
  27. protected string $baseAccountId = "7595228109947947043";
  28. /**
  29. * 设置配置信息
  30. * @param array $config
  31. * @return $this
  32. */
  33. public function config(array $config = []): static
  34. {
  35. $this->config = $config;
  36. return $this;
  37. }
  38. /**
  39. * 释放授权
  40. * @return $this
  41. */
  42. public function token(): static
  43. {
  44. $this->header = [
  45. "access-token" => $this->getAccessToken()
  46. ];
  47. return $this;
  48. }
  49. public function getAccessToken()
  50. {
  51. try {
  52. $accessToken = Cache::get($this->getPrefix());
  53. if (!empty($accessToken)) return $accessToken;
  54. $result = (new Token)->config($this->config)->getAccessToken();
  55. if (empty($result)) return "获取Token失败";
  56. Cache::set($this->getPrefix(),$result['access_token'],$result['expires_in']);
  57. return $result['access_token'];
  58. } catch (\Throwable $throwable) {
  59. return "";
  60. }
  61. }
  62. /**
  63. * @return string
  64. */
  65. protected function getPrefix(): string
  66. {
  67. return str_replace("[appid]",$this->config['appid'],$this->prefix);
  68. }
  69. /**
  70. * @param string $url
  71. * @param array $data
  72. * @param string $field
  73. * @return array
  74. */
  75. public function curlPostApi(string $url = "", array $data = [], string $field = "data"): array
  76. {
  77. print_r($this->header);
  78. $result = Http::asJson()->withHeaders($this->header)->post($this->gateway.$url,$data)->array();
  79. if (isset($result['err_no']) && $result['err_no'] <> 0) return ['msg' => $result['err_msg']];
  80. if(!empty($result[$field]))
  81. {
  82. return $result[$field];
  83. }
  84. return [];
  85. }
  86. /**
  87. * @param string $url
  88. * @param array $data
  89. * @param string $field
  90. * @return array
  91. */
  92. public function curlGetApi(string $url = "", array $data = [], string $field = "data"): array
  93. {
  94. print_r($this->header);
  95. $result = Http::asJson()->withProxy("")->withHeaders($this->header)->get($this->gateway.$url,$data)->array();
  96. if(!empty($result[$field]))
  97. {
  98. return $result[$field];
  99. }
  100. return [];
  101. }
  102. }