123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\DataApiNew\Helper;
- use App\DataApiNew\Helper\TencentCosHelper;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- class FileUploadHelper
- {
- //上传文件类型对应的存储目录
- public $upload_file_type = [
- 0 => "package_main", //套餐主图
- 1 => "package_type_image", //套餐类型的图片
- 2 => "lunbo_image", //轮播图
- 3 => "poster", //海报
- 4 => "admin_logo", //后台logo
- 5 => "h5_logo", //h5logo
- 6 => "menu_logo", //菜单logo
- 7 => "package_cover_img", //套餐封面图
- 8 => "kefu_image", //客服二维码
- 9 => "xcx_static", //小程序静态文件
- 10 => "agent_kefu_image", //合伙人客服二维码
- 11 => "small_shop_image", //小店头像图片
- 12 => "contact_qrcode", //联系二维码
- 13 => "system_tutorial_video", //系统教程视频
- 14 => "id_card_image", // 实名认证身份证照片
- 15 => "ident_image", //运营商标识图
- 16 => "jkc_college_cover", //集客仓商学院封面图
- 17 => "jkc_college_image", //集客仓商学院内容图
- 18 => "small_shop_bgimg", //小店背景图
- 19 => "order_idcard", //订单身份证
- 20 => "hot_refresh", //热更新
- 21 => "iot_cover_img", //设备封面图
- 22 => "iot_image", //设备主图
- 23 => "saler_apk", //app安装包
- 24 => "iot_package_introduce", //设备套餐介绍
- 31 => "broad_cover_img", //宽带封面图
- 32 => "broad_image", //宽带主图
- 33 => "pretty_image", //靓号订单默认封面图
- 34 => "work_order_image", //工单图片
- 999 => "wechat_xcx", //微信小程序
- ];
- //文件上传
- public function uploadFiles($request)
- {
- if (!in_array($request->file('file')->getClientOriginalExtension(), ['jpeg', 'png', 'jpg', 'wgt'])) {
- return error('文件格式不正确');
- }
- // 验证上传文件是否合法
- $validate = Validator::make($request->all(), [
- 'file' => 'required|file|max:102400',
- 'type' => 'required|integer',
- ], [
- 'file.required' => '请上传文件',
- 'type.required' => '请选择上传类型',
- ]);
- if ($validate->fails()) {
- return error($validate->errors()->first());
- }
- try {
- // 获取上传文件保存的目录
- $upload_file_type = $this->upload_file_type[$request->type];
- // 获取上传的文件
- $file = $request->file('file');
- // 生成唯一的文件名
- $filename = uniqid() . '_' . time() . '.' . $file->getClientOriginalExtension();
- // 将文件保存到指定目录 public/cos_file/
- $file->storeAs($upload_file_type, $filename, 'cos_file');
- // 获取文件相对路径
- $file_path = "cos_file/" . $upload_file_type . "/" . $filename;
- // 上传到腾讯云cos
- $cos = new TencentCosHelper();
- $result = $cos->uploadFiles($file_path);
- //删除文件
- unlink($file_path);
- return $result;
- } catch (\Exception $e) {
- Log::error($e);
- return ['code' => 0, 'msg' => '上传失败'];
- }
- }
- //文件上传 压缩版
- public function uploadFilesCompress($request)
- {
- if (!in_array($request->file('file')->getClientOriginalExtension(), ['jpeg', 'png', 'jpg', 'wgt'])) {
- return error('文件格式不正确');
- }
- // 验证上传文件是否合法
- $validate = Validator::make($request->all(), [
- 'file' => 'required|file|max:102400',
- 'type' => 'required|integer',
- ], [
- 'file.required' => '请上传文件',
- 'type.required' => '请选择上传类型',
- ]);
- if ($validate->fails()) {
- return error($validate->errors()->first());
- }
- try {
- // 获取上传文件保存的目录
- $upload_file_type = $this->upload_file_type[$request->type];
- $maxFileSize = 2;
- if ($request->type == 19) {
- $maxFileSize = 1.4;
- }
- // 获取上传的文件
- $file = $request->file('file');
- // 生成唯一的文件名
- $filename = uniqid() . '_' . time() . '.' . $file->getClientOriginalExtension();
- // 获取文件相对路径
- $file_path = "cos_file/" . $upload_file_type . "/" . $filename;
- // 压缩
- // Log::info("上传文件:开始压缩");
- $yasuo = (new UtilsHelper)->compressImage($file, $file_path, 800, 600, $maxFileSize * 1024 * 1024);
- // Log::info("上传文件:开始压缩完成".$yasuo);
- if ($yasuo == 0) {
- // 将文件保存到指定目录 public/cos_file/
- $file->storeAs($upload_file_type, $filename, 'cos_file');
- } elseif ($yasuo == 2) {
- $fileMimeType = $request->file('file')->getClientMimeType();
- $fileSize = $request->file('file')->getSize();
- Log::error("压缩文件格式不正确: 类型 - {$fileMimeType}, 大小 - {$fileSize} 字节");
- return ['code' => 0, 'msg' => '文件格式不正确'];
- }
- // 上传到腾讯云cos
- // Log::info("上传文件:开始上云");
- $cos = new TencentCosHelper();
- $result = $cos->uploadFiles($file_path);
- Log::info("上传文件:上云完成" . json_encode($result));
- //删除文件
- unlink($file_path);
- return $result;
- } catch (\Exception $e) {
- Log::error($e);
- return ['code' => 0, 'msg' => '上传失败:' . $e->getMessage()];
- }
- }
- //生成二维码并上传腾讯云
- public function createQrcode($type, $filename, $url)
- {
- try {
- require 'phpqrcode/qrlib.php';
- $public_path = public_path();
- // 获取上传文件保存的目录
- $upload_file_type = $this->upload_file_type[$type];
- // 获取文件相对路径
- $file_path = "cos_file/" . $upload_file_type . "/" . $filename;
- self::ensureDirectoryExists($public_path . "/cos_file/" . $upload_file_type);
- // 创建二维码图片
- \QRcode::png($url, $public_path . "/" . $file_path, 'L', 5);
- // 上传到腾讯云cos
- $cos = new TencentCosHelper();
- $result = $cos->uploadFiles($file_path);
- //删除文件
- unlink($file_path);
- return $result;
- } catch (\Exception $e) {
- return ['code' => 0, 'msg' => '上传失败:' . $e->getMessage()];
- }
- }
- //把二维码和海报背景合并后的图片上传到腾讯云
- public function upPosterCos($type, $filename)
- {
- try {
- $public_path = public_path();
- // 获取上传文件保存的目录
- $upload_file_type = $this->upload_file_type[$type];
- // 获取文件相对路径
- $file_path = "cos_file/" . $upload_file_type . "/" . $filename;
- self::ensureDirectoryExists($public_path . "/cos_file/" . $upload_file_type);
- // 上传到腾讯云cos
- $cos = new TencentCosHelper();
- $result = $cos->uploadFiles($file_path);
- //删除文件
- unlink($file_path);
- return $result;
- } catch (\Exception $e) {
- return ['code' => 0, 'msg' => '上传失败:' . $e->getMessage()];
- }
- }
- //检测文件夹是否存在,不存在则创建
- public static function ensureDirectoryExists($directoryPath)
- {
- if (!is_dir($directoryPath)) {
- // 如果文件夹不存在,尝试创建它
- if (!mkdir($directoryPath, 0777, true)) {
- // 创建失败,可以根据需要进行错误处理
- return false;
- }
- }
- return true;
- }
- }
|