BasicLife.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\extra\life;
  3. use app\extra\life\data\Token;
  4. use support\think\Cache;
  5. use yzh52521\EasyHttp\Http;
  6. class BasicLife
  7. {
  8. /**
  9. * 网关
  10. * @var string
  11. */
  12. protected string $gateway = "https://open.douyin.com/";
  13. /**
  14. * 配置信息
  15. * @var array
  16. */
  17. protected array $config = [];
  18. /**
  19. * 默认header
  20. * @var string[]
  21. */
  22. protected array $header = [];
  23. /**
  24. * AccessToken前缀
  25. * @var string
  26. */
  27. protected string $prefix = "dy_[appid]_access_token";
  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. * https://off-api.hqgtn.cn/life/token
  42. */
  43. public function token(): static
  44. {
  45. if ($this->config['version'] == "test") { // 测试
  46. $token = json_decode(file_get_contents("https://off-api.hqgtn.cn/life/token"),true);
  47. $this->header = [
  48. "access-token" => $token['data']['token']
  49. ];
  50. } else {
  51. $this->header = [
  52. "access-token" => $this->getAccessToken()
  53. ];
  54. }
  55. return $this;
  56. }
  57. public function getAccessToken()
  58. {
  59. try {
  60. $accessToken = Cache::get($this->getPrefix());
  61. if (!empty($accessToken)) return $accessToken;
  62. $result = (new Token)->config($this->config)->getAccessToken();
  63. if (empty($result)) return "获取Token失败";
  64. Cache::set($this->getPrefix(),$result['access_token'],$result['expires_in']);
  65. return $result['access_token'];
  66. } catch (\Throwable $throwable) {
  67. return "";
  68. }
  69. }
  70. /**
  71. * @return string
  72. */
  73. protected function getPrefix(): string
  74. {
  75. return str_replace("[appid]",$this->config['appid'],$this->prefix);
  76. }
  77. /**
  78. * @param string $url
  79. * @param array $data
  80. * @param string $field
  81. * @return array
  82. */
  83. public function curlPostApi(string $url = "", array $data = [], string $field = "data"): array
  84. {
  85. $result = Http::asJson()->withHeaders($this->header)->post($this->gateway.$url,$data)->array();
  86. if (isset($result['err_no']) && $result['err_no'] <> 0) return ['msg' => $result['err_msg']];
  87. if(!empty($result[$field]))
  88. {
  89. return $result[$field];
  90. }
  91. return [];
  92. }
  93. /**
  94. * @param string $url
  95. * @param array $data
  96. * @param string $field
  97. * @return array
  98. */
  99. public function curlGetApi(string $url = "", array $data = [], string $field = "data"): array
  100. {
  101. $result = Http::asJson()->withHeaders($this->header)->get($this->gateway.$url,$data)->array();
  102. if(!empty($result[$field]))
  103. {
  104. return $result[$field];
  105. }
  106. return [];
  107. }
  108. }