HomeController.php 10 KB

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