FileUploadHelper.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\DataApiNew\Helper;
  3. use App\DataApiNew\Helper\TencentCosHelper;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Validator;
  6. class FileUploadHelper
  7. {
  8. //上传文件类型对应的存储目录
  9. public $upload_file_type = [
  10. 0 => "package_main", //套餐主图
  11. 1 => "package_type_image", //套餐类型的图片
  12. 2 => "lunbo_image", //轮播图
  13. 3 => "poster", //海报
  14. 4 => "admin_logo", //后台logo
  15. 5 => "h5_logo", //h5logo
  16. 6 => "menu_logo", //菜单logo
  17. 7 => "package_cover_img", //套餐封面图
  18. 8 => "kefu_image", //客服二维码
  19. 9 => "xcx_static", //小程序静态文件
  20. 10 => "agent_kefu_image", //合伙人客服二维码
  21. 11 => "small_shop_image", //小店头像图片
  22. 12 => "contact_qrcode", //联系二维码
  23. 13 => "system_tutorial_video", //系统教程视频
  24. 14 => "id_card_image", // 实名认证身份证照片
  25. 15 => "ident_image", //运营商标识图
  26. 16 => "jkc_college_cover", //集客仓商学院封面图
  27. 17 => "jkc_college_image", //集客仓商学院内容图
  28. 18 => "small_shop_bgimg", //小店背景图
  29. 19 => "order_idcard", //订单身份证
  30. 20 => "hot_refresh", //热更新
  31. 21 => "iot_cover_img", //设备封面图
  32. 22 => "iot_image", //设备主图
  33. 23 => "saler_apk", //app安装包
  34. 24 => "iot_package_introduce", //设备套餐介绍
  35. 31 => "broad_cover_img", //宽带封面图
  36. 32 => "broad_image", //宽带主图
  37. 33 => "pretty_image", //靓号订单默认封面图
  38. 34 => "work_order_image", //工单图片
  39. 999 => "wechat_xcx", //微信小程序
  40. ];
  41. //文件上传
  42. public function uploadFiles($request)
  43. {
  44. if (!in_array($request->file('file')->getClientOriginalExtension(), ['jpeg', 'png', 'jpg', 'wgt'])) {
  45. return error('文件格式不正确');
  46. }
  47. // 验证上传文件是否合法
  48. $validate = Validator::make($request->all(), [
  49. 'file' => 'required|file|max:102400',
  50. 'type' => 'required|integer',
  51. ], [
  52. 'file.required' => '请上传文件',
  53. 'type.required' => '请选择上传类型',
  54. ]);
  55. if ($validate->fails()) {
  56. return error($validate->errors()->first());
  57. }
  58. try {
  59. // 获取上传文件保存的目录
  60. $upload_file_type = $this->upload_file_type[$request->type];
  61. // 获取上传的文件
  62. $file = $request->file('file');
  63. // 生成唯一的文件名
  64. $filename = uniqid() . '_' . time() . '.' . $file->getClientOriginalExtension();
  65. // 将文件保存到指定目录 public/cos_file/
  66. $file->storeAs($upload_file_type, $filename, 'cos_file');
  67. // 获取文件相对路径
  68. $file_path = "cos_file/" . $upload_file_type . "/" . $filename;
  69. // 上传到腾讯云cos
  70. $cos = new TencentCosHelper();
  71. $result = $cos->uploadFiles($file_path);
  72. //删除文件
  73. unlink($file_path);
  74. return $result;
  75. } catch (\Exception $e) {
  76. Log::error($e);
  77. return ['code' => 0, 'msg' => '上传失败'];
  78. }
  79. }
  80. //文件上传 压缩版
  81. public function uploadFilesCompress($request)
  82. {
  83. if (!in_array($request->file('file')->getClientOriginalExtension(), ['jpeg', 'png', 'jpg', 'wgt'])) {
  84. return error('文件格式不正确');
  85. }
  86. // 验证上传文件是否合法
  87. $validate = Validator::make($request->all(), [
  88. 'file' => 'required|file|max:102400',
  89. 'type' => 'required|integer',
  90. ], [
  91. 'file.required' => '请上传文件',
  92. 'type.required' => '请选择上传类型',
  93. ]);
  94. if ($validate->fails()) {
  95. return error($validate->errors()->first());
  96. }
  97. try {
  98. // 获取上传文件保存的目录
  99. $upload_file_type = $this->upload_file_type[$request->type];
  100. $maxFileSize = 2;
  101. if ($request->type == 19) {
  102. $maxFileSize = 1.4;
  103. }
  104. // 获取上传的文件
  105. $file = $request->file('file');
  106. // 生成唯一的文件名
  107. $filename = uniqid() . '_' . time() . '.' . $file->getClientOriginalExtension();
  108. // 获取文件相对路径
  109. $file_path = "cos_file/" . $upload_file_type . "/" . $filename;
  110. // 压缩
  111. // Log::info("上传文件:开始压缩");
  112. $yasuo = (new UtilsHelper)->compressImage($file, $file_path, 800, 600, $maxFileSize * 1024 * 1024);
  113. // Log::info("上传文件:开始压缩完成".$yasuo);
  114. if ($yasuo == 0) {
  115. // 将文件保存到指定目录 public/cos_file/
  116. $file->storeAs($upload_file_type, $filename, 'cos_file');
  117. } elseif ($yasuo == 2) {
  118. $fileMimeType = $request->file('file')->getClientMimeType();
  119. $fileSize = $request->file('file')->getSize();
  120. Log::error("压缩文件格式不正确: 类型 - {$fileMimeType}, 大小 - {$fileSize} 字节");
  121. return ['code' => 0, 'msg' => '文件格式不正确'];
  122. }
  123. // 上传到腾讯云cos
  124. // Log::info("上传文件:开始上云");
  125. $cos = new TencentCosHelper();
  126. $result = $cos->uploadFiles($file_path);
  127. Log::info("上传文件:上云完成" . json_encode($result));
  128. //删除文件
  129. unlink($file_path);
  130. return $result;
  131. } catch (\Exception $e) {
  132. Log::error($e);
  133. return ['code' => 0, 'msg' => '上传失败:' . $e->getMessage()];
  134. }
  135. }
  136. //生成二维码并上传腾讯云
  137. public function createQrcode($type, $filename, $url)
  138. {
  139. try {
  140. require 'phpqrcode/qrlib.php';
  141. $public_path = public_path();
  142. // 获取上传文件保存的目录
  143. $upload_file_type = $this->upload_file_type[$type];
  144. // 获取文件相对路径
  145. $file_path = "cos_file/" . $upload_file_type . "/" . $filename;
  146. self::ensureDirectoryExists($public_path . "/cos_file/" . $upload_file_type);
  147. // 创建二维码图片
  148. \QRcode::png($url, $public_path . "/" . $file_path, 'L', 5);
  149. // 上传到腾讯云cos
  150. $cos = new TencentCosHelper();
  151. $result = $cos->uploadFiles($file_path);
  152. //删除文件
  153. unlink($file_path);
  154. return $result;
  155. } catch (\Exception $e) {
  156. return ['code' => 0, 'msg' => '上传失败:' . $e->getMessage()];
  157. }
  158. }
  159. //把二维码和海报背景合并后的图片上传到腾讯云
  160. public function upPosterCos($type, $filename)
  161. {
  162. try {
  163. $public_path = public_path();
  164. // 获取上传文件保存的目录
  165. $upload_file_type = $this->upload_file_type[$type];
  166. // 获取文件相对路径
  167. $file_path = "cos_file/" . $upload_file_type . "/" . $filename;
  168. self::ensureDirectoryExists($public_path . "/cos_file/" . $upload_file_type);
  169. // 上传到腾讯云cos
  170. $cos = new TencentCosHelper();
  171. $result = $cos->uploadFiles($file_path);
  172. //删除文件
  173. unlink($file_path);
  174. return $result;
  175. } catch (\Exception $e) {
  176. return ['code' => 0, 'msg' => '上传失败:' . $e->getMessage()];
  177. }
  178. }
  179. //检测文件夹是否存在,不存在则创建
  180. public static function ensureDirectoryExists($directoryPath)
  181. {
  182. if (!is_dir($directoryPath)) {
  183. // 如果文件夹不存在,尝试创建它
  184. if (!mkdir($directoryPath, 0777, true)) {
  185. // 创建失败,可以根据需要进行错误处理
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. }