You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.8 KiB
96 lines
2.8 KiB
|
2 days ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Services\Course\RemoteCourseAssetImporter;
|
||
|
|
use App\Services\Sstbc\SstbcApiClient;
|
||
|
|
use App\Support\ApiResponse;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use RuntimeException;
|
||
|
|
use Throwable;
|
||
|
|
|
||
|
|
class SstbcCourseController extends Controller
|
||
|
|
{
|
||
|
|
use ApiResponse;
|
||
|
|
|
||
|
|
public function __construct(
|
||
|
|
protected SstbcApiClient $client,
|
||
|
|
protected RemoteCourseAssetImporter $assetImporter,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
public function index(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$data = $request->validate([
|
||
|
|
'page' => ['nullable', 'integer', 'min:1'],
|
||
|
|
'page_size' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||
|
|
'keyword' => ['nullable', 'string', 'max:128'],
|
||
|
|
]);
|
||
|
|
|
||
|
|
$params = [
|
||
|
|
'page' => (int) ($data['page'] ?? 1),
|
||
|
|
'page_size' => (int) ($data['page_size'] ?? 10),
|
||
|
|
'sort_name' => 'start_date',
|
||
|
|
'sort_type' => 'DESC',
|
||
|
|
'show_relation' => ['type_detail'],
|
||
|
|
'filter' => [[
|
||
|
|
'key' => 'is_fee',
|
||
|
|
'op' => 'eq',
|
||
|
|
'value' => 0,
|
||
|
|
]],
|
||
|
|
];
|
||
|
|
|
||
|
|
if (! empty($data['keyword'])) {
|
||
|
|
$params['filter'][] = [
|
||
|
|
'key' => 'name',
|
||
|
|
'op' => 'like',
|
||
|
|
'value' => $data['keyword'],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$remote = $this->client->fetchCourses($params);
|
||
|
|
} catch (Throwable $e) {
|
||
|
|
return $this->fail($e->getMessage(), 502);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->ok([
|
||
|
|
'items' => $remote['data'] ?? [],
|
||
|
|
'meta' => [
|
||
|
|
'current_page' => (int) ($remote['current_page'] ?? 1),
|
||
|
|
'per_page' => (int) ($remote['per_page'] ?? $params['page_size']),
|
||
|
|
'total' => (int) ($remote['total'] ?? 0),
|
||
|
|
'last_page' => (int) ($remote['last_page'] ?? 1),
|
||
|
|
],
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function importPromo(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$data = $request->validate([
|
||
|
|
'url' => ['required', 'string', 'url', 'max:512'],
|
||
|
|
]);
|
||
|
|
|
||
|
|
$url = $data['url'];
|
||
|
|
$allowedHost = parse_url((string) config('sstbc.base_url'), PHP_URL_HOST);
|
||
|
|
$urlHost = parse_url($url, PHP_URL_HOST);
|
||
|
|
|
||
|
|
if (! $allowedHost || $urlHost !== $allowedHost) {
|
||
|
|
return $this->fail('仅允许导入商学院域名下的宣传页图片', 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$media = $this->assetImporter->importFromUrl(
|
||
|
|
$url,
|
||
|
|
'promos',
|
||
|
|
(string) config('sstbc.base_url'),
|
||
|
|
);
|
||
|
|
} catch (RuntimeException $e) {
|
||
|
|
return $this->fail($e->getMessage(), 502);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->ok($media->toApiArray(), '宣传页已导入');
|
||
|
|
}
|
||
|
|
}
|