HomeController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace App\Http\Controllers;
  3. /*
  4. * 用户认证控制器
  5. */
  6. use App\Models\SiteAlbum;
  7. use App\Models\SiteAlbumFolder;
  8. use App\Models\SiteAlbumLog;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Validator;
  12. use ZipArchive;
  13. use Illuminate\Support\Facades\Session;
  14. use Illuminate\Support\Str;
  15. class HomeController extends Controller
  16. {
  17. /*
  18. * 首页
  19. */
  20. public function index(Request $request)
  21. {
  22. return view('index',['foldersTree' => $this->foldersTree]);
  23. }
  24. public function main(Request $request)
  25. {
  26. return view('main',['foldersTree' => $this->foldersTree]);
  27. }
  28. /*
  29. * 分类列表
  30. */
  31. public function gallery(Request $request)
  32. {
  33. $search = $request->input('search', '');
  34. if ($search) {
  35. $search = trim($search);
  36. $searchLike = '%' . $search . '%';
  37. $albums = SiteAlbum::where('model', 'like', $searchLike)->orderBy('id', 'desc')->get();
  38. $title = '搜索'.$search.'结果:';
  39. $folder_id = 0;
  40. $father_id = 0;
  41. if ($albums) {
  42. $albums = $albums->toArray();
  43. }
  44. } else {
  45. $folder_id = $request->input('fid',$this->getFoldersTreeFirstId());
  46. $father_id = $this->getFoldersTreeFatherId($folder_id);
  47. $folder = SiteAlbumFolder::find($folder_id);
  48. $albums = SiteAlbum::where('folder_id', $folder_id)->orderBy('id', 'desc')->get();
  49. $albums = $albums->toArray();
  50. $title = $folder->title;
  51. }
  52. foreach ($albums as $key => $album) {
  53. $cover = json_decode($album['cover']);
  54. foreach ($cover as $k => $v) {
  55. $cover[$k] = ossUrl($v);
  56. }
  57. $cover = empty($cover) ? ['/static/images/noimg.jpg'] : $cover;
  58. $albums[$key]['cover'] = $cover;
  59. }
  60. // 面包屑导航
  61. $breadcrumb = getBreadcrumb($folder_id, $this->foldersTree);
  62. if ($breadcrumb) {
  63. $breadcrumb = implode('<span>/</span>', $breadcrumb) . '<span>/</span>';
  64. } else {
  65. $breadcrumb = '';
  66. }
  67. return view('gallery',
  68. [
  69. 'foldersTree' => $this->foldersTree,
  70. 'folderName'=> $title,
  71. 'folder_id' => $folder_id,
  72. 'father_id' => $father_id,
  73. 'albums' => $albums,
  74. 'breadcrumb' => $breadcrumb,
  75. ]
  76. );
  77. }
  78. public function detail(Request $request) {
  79. $id = $request->input('id',0);
  80. $album = SiteAlbum::find($id);
  81. $bumFolder = SiteAlbumFolder::where('id', $album->folder_id)->first();
  82. $showTabs = [];
  83. if ($bumFolder) {
  84. $row = json_decode($bumFolder->show_tabs);
  85. $row[7] = '7';
  86. foreach ($row as $key => $value) {
  87. $column = '';
  88. $title = '';
  89. switch ($value) {
  90. case '0':
  91. $column = 'cover';
  92. $title = '主图';
  93. break;
  94. case '1':
  95. $column = 'en_detail';
  96. $title = '英文详情';
  97. break;
  98. case '2':
  99. $column = 'cn_detail';
  100. $title = '中文详情';
  101. break;
  102. case '3':
  103. $column = 'video';
  104. $title = '视频';
  105. break;
  106. case '4':
  107. $column = 'poster';
  108. $title = '海报';
  109. break;
  110. case '5':
  111. $column = 'cert';
  112. $title = '证书';
  113. break;
  114. case '6':
  115. $column = 'pdf';
  116. $title = 'PDF';
  117. break;
  118. case '7':
  119. $column = 'parameters';
  120. $title = '属性';
  121. break;
  122. }
  123. $content = json_decode($album[$column], true);
  124. if (empty($content) == false) {
  125. $showTabs[$key] = ['value'=>$value,'column'=>$column, 'title'=>$title];
  126. }
  127. }
  128. }
  129. // dd(json_decode($album['parameters'], true));
  130. return view('gallery-detail',[
  131. 'foldersTree' => $this->foldersTree,
  132. 'album' => $album->toArray(),
  133. 'showTabs' => $showTabs
  134. ]);
  135. }
  136. public function updateLog(Request $request)
  137. {
  138. $losgs = SiteAlbumLog::getFormattedLogs();
  139. return view('update_log',[
  140. 'foldersTree' => $this->foldersTree,
  141. 'logs' => $losgs
  142. ]);
  143. }
  144. /*
  145. * 下载全部
  146. */
  147. public function downloadAll(Request $request)
  148. {
  149. Session::put('downloadAllStatus', '1');
  150. $id = $request->input('id', 0);
  151. $tab = $request->input('tab', 'cover');
  152. $album = SiteAlbum::find($id);
  153. if ($album) {
  154. $album = $album->toArray();
  155. if (isset($album[$tab])) {
  156. $files = json_decode($album[$tab]);
  157. $fileUrls = [];
  158. // 获取所有图片的 URL
  159. foreach ($files as $key => $value) {
  160. $fileUrl = ossUrl($value); // 获取完整的图片 URL
  161. $fileUrls[] = $fileUrl; // 将图片 URL 存入数组
  162. }
  163. // 创建一个临时 ZIP 文件
  164. $zipFileName = 'album_' . $id . '_' . $tab . '.zip';
  165. $zipPath = storage_path('app/' . $zipFileName);
  166. $zip = new ZipArchive;
  167. if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE) {
  168. foreach ($fileUrls as $fileUrl) {
  169. $fileContent = file_get_contents($fileUrl); // 下载图片内容
  170. $fileName = basename($fileUrl); // 获取文件名
  171. $zip->addFromString($fileName, $fileContent); // 将图片添加到 ZIP 文件中
  172. }
  173. $zip->close();
  174. // 提供 ZIP 文件下载
  175. Session::put('downloadAllStatus', '2');
  176. return response()->download($zipPath, $zipFileName)->deleteFileAfterSend(true);
  177. } else {
  178. Session::put('downloadAllStatus', '2');
  179. return response()->json(['error' => '系统正在下载中,请稍后再试','status'=> 500]);
  180. }
  181. } else {
  182. Session::put('downloadAllStatus', '2');
  183. return response()->json(['error' => '未找到指定的 Tab 数据','status'=> 404]);
  184. }
  185. } else {
  186. Session::put('downloadAllStatus', '2');
  187. return response()->json(['error' => '未找到相册','status'=> 404]);
  188. }
  189. }
  190. public function downloadImage(Request $request)
  191. {
  192. Session::put('downloadAllStatus', '1');
  193. // Get the URL from the request parameter
  194. $imageUrl = $request->input('url');
  195. try {
  196. // Initialize cURL
  197. $ch = curl_init($imageUrl);
  198. // Set cURL options
  199. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  200. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Allow HTTPS (disable SSL peer verification)
  201. curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Set timeout to prevent hanging
  202. // Execute cURL request
  203. $imageContent = curl_exec($ch);
  204. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  205. if ($httpCode !== 200) {
  206. Session::put('downloadAllStatus', '2');
  207. return response()->json(['error' => 'Failed to download image'], 500);
  208. }
  209. curl_close($ch);
  210. // Generate a random filename
  211. $fileName = Str::random(32) . '.jpg';
  212. Session::put('downloadAllStatus', '2');
  213. // Return the image as a download
  214. return response($imageContent)
  215. ->header('Content-Type', 'image/jpeg')
  216. ->header('Content-Disposition', 'attachment; filename="' . $fileName . '"');
  217. } catch (\Exception $e) {
  218. Session::put('downloadAllStatus', '2');
  219. return response()->json(['error' => 'Error downloading image: ' . $e->getMessage()], 500);
  220. }
  221. }
  222. public function getDownloadAllStatus(Request $request)
  223. {
  224. $reset = $request->input('reset', 0);
  225. if ($reset) {
  226. Session::put('downloadAllStatus', '0');
  227. }
  228. $status = Session::get('downloadAllStatus',0);
  229. return response()->json(['status' => $status]);
  230. }
  231. /*
  232. * 更新日志
  233. */
  234. public function nextLog(Request $request)
  235. {
  236. $id = $request->input('id', 0);
  237. $log = SiteAlbumLog::getNextLogEntry($id);
  238. if ($log) {
  239. return response()->json(['status' => 'success', 'data' => $log]);
  240. } else {
  241. return response()->json(['status' => 'error', 'data' => []]);
  242. }
  243. }
  244. //获取分类树父ID
  245. private function getFoldersTreeFatherId($folder_id)
  246. {
  247. foreach ($this->foldersTree as $key => $value) {
  248. $father_id = $value['id'];
  249. if ($folder_id == $value['id']) {
  250. return $father_id;
  251. }
  252. foreach ($value['children'] as $k => $v) {
  253. if ($folder_id == $v['id']) {
  254. return $father_id;
  255. }
  256. foreach ($v['children'] as $kk => $vv) {
  257. if ($folder_id == $vv['id']) {
  258. return $father_id;
  259. }
  260. }
  261. }
  262. }
  263. return 0;
  264. }
  265. //返回分类树第一个无子节点的ID
  266. private function getFoldersTreeFirstId()
  267. {
  268. foreach ($this->foldersTree as $key => $value) {
  269. if (isset($value['children'])) {
  270. foreach ($value['children'] as $k => $v) {
  271. return $v['id'];
  272. }
  273. } else {
  274. return $value['id'];
  275. }
  276. }
  277. return 0;
  278. }
  279. }