Skip to content

Commit a328ae9

Browse files
committed
feat:后台应用增加公共函数 get_ba_client
1 parent 6f40197 commit a328ae9

2 files changed

Lines changed: 25 additions & 29 deletions

File tree

app/admin/common.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use ba\Filesystem;
4+
use GuzzleHttp\Client;
45

56
if (!function_exists('get_controller_list')) {
67
function get_controller_list($app = 'admin'): array
@@ -9,3 +10,25 @@ function get_controller_list($app = 'admin'): array
910
return Filesystem::getDirFiles($controllerDir);
1011
}
1112
}
13+
14+
if (!function_exists('get_ba_client')) {
15+
/**
16+
* 获取一个请求 BuildAdmin 开源社区的 Client
17+
* @throws Throwable
18+
*/
19+
function get_ba_client(): Client
20+
{
21+
return new Client([
22+
'base_uri' => config('buildadmin.api_url'),
23+
'timeout' => 30,
24+
'connect_timeout' => 30,
25+
'verify' => false,
26+
'http_errors' => false,
27+
'headers' => [
28+
'X-REQUESTED-WITH' => 'XMLHttpRequest',
29+
'Referer' => dirname(request()->root(true)),
30+
'User-Agent' => 'BuildAdminClient',
31+
]
32+
]);
33+
}
34+
}

app/admin/library/module/Server.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use ba\Exception;
88
use ba\Filesystem;
99
use think\facade\Db;
10-
use GuzzleHttp\Client;
1110
use FilesystemIterator;
1211
use think\facade\Config;
1312
use RecursiveIteratorIterator;
@@ -21,8 +20,6 @@
2120
*/
2221
class Server
2322
{
24-
private static ?Client $client = null;
25-
2623
private static string $apiBaseUrl = '/api/v6.store/';
2724

2825
/**
@@ -33,7 +30,7 @@ public static function download(string $uid, string $dir, array $extend = []): s
3330
{
3431
$tmpFile = $dir . $uid . ".zip";
3532
try {
36-
$client = self::getClient();
33+
$client = get_ba_client();
3734
$response = $client->get(self::$apiBaseUrl . 'download', ['query' => array_merge(['uid' => $uid, 'server' => 1], $extend)]);
3835
$body = $response->getBody();
3936
$content = $body->getContents();
@@ -63,7 +60,7 @@ public static function download(string $uid, string $dir, array $extend = []): s
6360
public static function installPreCheck(array $query = []): bool
6461
{
6562
try {
66-
$client = self::getClient();
63+
$client = get_ba_client();
6764
$response = $client->get(self::$apiBaseUrl . 'preCheck', ['query' => $query]);
6865
$body = $response->getBody();
6966
$statusCode = $response->getStatusCode();
@@ -560,28 +557,4 @@ public static function getRuntime(string $dir, string $key = ''): mixed
560557
return $runtimeContentArr;
561558
}
562559
}
563-
564-
/**
565-
* 获取请求对象
566-
* @return Client
567-
*/
568-
protected static function getClient(): Client
569-
{
570-
$options = [
571-
'base_uri' => Config::get('buildadmin.api_url'),
572-
'timeout' => 30,
573-
'connect_timeout' => 30,
574-
'verify' => false,
575-
'http_errors' => false,
576-
'headers' => [
577-
'X-REQUESTED-WITH' => 'XMLHttpRequest',
578-
'Referer' => dirname(request()->root(true)),
579-
'User-Agent' => 'BuildAdminClient',
580-
]
581-
];
582-
if (is_null(self::$client)) {
583-
self::$client = new Client($options);
584-
}
585-
return self::$client;
586-
}
587560
}

0 commit comments

Comments
 (0)