BasicLife.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if (str_contains($this->config['appid'], 'tt')) { // 小程序
  45. $token = json_decode(file_get_contents("https://tran.jsshuita.cn/test/mini/token"),true);
  46. $this->header = [
  47. "access-token" => $token['data']['data']
  48. ];
  49. } else {
  50. $this->header = [
  51. "access-token" => $this->getAccessToken()
  52. ];
  53. }
  54. return $this;
  55. }
  56. public function getAccessToken()
  57. {
  58. try {
  59. $accessToken = Cache::get($this->getPrefix());
  60. if (!empty($accessToken)) return $accessToken;
  61. $result = (new Token)->config($this->config)->getAccessToken();
  62. if (empty($result)) return "获取Token失败";
  63. Cache::set($this->getPrefix(),$result['access_token'],$result['expires_in']);
  64. return $result['access_token'];
  65. } catch (\Throwable $throwable) {
  66. return "";
  67. }
  68. }
  69. /**
  70. * @return string
  71. */
  72. protected function getPrefix(): string
  73. {
  74. return str_replace("[appid]",$this->config['appid'],$this->prefix);
  75. }
  76. /**
  77. * @param string $url
  78. * @param array $data
  79. * @param string $field
  80. * @return array
  81. */
  82. public function curlPostApi(string $url = "", array $data = [], string $field = "data"): array
  83. {
  84. $result = Http::asJson()->withHeaders($this->header)->post($this->gateway.$url,$data)->array();
  85. print_r($result);
  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()->withProxy("")->withHeaders($this->header)->get($this->gateway.$url,$data)->array();
  102. if(!empty($result[$field]))
  103. {
  104. return $result[$field];
  105. }
  106. return [];
  107. }
  108. }