AllUploadController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\platform\modules\Application\controllers;
  3. use app\platform\controllers\BaseController;
  4. use app\platform\modules\system\models\SystemSetting;
  5. use app\platform\modules\application\models\CoreAttach;
  6. use app\common\services\qcloud\Api;
  7. use app\common\services\aliyunoss\OssClient;
  8. use app\common\services\aliyunoss\OSS\Core\OssException;
  9. use Illuminate\Support\Carbon;
  10. class AllUploadController extends BaseController
  11. {
  12. protected $path;
  13. protected $pattern;
  14. public function __construct()
  15. {
  16. $this->path = config('filesystems.disks.syst')['url'].'/'; //本地图片实际存放路径
  17. }
  18. public function upload()
  19. {
  20. $file = request()->file('file');
  21. if (!$file->isValid()) {
  22. return $this->errorJson('上传失败');
  23. }
  24. if ($file->getClientSize() > 30*1024*1024) {
  25. return $this->errorJson('上传图片资源过大');
  26. }
  27. //默认支持的文件格式类型
  28. $defaultImgType = [
  29. 'jpg', 'bmp', 'eps', 'gif', 'mif', 'miff', 'png', 'tif',
  30. 'tiff', 'svg', 'wmf', 'jpe', 'jpeg', 'dib', 'ico', 'tga', 'cut', 'pic'
  31. ];
  32. $defaultAudioType = ['avi', 'asf', 'wmv', 'avs', 'flv', 'mkv', 'mov', '3gp', 'mp4',
  33. 'mpg', 'mpeg', 'dat', 'ogm', 'vob', 'rm', 'rmvb', 'ts', 'tp', 'ifo', 'nsv'
  34. ];
  35. $defaultVideoType = [
  36. 'mp3', 'aac', 'wav', 'wma', 'cda', 'flac', 'm4a', 'mid', 'mka', 'mp2',
  37. 'mpa', 'mpc', 'ape', 'ofr', 'ogg', 'ra', 'wv', 'tta', 'ac3', 'dts'
  38. ];
  39. $default_file_mime_type = [
  40. 'audio/aac', 'video/x-msvideo', 'image/bmp', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'image/gif',
  41. 'image/vnd.microsoft.icon', 'image/jpeg', 'audio/midi', 'audio/x-midi', 'audio/mpeg', 'video/mpeg', 'image/png', 'application/pdf', 'application/vnd.ms-powerpoint',
  42. 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/x-rar-compressed', 'application/rtf', 'image/svg+xml', 'image/tiff',
  43. 'text/plain', 'audio/wav', 'image/webp', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/xml', 'text/xml',
  44. 'video/3gpp', 'audio/3gpp', 'video/x-ms-asf', 'video/x-ms-wmv', 'video/x-flv', 'video/quicktime', 'video/mp4', 'audio/x-wav', 'audio/x-m4a', 'audio/mid', 'audio/ogg',
  45. 'audio/x-realaudio', 'application/postscript', 'application/x-msmetafile', 'image/x-icon', 'application/vnd.ms-works', 'application/rar', 'application/zip',
  46. ];
  47. $ext = $file->getClientOriginalExtension();
  48. $originalName = $file->getClientOriginalName();
  49. $realPath = $file->getRealPath();
  50. $mime_type = $file->getMimeType();
  51. if (!in_array($mime_type, $default_file_mime_type)) {
  52. return $this->errorJson('文件类型错误上传失败');
  53. }
  54. $merge_ext = array_merge($defaultImgType, $defaultAudioType, $defaultVideoType);
  55. if (!in_array($ext, $merge_ext)) {
  56. return $this->errorJson('非规定类型的文件格式');
  57. }
  58. if (in_array($ext, $defaultImgType)) {
  59. $file_type = 'images';
  60. } elseif (in_array($ext, $defaultAudioType)) {
  61. $file_type = 'audios';
  62. } elseif (in_array($ext, $defaultVideoType)) {
  63. $file_type = 'videos';
  64. }
  65. $newFileName = $this->getNewFileName($originalName, $ext);
  66. $setting = SystemSetting::settingLoad('global', 'system_global');
  67. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  68. if (in_array($ext, $defaultImgType)) {
  69. if ($setting['image_extentions'] && !in_array($ext, array_filter($setting['image_extentions'])) ) {
  70. return $this->errorJson('非规定类型的图片格式');
  71. }
  72. $defaultImgSize = $setting['img_size'] ? $setting['img_size'] * 1024 : 1024*1024*5; //默认大小为5M
  73. if ($file->getClientSize() > $defaultImgSize) {
  74. return $this->errorJson('图片文件大小超出规定值');
  75. }
  76. }
  77. if (in_array($ext, $defaultAudioType) || in_array($ext, $defaultVideoType)) {
  78. if ($setting['audio_extentions'] && !in_array($ext, array_filter($setting['audio_extentions'])) ) {
  79. return $this->errorJson('非规定类型的文件格式');
  80. }
  81. $defaultAudioSize = $setting['audio_limit'] ? $setting['audio_limit'] * 1024 : 1024*1024*30; //音视频最大 30M
  82. if ($file->getClientSize() > $defaultAudioSize) {
  83. return $this->errorJson('文件大小超出规定值');
  84. }
  85. }
  86. $file_type = $file_type == 'images' ? 'syst_images' : $file_type;
  87. if (!\Storage::disk($file_type)->put($newFileName, file_get_contents($realPath))) {
  88. return $this->errorJson('本地上传失败');
  89. }
  90. $url = \Storage::disk($file_type)->url($newFileName);
  91. if ($remote['type'] != 0) {
  92. file_remote_upload($url, true, $remote);
  93. }
  94. $this->getData($originalName, $file_type, $url, $remote['type']);
  95. return $this->successJson('ok', ['success' => yz_tomedia($url), 'fail' => '']);
  96. }
  97. /**
  98. * 获取新文件名
  99. * @param string $originalName 原文件名
  100. * @param string $ext 文件扩展名
  101. * @return string 新文件名
  102. */
  103. public function getNewFileName($originalName, $ext)
  104. {
  105. return md5($originalName . str_random(6)) . '.' . $ext;
  106. }
  107. public function getUniacid()
  108. {
  109. return \YunShop::app()->uniacid ? : 0;
  110. }
  111. //获取本地已上传图片的列表
  112. public function getLocalList()
  113. {
  114. if (request()->year != '不限') {
  115. $search['year'] = request()->year;
  116. }
  117. if (request()->month != '不限') {
  118. $search['month'] = request()->month;
  119. }
  120. $uid = \YunShop::app()->uid;
  121. $query = CoreAttach::where(['uniacid'=>0,'type'=>1])->orderby('id', 'desc');
  122. if ($uid && $uid != 1) {
  123. $query->where('uid', $uid);
  124. }
  125. if ($search['year'] || $search['month']) {
  126. $start_time = Carbon::createFromDate($search['year'], $search['month'])->startOfMonth()->timestamp;
  127. $end_time = Carbon::createFromDate($search['year'], $search['month'])->endOfMonth()->timestamp;
  128. $query->whereBetween('created_at', [$start_time, $end_time]);
  129. }
  130. $list = $query->paginate()->toArray();
  131. foreach ($list['data'] as $k => $v) {
  132. if ($v['attachment'] && $v['id']) {
  133. $data['data'][$k]['id'] = $v['id'];
  134. $data['data'][$k]['url'] = yz_tomedia($v['attachment']);
  135. }
  136. }
  137. $data['total'] = $list['total'];
  138. $data['per_page'] = $list['per_page'];
  139. $data['last_page'] = $list['last_page'];
  140. $data['prev_page_url'] = $list['prev_page_url'];
  141. $data['next_page_url'] = $list['next_page_url'];
  142. $data['current_page'] = $list['current_page'];
  143. $data['from'] = $list['from'];
  144. $data['to'] = $list['to'];
  145. if (!$data['data']) {
  146. $data['data'] = [];
  147. }
  148. return $this->successJson('获取成功', $data);
  149. }
  150. public function delLocalImg()
  151. {
  152. $id = request()->id;
  153. $core = CoreAttach::find($id);
  154. if (!$core) {
  155. return $this->errorJson('请重新选择');
  156. }
  157. $setting = SystemSetting::settingLoad('remote', 'system_remote');
  158. if ($core['upload_type']) {
  159. $remote_url = '';
  160. if ($setting['type'] == 2) {
  161. $remote_url = $setting['alioss']['url'];
  162. }
  163. if ($setting['type'] == 4) {
  164. $remote_url = $setting['cos']['url'];
  165. }
  166. if ($remote_url && strexists($core['attachment'], $remote_url)) {
  167. $str_len = strlen($remote_url);
  168. $core['attachment'] = substr($core['attachment'], $str_len+1);
  169. }
  170. $status = file_remote_delete($core['attachment'], $core['upload_type'], $setting);
  171. } else {
  172. $status = file_delete($core['attachment']);
  173. }
  174. if ($core->delete()) {
  175. return $this->successJson('删除成功');
  176. }
  177. return $this->errorJson('删除失败');
  178. }
  179. //上传记录表
  180. public function getData($originalName, $file_type, $newFileName, $save_type)
  181. {
  182. //存储至数据表中
  183. $core = new CoreAttach;
  184. switch ($file_type) {
  185. case 'syst_images':
  186. $type = 1;
  187. break;
  188. case 'audios':
  189. $type = 2;
  190. break;
  191. default:
  192. $type = 3;
  193. break;
  194. }
  195. $d = [
  196. 'uniacid' => $this->getUniacid(),
  197. 'uid' => \Auth::guard('admin')->user()->uid,
  198. 'filename' => $originalName,
  199. 'type' => $type, //类型1.图片; 2.音乐
  200. 'attachment' => $newFileName,
  201. 'upload_type' => $save_type
  202. ];
  203. $core->fill($d);
  204. $validate = $core->validator();
  205. if (!$validate->fails()) {
  206. if ($core->save()) {
  207. return 1;
  208. }
  209. }
  210. return $validate->messages();
  211. }
  212. }