<?php

namespace app\controller\api;

use app\extra\basic\Base;
use app\extra\tools\UploadExtend;
use app\middleware\WxMiddleware;
use app\model\saas\SaasUpload;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;
use LinFly\Annotation\Route\Middleware;

#[Controller(prefix: "/wx_api/upload"),Middleware(WxMiddleware::class)]
class Uploads extends Base
{


    #[Route(path: "temp",methods: "post")]
    public function getCosTemp()
    {
        try {

        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 上传文件
     * @param Request $request
     * @return Response
     */
    #[Route(path: "data",methods: "post")]
    public function uploadData(Request $request): Response
    {
        try {
            $param = $request->post();
            $resp = UploadExtend::disk(UploadExtend::MODE_LOCAL)->uploadFile();
            events("clear-upload",['key' => $param['key']]);
            (new SaasUpload())->insertGetId([
                "key"       => $param['key'],
                "path"      => $resp[0]['url'],
                "agent_id"  => $param['shop']
            ]);
            if (!isset($resp[0]['url'])) return errorTrans(40010);
            return successTrans("success.data",[
                "fileName"  => $resp[0]['origin_name'],
                "src"       => $resp[0]['url'],
            ],200);
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }
    }

}