WebUploadController.php 3.08 KB
<?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'
        ]);
    }
}