<?php

namespace app\workerapi\controller;

use app\workerapi\controller\BaseApiController;
use app\common\enum\FileEnum;
use app\workerapi\validate\UploadFileValidate;
use Exception;
use app\common\service\UploadService;

class UploadController  extends BaseApiController
{
    public array $notNeedLogin = ['image'];
    public function image()
    {
        try {
            $result = UploadService::image(2, $this->userId,FileEnum::SOURCE_WORKER);
            return $this->success('上传成功', $result);
        } catch (Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function uploadFile()
    {
        // dd($this->request->all());
        $params = (new UploadFileValidate())->post()->goCheck('uploadFile');
        try {
            if ($params['file_type'] == FileEnum::IMAGE_TYPE) {
                $result = UploadService::image(2, $this->userId, $params['source']);
            } else if ($params['file_type'] == FileEnum::VIDEO_TYPE) {
                $result = UploadService::video(2, $this->userId, $params['source']);
            } else if ($params['file_type'] == FileEnum::FILE_TYPE) {
                $result = UploadService::file(2, $this->userId, $params['source']);
            } else {
                throw  new \Exception('未定义文件类型');
            }
            return $this->success('上传成功', $result);
        } catch (Exception $e) {
            return $this->fail($e->getMessage());
        }
    }
}