BasicLife.php 2.9 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. /**
  28. * 设置配置信息
  29. * @param array $config
  30. * @return $this
  31. */
  32. public function config(array $config = []): static
  33. {
  34. $this->config = $config;
  35. return $this;
  36. }
  37. /**
  38. * 释放授权
  39. * @return $this
  40. */
  41. public function token(): static
  42. {
  43. $this->header = [
  44. "access-token" => $this->getAccessToken()
  45. ];
  46. return $this;
  47. }
  48. public function getAccessToken()
  49. {
  50. try {
  51. echo $this->getPrefix()."\n";
  52. $accessToken = Cache::get($this->getPrefix());
  53. if (!empty($accessToken)) return $accessToken;
  54. echo "Token:{$accessToken}\n";
  55. print_r($this->config);
  56. $result = (new Token)->config($this->config)->getAccessToken();
  57. print_r($result);
  58. if (empty($result)) return "获取Token失败";
  59. Cache::set($this->getPrefix(),$result['access_token'],$result['expires_in']);
  60. return $result['access_token'];
  61. } catch (\Throwable $throwable) {
  62. return "";
  63. }
  64. }
  65. /**
  66. * @return string
  67. */
  68. protected function getPrefix(): string
  69. {
  70. return str_replace("[appid]",$this->config['appid'],$this->prefix);
  71. }
  72. /**
  73. * @param string $url
  74. * @param array $data
  75. * @param string $field
  76. * @return array
  77. */
  78. public function curlPostApi(string $url = "", array $data = [], string $field = "data"): array
  79. {
  80. $result = Http::asJson()->withHeaders($this->header)->post($this->gateway.$url,$data)->array();
  81. if (isset($result['err_no']) && $result['err_no'] <> 0) return ['msg' => $result['err_msg']];
  82. if(!empty($result[$field]))
  83. {
  84. return $result[$field];
  85. }
  86. return [];
  87. }
  88. /**
  89. * @param string $url
  90. * @param array $data
  91. * @param string $field
  92. * @return array
  93. */
  94. public function curlGetApi(string $url = "", array $data = [], string $field = "data"): array
  95. {
  96. $result = Http::asJson()->withProxy("")->withHeaders($this->header)->get($this->gateway.$url,$data)->array();
  97. if(!empty($result[$field]))
  98. {
  99. return $result[$field];
  100. }
  101. return [];
  102. }
  103. }