| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126 | <<<<<<< HEAD<?phpdeclare(strict_types=1);/** * upload文件管理 * * @version      0.0.0 * @author      by huwhois * @time        2017/11/27 */namespace app\sys\controller;use Exception;use UnexpectedValueException;use DirectoryIterator;use think\facade\View;use think\File;use think\Image;use think\facade\Config;use think\exception\ValidateException;use app\common\service\FileService;use app\common\model\FileManager as FileManagerModel;use think\file\UploadedFile;class FileManager extends Base{    protected $modelName = 'FileManager';    protected $t_suffix = '_thumb';    protected $width = 400;  // 缩略图高度    protected $height = 300;    protected $storage_path = 'public/storage';    public function index()    {        $param = $this->request->param();        $param['limit'] = isset($param['limit']) ? (int) $param['limit'] : Config::get('app.page_size');        $list = FileManagerModel::queryPage($param);        View::assign('list', $list);        return View::fetch();    }    /**     * 浏览文件     */    public function explorer()    {        $param = $this->request->param();        $activepath =  isset($param['activepath']) ? $param['activepath'] : '';        $realpath = $this->app->getRootPath() . $this->storage_path . $activepath;        try {            $dirhandle = new DirectoryIterator($realpath);            $dirs = $files = [];            foreach ($dirhandle as $fileInfo) {                if($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {                    continue;                } elseif ($fileInfo->isDir()) {                    $dirs[] = $fileInfo->getFilename();                } elseif ($fileInfo->isFile()) {                    $files[] = [                        'filename'  => $fileInfo->getFilename(),                        'extension' => strtolower($fileInfo->getExtension()),                        'size' => format_bytes($fileInfo->getSize()),                        'time' => $fileInfo->getCTime(),                    ];                }            }                        $counts = count($dirs) + count($files);            $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);            View::assign([                'dirs'      => $dirs,                'files'     => $files,                'counts'    => $counts,                'activeurl' => $activeurl,                'activepath' => $activepath,            ]);            return View::fetch();        } catch(UnexpectedValueException $uve) {            $this->error($uve->getMessage());        }    }    /**     * 附件上传     */    public function uploadFile()    {        $activepath = $this->request->has('activepath') ? $this->request->param('activepath') : '';        $files = $this->request->file('upload_file');        if ($files) {            try {                validate(                    [                        'file' => [                            // 限制文件大小(单位b),这里限制为10M                            'fileSize' => 10 * 1024 * 1024,                            // 限制文件后缀,多个后缀以英文逗号分割                            'fileExt'  => 'jpg,png,gif,jpeg,webp,jfif,pdf,doc,docx,xls,xlsx,ppt,pptx,txt'                        ]                    ],                    [                        'file.fileSize' => '文件太大',                        'file.fileExt' => '不支持的文件后缀',                    ]                )->check(['file' => $files]);                $savenames = [];                $uploadFiles = [];                                if (!is_array($files) && $files instanceof UploadedFile) {                    $uploadFiles[] = $files;                } else {                    $uploadFiles = $files;                }                $url = Config::get('filesystem.disks.public.url');                foreach ($uploadFiles as $file) {                    $fileinfo = new FileManagerModel();                                        $fileinfo->title         = $file->getOriginalName();                    $fileinfo->filesize      = $file->getSize();                    $fileinfo->filetime      = $file->getCTime();                    $fileinfo->fileextension = $file->extension();                    $fileinfo->username      = $this->getSysUser()->username;                    $fileinfo->hash_md5      = $file->md5();                    if (empty($activepath)) {                        $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);                                                                $fileinfo->filepath = $url . '/' . $savename;                        $fileinfo->filename = basename($savename);                      } else {                        $savename = $activepath . '/' . $file->hashName();                        $savepath = $this->app->getRootPath() . $this->storage_path . $savename;                                                $file = $file->move($this->app->getRootPath() . $this->storage_path . $activepath, $savepath);                        $fileinfo->filepath = $url . $savename;                        $fileinfo->filename = $file->hashName();                    }                    $fileinfo->save();                    $savenames[] = $fileinfo->filepath;                }                return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);            } catch (ValidateException $e) {                $this->error($e->getMessage());            }        } else {            $this->error('图片不能为空');        }    }    /**     * 修改文件title     */    public function updateFileTitle($id = 0, $filetitle = '')    {        $fileInfo = FileManagerModel::find($id);        if ($fileInfo == null) {            return json(['code'=>1, 'fileid='.$id.'不存在']);        }        $fileInfo->title = $filetitle;        if ($fileInfo->save()) {            return json(['code'=>0, 'msg'=>'success']);        } else {            return json(['code'=>1, 'msg'=>'failed']);        }    }    /**     * 删除     * @param int|array $id  info id     * @return array     */    public function delete($id)    {        if ($this->request->isPost()) {            $whereOp = '=';            if (is_array($id)) {                $whereOp = 'IN';            }            $list = FileManagerModel::where('fileid', $whereOp, $id)->select();            foreach ($list as $value) {                $file = $this->app->getRootPath() . 'public' . $value->filepath;                if (file_exists($file)) {                    unlink($file);                }            }              if (FileManagerModel::destroy($id)) {                return ['code' => 0,'msg'=>'删除成功'];            } else {                return ['code' => 1,'msg'=>'删除失败'];            }        }    }    /**     * 删除空目录     */    public function deldir()    {        if ($this->request->isPost()) {            $dir = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('dir'));            $dir_name = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $dir;            try {                rmdir($dir_name);                return ['code' => 0, 'msg' => 'success'];            } catch (Exception $e) {                return ['code' => 1, 'msg' => 'failed, 目录不为空'];            }        }    }    /**     * 删除文件     */    public function delfile()    {        if ($this->request->isPost()) {            $filename = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('filename'));            $filepath = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $filename;            if (unlink($filepath)) {                return ['code' => 0, 'msg' => 'success'];            } else {                return ['code' => 1, 'msg' => 'failed'];            }        }    }    /**     * 处理上传的图片     */    protected function dealUploadImg(File $file, $water, $thumb, $width, $height, $overwrite)    {        $savename = "";        $thumbname = "";        if ($water == true || $thumb == true) {            $image = Image::open($file);            if ($water) {                $type = $this->system->water_type ?: Config::get('filesystem.water.type');                if ($type == 'water') {                    $watemark = $this->system->watermark ?: Config::get('filesystem.water.watermark');                    $image->water($watemark);                } else {                    $watetext = $this->system->watertext ?: Config::get('filesystem.water.watertext');                    $image->text($watetext, Config::get('filesystem.water.waterfont'), 30, '#ffffff30');                }            }            $savename = $file->hashName();            $realPath = Config::get('filesystem.disks.public.root') . '/' . $savename;            if ($thumb == true) {                if ($overwrite == true) {                    $image->thumb($width, $height, 1);                    $image->save($realPath);                } else {                    $image->save($realPath);                    $image->thumb($width, $height, 1);                    $ext = $file->extension();                    $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $savename)) . $this->t_suffix . '.' . $ext;                    $image->save(Config::get('filesystem.disks.public.root') . '/' . $thumbname);                }            } else {                $image->save($realPath);            }            unset($image);        } else {            $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);        }        return [            'picname'   => str_replace('\\', '/', $savename),            'thumbname' => $thumbname        ];    }    /**     * 图片上传(iframe 页面)     */    public function uploadimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)    {        View::assign([            'img_id' => $img_id,            'water'  => $water,            'thumb'  => $thumb,            'width'  => $width,            'height' => $height,            'overwrite' => $overwrite        ]);        return View::fetch();    }    /**     * 本地图片上传     * @file upload_file   上传的文件     * @param string  $img_id   图片ipnut text id 默认值 picture     * @param boolean $water    是否添加水印     * @param boolean $thumb    是否制作缩略图     * @param int $width        缩略图最大宽     * @param int $height       缩略图最大高     * @param bool $overwrite   生成缩略图后是否保存原图     */    public function uploadLocalImg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)    {        if ($this->request->isPost()) {            $file = $this->request->file('upload_file');            if ($file) {                try {                    validate(                        [                            'file' => [                                // 限制文件大小(单位b),这里限制为4M                                'fileSize' => 4 * 1024 * 1024,                                // 限制文件后缀,多个后缀以英文逗号分割                                'fileExt'  => 'jpg,png,gif,jpeg,webp,jfif'                            ]                        ],                        [                            'file.fileSize' => '文件太大',                            'file.fileExt' => '不支持的文件后缀',                        ]                    )->check(['file' => $file]);                                    $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);                    return array_merge(                        [                            'code' => 0,                            'img_id' => $img_id,                            'picture_url' => Config::get('filesystem.disks.public.url') . '/'                        ],                        $arr                    );                } catch (ValidateException $e) {                    $this->error($e->getMessage());                }            } else {                $this->error('图片不能为空');            }        }    }    /**     * 网络图片上传     * @paran string url_file   网络图片地址     * @param string  $img_id   图片ipnut text id 默认值 picture     * @param boolean $water    是否添加水印     * @param boolean $thumb    是否制作缩略图     * @param int $width        缩略图最大宽     * @param int $height       缩略图最大高     * @param bool $overwrite   生成缩略图后是否保存原图     */    public function uploadUrlImg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)    {        if ($this->request->isPost()) {            $urlImg = $this->request->param('url_file');            if ($urlImg) {                try {                    $fileService = new FileService();                    $file = $fileService->downloadUrlImg($urlImg);                    $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);                    @unlink($file->realPath);                    return array_merge(                        [                            'code' => 0,                            'img_id' => $img_id,                            'picture_url' => Config::get('filesystem.disks.public.url') . '/'                        ],                        $arr                    );                } catch (\think\exception\ValidateException $e) {                    $this->error($e->getMessage());                }            } else {                $this->error('图片地址不能为空');            }        }    }    /**     * 选择服务器图片     * @paran string online_file  服务器图片地址     * @param string  $img_id   图片ipnut text id 默认值 picture     * @param boolean $water    是否添加水印     * @param boolean $thumb    是否制作缩略图     * @param int $width        缩略图最大宽     * @param int $height       缩略图最大高     * @param bool $overwrite   生成缩略图后是否保存原图     */    public function uploadonlineimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)    {        if ($this->request->isPost()) {            $pathImg = $this->request->param('online_file');            if ($pathImg && file_exists($this->app->getRootPath() . "public" . $pathImg)) {                $picname = $pathImg;                $thumbname = "";                if ($thumb) {                    if (stripos($picname, $this->t_suffix)) {                        $thumbname = $pathImg;                    } else {                        try {                            $file = new File($this->app->getRootPath() . "public"  . $pathImg);                            $ext = $file->getExtension();                            $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $picname)) . $this->t_suffix . '.' . $ext;                            if (!file_exists($thumbname)) {                                $image = Image::open($file);                                $image->thumb($width, $height, 1);                                $image->save($this->app->getRootPath() . "public"  .  $thumbname);                                unset($image);                            }                            if ($overwrite) {                                $picname = $thumbname;                            }                        } catch (Exception $e) {                            $this->error($e->getMessage());                        }                    }                }                return [                    'code' => 0,                    'img_id' => $img_id,                    'picname' => $picname,                    'thumbname' => $thumbname,                ];            } else {                $this->error('图片地址不存在');            }        }    }    public function onlineimg($page)    {        $files =  FileService::getFiles(Config::get('filesystem.disks.public.root'));        if (!count($files)) {            return json_encode(array(                "state" => "no match file",                "list" => array(),                "start" => $page,                "total" => count($files)            ));        }        /* 获取指定范围的列表 */        $page = $page - 1 < 0 ? 0 : (int)$page - 1;        $size = 20;        $start = $page * $size;        $end = $start + $size;        $len = count($files);        for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {            $list[] = $files[$i];        }        return json([            "code" => 0,            "list" => $list,            "page" => $page + 1,            "total" => count($files)        ]);    }    /**     * 判断(远程)文件是否为图片     */    // public function isImg()    // {    //     if ($this->request->isAjax()) {    //         $filename = $this->request->param('filename');    //         $res = \mylib\GetImageByurl::isImg($filename);    //         if (!$res) {    //             $this->error('该图片不合法');    //         } else {    //             return ['code'=>2,'msg'=>'图片地址可用'];    //         }    //     }    // }    /**     * ckeditor 富文本编辑器上传图片     */    public function ckeditorUploadImage()    {        if ($this->request->isPost()) {            $file = $this->request->file('upload');            if ($file) {                try {                    validate(                        [                            'file' => [                                // 限制文件大小(单位b),这里限制为4M                                'fileSize' => 4 * 1024 * 1024,                                // 限制文件后缀,多个后缀以英文逗号分割                                'fileExt'  => 'jpg,png,gif,jpeg,webp,jfif'                            ]                        ],                        [                            'file.fileSize' => '文件太大',                            'file.fileExt' => '不支持的文件后缀',                        ]                    )->check(['file' => $file]);                    $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);                    return json([                        'uploaded' => 1,                        'fileName' => basename($savename),                        'url' => Config::get('filesystem.disks.public.url') . '/' . $savename                    ]);                } catch (\think\exception\ValidateException $e) {                    $this->error($e->getMessage());                    return json([                        'uploaded' => 1,                        'error' =>  ['message' => $e->getMessage()]                    ]);                }            } else {                return json([                    'uploaded' => 1,                    'error' =>  ['message' => '图片不能为空']                ]);            }        }    }}=======<?phpdeclare(strict_types=1);/** * upload文件管理 * * @version      0.0.0 * @author      by huwhois * @time        2017/11/27 */namespace app\sys\controller;use Exception;use UnexpectedValueException;use DirectoryIterator;use think\facade\View;use think\facade\Config;use think\File;use think\Image;use think\exception\ValidateException;use think\file\UploadedFile;use app\common\service\FileService;use app\common\model\FileManager as FileManagerModel;use app\common\facade\FileUtils;class FileManager extends Base{    protected $modelName = 'FileManager';    protected $t_suffix = '_thumb';    protected $width = 400;  // 缩略图高度    protected $height = 300;    protected $storage_path = 'public/storage';        public function index()    {        $param = $this->request->param();        $param['limit'] = isset($param['limit']) ? (int) $param['limit'] : Config::get('app.page_size');        $list = FileManagerModel::queryPage($param);        View::assign('list', $list);                return View::fetch();    }    public function queryList()    {        $param = $this->request->param();        $list = FileManagerModel::queryList($param);        return json(['code'=>0, 'list'=>$list]);    }    /**     * 浏览文件     */    public function explorer()    {        $param = $this->request->param();        $activepath =  isset($param['activepath']) ? $param['activepath'] : '';        $realpath = $this->app->getRootPath() . $this->storage_path . $activepath;        try {            $dirhandle = new DirectoryIterator($realpath);            $dirs = $files = [];            foreach ($dirhandle as $fileInfo) {                if($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {                    continue;                } elseif ($fileInfo->isDir()) {                    $dirs[] = $fileInfo->getFilename();                } elseif ($fileInfo->isFile()) {                    $files[] = [                        'filename'  => $fileInfo->getFilename(),                        'extension' => strtolower($fileInfo->getExtension()),                        'size' => format_bytes($fileInfo->getSize()),                        'time' => $fileInfo->getCTime(),                    ];                }            }                        $counts = count($dirs) + count($files);            $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);            // halt($activeurl);            if ($this->request->isAjax()) {                return json([                    'code'      => 0,                    'dirs'      => $dirs,                    'files'     => $files,                    'counts'    => $counts,                    'activeurl' => $activeurl,                    'activepath'=> $activepath,                ]);            } else {                View::assign([                    'dirs'      => $dirs,                    'files'     => $files,                    'counts'    => $counts,                    'activeurl' => $activeurl,                    'activepath'=> $activepath,                ]);                                return View::fetch();            }                    } catch(UnexpectedValueException $uve) {            $this->error($uve->getMessage());        }    }    /**     * 附件上传     */    public function uploadFile()    {        $activepath = $this->request->has('activepath') ? $this->request->param('activepath') : '';        $files = $this->request->file('upload_file');        if ($files) {            try {                validate(                    [                        'file' => [                            // 限制文件大小(单位b),这里限制为10M                            'fileSize' => 10 * 1024 * 1024,                            // 限制文件后缀,多个后缀以英文逗号分割                            'fileExt'  => 'jpg,png,gif,jpeg,webp,jfif,pdf,doc,docx,xls,xlsx,ppt,pptx,txt'                        ]                    ],                    [                        'file.fileSize' => '文件太大',                        'file.fileExt' => '不支持的文件后缀',                    ]                )->check(['file' => $files]);                $savenames = [];                $uploadFiles = [];                                if (!is_array($files) && $files instanceof UploadedFile) {                    $uploadFiles[] = $files;                } else {                    $uploadFiles = $files;                }                $url = Config::get('filesystem.disks.public.url');                foreach ($uploadFiles as $file) {                    $fileinfo = new FileManagerModel();                                        $fileinfo->title         = $file->getOriginalName();                    $fileinfo->filesize      = $file->getSize();                    $fileinfo->filetime      = $file->getCTime();                    $fileinfo->fileextension = $file->extension();                    $fileinfo->username      = $this->getSysUser()->username;                    $fileinfo->hash_md5      = $file->md5();                    if (empty($activepath)) {                        $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);                                                $fileinfo->filepath = $url . '/' . $savename;                        $fileinfo->filename = basename($savename);                      } else {                        $savename = $activepath . '/' . $file->hashName();                        $savepath = $this->app->getRootPath() . $this->storage_path . $savename;                                                $file = $file->move($this->app->getRootPath() . $this->storage_path . $activepath, $savepath);                        $fileinfo->filepath = $url . $savename;                        $fileinfo->filename = $file->hashName();                    }                    $fileinfo->save();                    $savenames[] = $fileinfo->filepath;                }                return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);            } catch (ValidateException $e) {                $this->error($e->getMessage());            }        } else {            $this->error('图片不能为空');        }    }    /**     * 修改文件title     */    public function updateFileTitle($id = 0, $filetitle = '')    {        $fileInfo = FileManagerModel::find($id);        if ($fileInfo == null) {            return json(['code'=>1, 'fileid='.$id.'不存在']);        }        $fileInfo->title = $filetitle;        if ($fileInfo->save()) {            return json(['code'=>0, 'msg'=>'success']);        } else {            return json(['code'=>1, 'msg'=>'failed']);        }    }    /**     * 删除     * @param int|array $id  info id     * @return array     */    public function delete($id)    {        if ($this->request->isPost()) {            $whereOp = '=';            if (is_array($id)) {                $whereOp = 'IN';            }            $list = FileManagerModel::where('fileid', $whereOp, $id)->select();            foreach ($list as $value) {                $file = $this->app->getRootPath() . 'public' . $value->filepath;                if (file_exists($file)) {                    unlink($file);                }            }              if (FileManagerModel::destroy($id)) {                return ['code' => 0,'msg'=>'删除成功'];            } else {                return ['code' => 1,'msg'=>'删除失败'];            }        }    }    /**     * 删除空目录     */    public function deldir()    {        if ($this->request->isPost()) {            $dir = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('dir'));            $dir_name = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $dir;            try {                rmdir($dir_name);                return ['code' => 0, 'msg' => 'success'];            } catch (Exception $e) {                return ['code' => 1, 'msg' => 'failed, 目录不为空'];            }        }    }    /**     * 删除文件     */    public function delfile()    {        if ($this->request->isPost()) {            $filename = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('filename'));            $filepath = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $filename;            if (unlink($filepath)) {                return ['code' => 0, 'msg' => 'success'];            } else {                return ['code' => 1, 'msg' => 'failed'];            }        }    }    /**     * 图片上传(iframe 页面)     */    public function uploadimg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,        $original = false, $infoid = 0, $cjid = 0)    {        View::assign([            'img_id'    => $img_id,            'thumb'     => $thumb,            'width'     => $width,            'height'    => $height,            'original'  => $original,            'infoid'    => $infoid,            'cjid'      => $cjid        ]);        return View::fetch();    }    /**     * 本地图片上传     * @file upload_file   上传的文件     * @param string  $img_id   图片ipnut text id 默认值 picture     * @param boolean $thumb    是否制作缩略图     * @param int $width        缩略图最大宽     * @param int $height       缩略图最大高     * @param int $id           信息ID     * @param int $cjid         信息临时ID     * @param bool $saveoriginal   生成缩略图后是否保存原图 默认保存 saveoriginal     */    public function uploadLocalImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,        $original = false, $id = 0, $cjid = 0)    {        if ($this->request->isPost()) {            $file = $this->request->file('upload_file');            if ($file) {                try {                    validate(                        [                            'file' => [                                // 限制文件大小(单位b),这里限制为4M                                'fileSize' => 4 * 1024 * 1024,                                // 限制文件后缀,多个后缀以英文逗号分割                                'fileExt'  => 'jpg,png,gif,jpeg,webp,jfif'                            ]                        ],                        [                            'file.fileSize' => '文件太大',                            'file.fileExt' => '不支持的文件后缀',                        ]                    )->check(['file' => $file]);                    $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid);                    return json([                        'code'    => 0,                        'img_id'  => $img_id,                        'picname' => $result['picname'],                        'thumb'   => $result['thumbname']                    ]);                } catch (ValidateException $e) {                    $this->error($e->getMessage());                } catch (Exception $e) {                    $this->error($e->getMessage());                }            } else {                $this->error('图片不能为空');            }        }    }    /**     * 网络图片上传     * @paran string url_file   网络图片地址     * @param string  $img_id   图片ipnut text id 默认值 picture     * @param boolean $water    是否添加水印     * @param boolean $thumb    是否制作缩略图     * @param int $width        缩略图最大宽     * @param int $height       缩略图最大高     * @param bool $overwrite   生成缩略图后是否保存原图     */    public function uploadUrlImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,        $original = false, $id = 0, $cjid = 0)    {        if ($this->request->isPost()) {            $urlImg = $this->request->param('url_file');            if ($urlImg) {                try {                    $file = FileUtils::downloadUrlImg($urlImg);                                        $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid);                    // 删除临时文件                    @unlink($file->getRealPath());                    return json([                        'code'    => 0,                        'img_id'  => $img_id,                        'picname' => $result['picname'],                        'thumb'   => $result['thumbname']                    ]);                } catch (\think\exception\ValidateException $e) {                    $this->error($e->getMessage());                }            } else {                $this->error('图片地址不能为空');            }        }    }    /**     * 选择服务器图片     * @paran string online_file  服务器图片地址     * @param string  $img_id   图片ipnut text id 默认值 picture     */    public function uploadOnlineImg(string $img_id = 'picture')    {        if ($this->request->isPost()) {            $pathImg = $this->request->param('online_file');            if ($pathImg && file_exists($this->app->getRootPath() . "public" . $pathImg)) {                return json([                    'code'    => 0,                    'img_id'  => $img_id,                    'picname' => $pathImg,                    'thumb'   => ''                ]);            } else {                $this->error('图片地址不存在');            }        }    }    /**     * 判断(远程)文件是否为图片     */    // public function isImg()    // {    //     if ($this->request->isAjax()) {    //         $filename = $this->request->param('filename');    //         $res = \mylib\GetImageByurl::isImg($filename);    //         if (!$res) {    //             $this->error('该图片不合法');    //         } else {    //             return ['code'=>2,'msg'=>'图片地址可用'];    //         }    //     }    // }    /**     * ckeditor 富文本编辑器上传图片     */    public function ckeditorUploadImage()    {        if ($this->request->isPost()) {            $file = $this->request->file('upload');            if ($file) {                try {                    validate(                        [                            'file' => [                                // 限制文件大小(单位b),这里限制为4M                                'fileSize' => 4 * 1024 * 1024,                                // 限制文件后缀,多个后缀以英文逗号分割                                'fileExt'  => 'jpg,png,gif,jpeg,webp,jfif'                            ]                        ],                        [                            'file.fileSize' => '文件太大',                            'file.fileExt' => '不支持的文件后缀',                        ]                    )->check(['file' => $file]);                    $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);                    $savename = str_replace('\\', '/', $savename);                    $infoid   = (int) $this->request->param('infoid');                    $cjid     = (int) $this->request->param('cjid');                    $username = $this->getSysUser()->username;                                        FileManagerModel::saveFileInfo($file, $savename, $file->getOriginalName(), $infoid, $cjid, $username);                    return json([                        'uploaded' => 1,                        'fileName' => basename($savename),                        'url' => Config::get('filesystem.disks.public.url') . '/' . $savename                    ]);                } catch (\think\exception\ValidateException $e) {                    $this->error($e->getMessage());                    return json([                        'uploaded' => 1,                        'error' =>  ['message' => $e->getMessage()]                    ]);                }            } else {                return json([                    'uploaded' => 1,                    'error' =>  ['message' => '图片不能为空']                ]);            }        }    }    /**     * 处理上传的图片     */    protected function dealUploadImg(UploadedFile $file, $thumb, $width, $height, $original, $id, $cjid): array    {        $urlpath = Config::get('filesystem.disks.public.url');        $username = $this->getSysUser()->username;        $savename =  str_replace('\\', '/', $file->hashName());        $originalName = $file->getOriginalName();        $thumbname = "";        if ($thumb == true) {            $image = Image::open($file);                        $image->thumb($width, $height, 1);            $ext = $file->extension();                        if ($original == true) {                \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);                                $thumbname = str_replace('.' . $ext, '',  $savename) . $this->t_suffix . '.' . $ext;                $savepath = str_replace('\\', '/', Config::get('filesystem.disks.public.root') . '/' . $thumbname);                $image->save($savepath);                                FileManagerModel::saveFileInfo($savepath, $thumbname, $originalName, $id, $cjid, $username);                $thumbname = $urlpath . '/' . $thumbname;            } else {                $image->save(Config::get('filesystem.disks.public.root') . '/' . $savename);            }        } else {            \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);        }        $fileinfo = FileManagerModel::saveFileInfo($file, $savename, $originalName, $id, $cjid, $username);        return [            'picname'   => $fileinfo->filepath,            'thumbname' => $thumbname,        ];    }}>>>>>>> 78b76253c8ce5873016cf837373af5e30ac80c86
 |