<?php
namespace app\api\controller;
use support\Request;
use support\Response;
use Shopwwi\WebmanFilesystem\FilesystemFactory;
use Shopwwi\WebmanFilesystem\Facade\Storage;
use hg\apidoc\annotation as Apidoc;
use taoser\facade\Validate;
/**
 * 基础控制器
 * @Apidoc\NotParse()
 * @Apidoc\NotDebug()
 */
class BaseController
{
    /**
     * 不需要鉴权的方法
     * @var array
     */
    public $noNeedAuth = [];

    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    public $noNeedLogin = [];
    function __construct()
    {
        $this->_init();
    }
    protected function _init(){
    }
    /**
     * 返回格式化json数据
     *
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return Response
     */
    protected function json(int $code, string $msg = 'ok', array|object|null $data = []): Response
    {
        return json(['code' => $code, 'data' => $data, 'msg' => $msg]);
    }

    protected function success(string $msg = '成功', array|object|null $data = []): Response
    {
        return $this->json(0, $msg, $data);
    }

    protected function fail(string $msg = '失败', array|object|null $data = []): Response
    {
        return $this->json(1,$msg, $data);
    }
    protected function error(string $msg = '失败', array|object|null $data = []): Response
    {
        return $this->json(1,$msg, $data);
    }

    /**
     * @Apidoc\Title("上传")
     * @Apidoc\Method("POST")
     */
    function upload(Request $request)
    {
        try{
            $user = \support\Jwt::getUser();
        }catch(\Exception $e){
            $user = ['id'=>0];
        }
        $savePath = $request->post('savePath','files');
	
        $validate = Validate::rule('savePath', 'alphaNum');
        $data = ['savePath'  => $savePath];
        if (!$validate->check($data)) {
            return $this->fail( '参数错误:'.$validate->getError());
        }
        $savePath = trim($savePath,'/');
        $savePath = 'upload/'.$savePath.'/'.$user['id'];
        \support\Log::alert('savePath:'.$savePath);
        $mimetype = explode(',',Config('site.upload_mimetype'));
        $maxsize = Config('site.upload_maxsize')*1024*1024;
        //多文件上传
        $files = $request->file();
        try {
            $result = Storage::adapter('public')
                ->path($savePath)
                ->size($maxsize)
                ->extYes($mimetype)
                ->uploads($files,0,$maxsize * count($files),false);
            $save_datas = [];
            foreach($result as $k=>$fileinfo){
                $save_datas[] = [
                    'user_id'   =>  $user['id'],
                    'category'  =>  'default',
                    'title'     =>  $fileinfo->origin_name,
                    'path'      =>  $fileinfo->file_name,
                    'size'      =>  $fileinfo->size,
                    'mime_type' =>  $fileinfo->mime_type,
                    'extension' =>  $fileinfo->extension,
                    'height'    =>  $fileinfo->file_height,
                    'width'     =>  $fileinfo->file_width,
                    'sha1'      =>  sha1_file(public_path($fileinfo->file_name)),
                    'use_count' =>  0,
                ];
            }
            \app\model\Files::insertAll($save_datas);
            return $this->success(__('successful'),$result);
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
    }
    

}