<?php
namespace app\controller\manager;

use app\service\Image;
use app\model\{System, File};
use app\validate\Upload as VUpload;
use think\facade\{Filesystem, Config, Lang};
use think\Image as TImage;
use app\controller\BaseController;

class Upload extends BaseController
{
    private $isCompress = true;
    private $validate;
    private $uploadPath;
    private $uploadPathIsWritable = 0;

    public function __construct()
    {
        $system = System::getSystem();
        if (!empty($system)) {
            $this->isCompress = $system['compress']??true;
        }
        $this->validate = new VUpload();
        $this->uploadPath = Config::get('filesystem.disks.local.url');
        if(is_writable(app()->getRootPath() . 'public' . $this->uploadPath)){
            $this->uploadPathIsWritable = 1;
        }
    }

    //视频上传
    public function video()
    {
        if(!$this->uploadPathIsWritable){
            return $this->json(1, '上传文件夹需要写入权限');
        }
        $video = request()->file('video');
        if($this->validate->checkVideo($video)){
            $src = Filesystem::disk('video')->putFile(date('Ymd'), $video, 'uniqid');
            $src = $this->uploadPath . '/videos/' . $src;
            $return['src'] = $src;
            File::add($video, $src, 'video');    //加入上传文件表
            return $this->json(0, 'ok', $return);
        }else{
            $errorMsg = Lang::get($this->validate->getError());
            return $this->json(1, $errorMsg);
        }
    }
    
    //文件上传(通用)
    public function file()
    {
        $file = request()->file('file');
        if($this->validate->checkFile($file)){
            try{
                if(!$this->uploadPathIsWritable){
                    throw new \Exception('上传文件夹需要写入权限');
                }
                $src = Filesystem::putFile(date('Ymd'), $file, 'uniqid');
                $src = $this->uploadPath . '/' . $src;
                $return['src'] = $src;
                $return['name'] = $file->getOriginalName();
                File::add($file, $src, 'file');    //加入上传文件表
            } catch (\Exception $e) {
                return $this->json(1, $e->getMessage());
            }
            return $this->json(0,'ok',$return);
        }else{
            $errorMsg = Lang::get($this->validate->getError());
            return $this->json(1, $errorMsg);
        }
    }
    
    //图片上传(通用)
    public function image()
    {
        $image = request()->file('image');
        if($this->validate->checkImage($image)){
            try{
                if(!$this->uploadPathIsWritable){
                    throw new \Exception('上传文件夹需要写入权限');
                }
                $src = Filesystem::putFile(date('Ymd'), $image, 'uniqid');
                $src = $this->uploadPath . '/' . $src;
                $suffix = strtolower($image->getOriginalExtension());
                if($suffix == 'gif'){
                    $return['thumb_src'] = $src;    //TODO获取GIF缩略图
                }else{
                    $return['thumb_src'] = Image::getThumb($src, 100, 100, TImage::THUMB_SCALING);   //上传返回缩略图宽度为100
                }
                $return['src'] = $src;
                if($this->isCompress){
                    Image::resize($src);
                }
                File::add($image, $src);    //加入上传文件表
            } catch (\Exception $e) {
                return $this->json(1, $e->getMessage());
            }
            return $this->json(0, 'ok', $return);
        }else{
            $errorMsg = Lang::get($this->validate->getError());
            return $this->json(1, $errorMsg);
        }
    }

    //富文本编辑器商城图片
    public function wangImage()
    {
        
        $imageArr = request()->file('wang_img'); // 该方式,前端js上传方法中字段名称必须以数组形式传参 如 wang_img[] = 值
        $errno = 0;
        $data = [];
        if(!$this->uploadPathIsWritable){
            $errno = 1;
            $data[] = '上传文件夹需要写入权限';
        }else{
            foreach ($imageArr as $image) {
                if($this->validate->checkImage($image)){
                    $src = Filesystem::putFile(date('Ymd'), $image, 'uniqid');
                    $src = $this->uploadPath . '/' . $src;
                    $data[] = $src;
                    if($this->isCompress){
                        Image::resize($src);
                    }
                    File::add($image, $src);    //加入上传文件表
                }else{
                    $errno = 1;
                    $data = [];
                    $data[] = Lang::get($this->validate->getError());
                    break;
                }
            }
        }
        
        $return['errno'] = $errno;
        $return['data'] = $data;
        return json($return);
    }
}