| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- /**
- * author: nj
- * email:
- * date: 2021-10-16 14:43
- */
- namespace Modules\ManageApi\Controllers;
- use App\Repositories\UploadFileRepository;
- use App\Repositories\Utils\Check;
- use File;
- class PluploadController extends BaseController
- {
- private function runUpload()
- {
- $request = request();
- $result = self::getResetMsgResult();
- $upload_sys_info = get_upload_sys_info();
- $upload_default_params = array(
- 'manager_uid' => $this->ctManagerUid,
- 'user_type' => 2, //0=游客, 1=前台用户, 2=后台用户
- 'is_appointed' => 0, //0-不固定高宽 1-固定高宽
- 'is_water' => 0, //0-无水印 1-加水印
- 'max_width' => 0, //最大宽度
- 'max_height' => 0, //最大高度
- 'max_size' => $upload_sys_info['upload_max_size'], //允许最大上传尺寸,单位K
- 'ext_type' => 1, //扩展名类型 0-不限制 1-图片 2-附件 3-flash 4-多媒体
- 'thumb_width' => array(1600, 600, 120),
- 'thumb_height' => array(1600, 600, 120),
- 'thumb_water' => array(0, 0, 0),
- 'server_no' => config('image_servers')['upload_server_no'], //当前配置的服务器编号.
- );
- $upload_field_name = $request->input('upload_field_name', ''); //上传字段名
- $module_alias =$request->input('module_alias', ''); //模块
- $related_id = intval($request->input('related_id', 0)); //关联ID
- $item_alias = $request->input('item_alias', ''); //子项别名
- $item_id = intval($request->input('item_id', 0)); //子项ID
- $max_size = intval($request->input('max_size', 0)); //上传最大尺寸限制
- $thumb_appointed = $request->input('thumb_appointed', ''); //是否强制尺寸高宽
- $thumb_water = $request->input('thumb_water', ''); //是否加水印
- $thumb_num = intval($request->input('thumb_num', 0)); //缩略图数量
- $thumb_max_width = $request->input('thumb_max_width', ''); //缩略图最大宽度,多组用半角逗号分隔开
- $thumb_max_height = $request->input('thumb_max_height', '');//缩略图最大高度,多组用半角逗号分隔开
- $ext_type = intval($request->input('ext_type', 1)); //文件扩展名限制类型 0-不限制 1-图片 2-附件 3-flash 4-多媒体
- $sub_dir = $request->input('sub_dir', ''); //子目录
- $user_type = 2; //0=游客, 1=前台用户, 2=后台用户
- $server_type = 0; //服务器类型, 0=本地服务器,1=自由可控远程服务器,3=第三方比如七牛
- $need_origin_pic = intval($request->input('need_origin_pic', 0));
- $unlink_origin_pic = $need_origin_pic == 0 ? true : false; //是否删除原图
- $serverNo = $request->input('server_no', null); // 服务器编号.
- if ($sub_dir) {
- if (!Check::dirName($sub_dir)) {
- $result['error']['code'] = -92;
- $result['error']['message'] = '子目录名有误';
- return $result;
- }
- }
- //判断外部提交的最大尺寸是否超出系统允许的最大尺寸
- if ($max_size > $upload_default_params['max_size']) {
- $max_size = $upload_default_params['max_size'];
- } elseif (empty($max_size)) {
- $max_size = $upload_default_params['max_size'];
- }
- $water_picture = public_path('assets/sys/watermark.png'); //水印图
- $water_picture_exists = File::exists($water_picture) ? true : false;
- $arr_thumb_max_width = $arr_thumb_max_height = $arr_thumb_water = $arr_thumb_appointed = array();
- $arr_thumb_max_width = explode(',', $thumb_max_width);
- $arr_thumb_max_height = explode(',', $thumb_max_height);
- $arr_thumb_water = explode(',', $thumb_water);
- $arr_thumb_appointed = explode(',', $thumb_appointed);
- $arr_ext = self::getAllowExtByType($ext_type);
- if ($max_size == -1) {
- $format_max_size = -1;
- } else {
- $format_max_size = $max_size * 1024;
- }
- $upload_save_dir = 'attachment'; // 目前写死,以后写配置里 todo.
- $save_root_path = public_path('');
- $uploader = new UploadFileRepository(
- $format_max_size,
- $arr_ext,
- array(),
- $upload_save_dir,
- $save_root_path
- );
- $uploader->urlBase = '/';
- $uploader->thumb = $thumb_num > 0 ? true : false;
- $uploader->thumbMaxWidth = $arr_thumb_max_width;
- $uploader->thumbMaxHeight = $arr_thumb_max_height;
- $uploader->thumbAppointed = $arr_thumb_appointed;
- $uploader->thumbWater = $arr_thumb_water;
- $uploader->waterType = 'picture';
- if ($water_picture_exists) {
- $uploader->waterPicture = $water_picture;
- }
- $uploader->waterPosition = 9; //位置9
- $uploader->unlinkOriginPic = $unlink_origin_pic; //是否除原图
- $uploader->subDir = $sub_dir;
- if (!empty($serverNo)) {
- $uploader->setServerNo($serverNo);
- }
- $upload_field_name = empty($upload_field_name) ? 'upload_files' : $upload_field_name;
- if ($uploader->upload($upload_field_name)) {
- $arr_attachments = $arr_origin_pics = array();
- $arr_attachment_list = $uploader->getResultFileInfo()[0];
- if ($arr_attachment_list) {
- foreach ($arr_attachment_list as $k => $v) {
- if (isset($v['link_url']) && $v['link_url']) {
- $link_url = $v['link_url'];
- $link_origin_url = $unlink_origin_pic == true ?
- '' : $v['origin_pic_relative_path'];
- $arr_attachment_list[$k]['id'] = '';
- $arr_attachments[$k] = $arr_attachment_list[$k];
- $arr_attachments[$k]['link_url'] = get_url_by_name($link_url);
- $arr_attachments[$k]['link_origin_url'] = get_url_by_name($link_origin_url); //原图绝对地址
- unset(
- $arr_attachments[$k]['user_type'],
- $arr_attachments[$k]['user_id'],
- $arr_attachments[$k]['origin_alias'],
- $arr_attachments[$k]['module_alias'],
- $arr_attachments[$k]['related_id'],
- $arr_attachments[$k]['ip'],
- $arr_attachments[$k]['item_alias'],
- $arr_attachments[$k]['item_id'],
- $arr_attachments[$k]['origin_pic_native_path'],
- $arr_attachments[$k]['file_native_path'],
- $arr_attachments[$k]['origin_pic_name'],
- $arr_attachments[$k]['origin_pic_size'],
- $arr_attachments[$k]['origin_pic_width'],
- $arr_attachments[$k]['origin_pic_height']
- );
- }
- }
- }
- $result['content'] = $arr_attachments;
- } else {
- $result['error']['code'] = -91;
- $result['error']['message'] = $uploader->getResultError();
- }
- return $result;
- }
- public function upload()
- {
- $result = self::runUpload();
- return response()->json($result);
- }
- public function uEditorUploadResult()
- {
- $result = array('ErrorCode' => 0, 'Message' => '', 'Content' => '');
- $upload_result = self::runUpload();
- $result['ErrorCode'] = $upload_result['error']['code'];
- $result['Message'] = $upload_result['error']['message'];
- $result['Content'] = $upload_result['content'];
- return $result;
- }
- public function editorMdUploadResult()
- {
- $result = array('ErrorCode' => 0, 'Message' => '', 'Content' => '');
- $upload_result = self::runUpload();
- $result['ErrorCode'] = $upload_result['error']['code'];
- $result['Message'] = $upload_result['error']['message'];
- $result['Content'] = $upload_result['content'];
- return $result;
- }
- /**
- * 获取重设的消息结果
- * @return array
- */
- private function getResetMsgResult()
- {
- return array( 'jsonrpc' => '2.0',
- 'error' => array('code' => 0, 'message' => '', 'id' => 'id'), 'content' => '' );
- }
- /**
- * 获取允许的扩展名
- * @param int $ext_type 扩展类型
- * @return array
- */
- private function getAllowExtByType($ext_type)
- {
- $arr_exts = array(
- 'picture' => array('jpg', 'gif', 'png', 'jpeg'),
- 'compress' => array('zip', 'rar', '7z', 'tar', 'bz2', 'gz', 'apk'),
- 'office' => array('docx', 'xlsx', 'pptx', 'doc', 'xls',
- 'ppt', 'pps', 'wps', 'et', 'dps', 'xml', 'kml'), //办公
- 'flash' => array('swf', 'flv', 'as', 'fla'), //FLASH
- 'media' => array("ra","rm","rmvb","avi","mpeg","mid","wma","wav","mp3","wmv","mov", 'mp4', 'm3u8', 'lrc'), //多媒体
- );
- $return_ext = array();
- switch ($ext_type) {
- case 1:
- $return_ext = $arr_exts['picture'];
- break;
- case 2:
- $return_ext = array_merge($arr_exts['compress'], $arr_exts['office']);
- break;
- case 3:
- $return_ext = $arr_exts['flash'];
- break;
- case 4:
- $return_ext = $arr_exts['media'];
- break;
- case 5:
- $return_ext = array_merge($arr_exts['picture'], $arr_exts['flash']);
- break;
- case 6:
- $return_ext = array_merge($arr_exts['flash'], $arr_exts['media']);
- break;
- default:
- break;
- }
- return $return_ext;
- }
- }
|