WebUploadController.php
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace App\Http\Container\AppSection\Controllers;
use App\Helpers\JsonResource;
use App\Helpers\UploadHelper;
use App\Models\SystemConfig;
use Arr;
use Cache;
use Hikoon\LaravelApi\Support\ApiController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Str;
use Symfony\Component\HttpFoundation\StreamedResponse;
class WebUploadController extends ApiController
{
/**
* @return \Inertia\Response
*/
public function view(): Response
{
$config = SystemConfig::query()
->whereIn('identifier', ['logo', 'D2U8DkGdJUK7YlTyLou8Q', 'mawujfMPKtRrRvzUOxvWM'])
->pluck('content', 'identifier');
$uploadSts = UploadHelper::getStsToken();
return Inertia::render('upload', [
'logoSrc' => Arr::get($config, 'logo', ''),
'customerSrc' => Arr::get($config, 'mawujfMPKtRrRvzUOxvWM', ''),
'uploadAccept' => Arr::get($config, 'D2U8DkGdJUK7YlTyLou8Q', ''),
'ossConfig' => [
'region' => Str::before(UploadHelper::getOssEndpoint(true), '.'),
'bucket' => UploadHelper::getOssBucket(),
'domain' => UploadHelper::getOssDomain(),
'accessKeyId' => $uploadSts->accessKeyId,
'accessKeySecret' => $uploadSts->accessKeySecret,
'stsToken' => $uploadSts->securityToken,
]
]);
}
/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function Login(Request $request): JsonResponse
{
$request->validate(['code' => 'required|uuid'], ['code.*' => '非法访问']);
$key = 'login_code_:' . $request->offsetGet('code');
Cache::set($key, str_replace('Token ', '', $request->header('Authorization')), now()->addMinute());
return JsonResource::success(JsonResource::SUCCESS);
}
/**
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function subscribeLogin(Request $request): StreamedResponse
{
$request->validate(['code' => 'required|uuid', 'code.*' => '非法访问']);
return \Response::stream(static function () use ($request) {
$key = 'login_code_:' . $request->offsetGet('code');
while (!Cache::get($key)) {
sleep(1);
echo 'id: ' . now()->timestamp . PHP_EOL . "event: wait" . PHP_EOL . 'data: ' . PHP_EOL . 'retry: 1500' . PHP_EOL . PHP_EOL;
}
echo 'id: ' . now()->timestamp . PHP_EOL . "event: message" . PHP_EOL . 'data: Token ' . Cache::get($key) . PHP_EOL . PHP_EOL;
ob_flush();
flush();
}, 200, [
'Cache-Control' => 'no-cache',
'Access-Control-Allow-Origin' => '*',
'connection' => 'keep-alive',
'Content-Type' => 'text/event-stream',
'X-Accel-Buffering' => 'no'
]);
}
}