SiteAlbumController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\SiteAlbum;
  4. use App\Admin\Repositories\SiteAlbumFolder;
  5. use App\Libraries\CommonHelper;
  6. use App\Models\SiteAlbumFolder as SiteAlbumFolderModel;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Dcat\Admin\Layout\Content;
  12. use Dcat\Admin\Admin;
  13. use App\Admin\Repositories\NullRepository;
  14. use Dcat\Admin\Traits\HasUploadedFile;
  15. use function Symfony\Component\Translation\t;
  16. class SiteAlbumController extends AdminController
  17. {
  18. use HasUploadedFile;
  19. /**
  20. * page index
  21. */
  22. public function index(Content $content)
  23. {
  24. return $content
  25. ->header(admin_trans( 'admin.album'))
  26. ->body($this->indexForm());
  27. }
  28. protected function indexForm()
  29. {
  30. return Form::make(new NullRepository(), function (Form $form) {
  31. $form->block(2, function (Form\BlockForm $form) {
  32. $type = [
  33. 'default' => [
  34. 'icon' => true,
  35. ],
  36. ];
  37. $plugins = ['types'];
  38. $menuModel = new SiteAlbumFolderModel();
  39. $form->tree()
  40. ->setTitleColumn('title')
  41. ->nodes($menuModel->allNodes())
  42. ->type($type)
  43. ->plugins($plugins)
  44. ->width(12,0);
  45. });
  46. $form->block(10, function (Form\BlockForm $form) {
  47. $form->html($this->grid())->width(12);
  48. });
  49. });
  50. }
  51. protected function grid()
  52. {
  53. return Grid::make(new SiteAlbum(), function (Grid $grid) {
  54. //默认分页条数
  55. $grid->paginate(config('admin.per_page'));
  56. $grid->column('id')->sortable();
  57. $grid->column('title');
  58. $grid->column('model');
  59. $grid->column('order')->sortable();
  60. $grid->column('enabled')->switch();
  61. $grid->column('created_at')->sortable();
  62. $grid->quickSearch(['title','model']);
  63. $grid->disableViewButton();
  64. //$grid->showQuickEditButton();
  65. $grid->disablePerPages();
  66. $grid->disableRefreshButton();
  67. $grid->model()->orderBy('order', 'asc')->orderBy('id', 'desc');
  68. //弹窗大小
  69. $grid->setDialogFormDimensions('830px','670px');
  70. });
  71. }
  72. protected function form()
  73. {
  74. $thisObj = $this;
  75. return Form::make(new SiteAlbum(), function (Form $form) use ($thisObj) {
  76. $form->width(9, 1);
  77. $form->disableViewButton();
  78. $form->disableViewCheck();
  79. $form->saving(function (Form $form) use ($thisObj) {
  80. //处理video
  81. $videos = $form->input('video');
  82. if ($videos) {
  83. foreach ($videos as $key => $value) {
  84. if (empty($value['cover']) && $value['_remove_'] != 1) {
  85. //自动生成封面
  86. $result = $thisObj->autoGenerateCover($value['video_src']);
  87. if ($result['status']) {
  88. $videos[$key]['cover'] = $result['path'];
  89. } else {
  90. return $form->response()->error($result['msg']);
  91. }
  92. }
  93. }
  94. } else {
  95. $videos = [];
  96. }
  97. $form->input('video', $videos);
  98. //处理pdf
  99. $pdfs = $form->input('pdf');
  100. $pdfs = empty($pdfs) ? [] : $pdfs;
  101. $form->input('pdf', $pdfs);
  102. });
  103. $form->tab(admin_trans_label('basic_info'), function (Form $form) {
  104. $form->select('folder_id')->options(SiteAlbumFolder::selectOptions())->required();
  105. $form->text('title')->required();
  106. $form->text('model');
  107. })->tab(admin_trans_label('cover'), function (Form $form) {
  108. $form->multipleImage('cover')
  109. ->retainable()//禁止删OSS图
  110. ->sortable() // 可拖动排序
  111. ->removable() // 可移除图片
  112. ->autoUpload() // 自动上传
  113. ->uniqueName()
  114. ->limit(config('admin.upload.oss_image.limit'))
  115. ->accept(config('admin.upload.oss_image.accept'))
  116. ->maxSize(config('admin.upload.oss_image.max_size'))
  117. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  118. ->saving(function ($images) use ($form) {
  119. return json_encode($images);
  120. });
  121. })->tab(admin_trans_label('en_detail'), function (Form $form) {
  122. $form->multipleImage('en_detail')
  123. ->retainable()//禁止删OSS图
  124. ->sortable() // 可拖动排序
  125. ->removable() // 可移除图片
  126. ->autoUpload() // 自动上传
  127. ->uniqueName()
  128. ->limit(config('admin.upload.oss_image.limit'))
  129. ->accept(config('admin.upload.oss_image.accept'))
  130. ->maxSize(config('admin.upload.oss_image.max_size'))
  131. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  132. ->saving(function ($images) use ($form) {
  133. return json_encode($images);
  134. });
  135. })->tab(admin_trans_label('cn_detail'), function (Form $form) {
  136. $form->multipleImage('cn_detail')
  137. ->retainable()//禁止删OSS图
  138. ->sortable() // 可拖动排序
  139. ->removable() // 可移除图片
  140. ->autoUpload() // 自动上传
  141. ->uniqueName()
  142. ->limit(config('admin.upload.oss_image.limit'))
  143. ->accept(config('admin.upload.oss_image.accept'))
  144. ->maxSize(config('admin.upload.oss_image.max_size'))
  145. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  146. ->saving(function ($images) use ($form) {
  147. return json_encode($images);
  148. });
  149. })->tab(admin_trans_label('video'), function (Form $form) {
  150. $count = 0;
  151. $form->hasMany('video', function (Form\NestedForm $form) use (&$count) {
  152. $videos = $form->model()->video;
  153. $imgArray = "";
  154. if ($videos) {
  155. $videos = json_decode($videos,true);
  156. foreach ($videos as $key => $value) {
  157. if ($value['cover'] && $key == $count-1) {
  158. $imgArray = [$value['cover']];
  159. }
  160. }
  161. }
  162. $imgHtml = CommonHelper::displayImage($imgArray);
  163. $form->html($imgHtml,admin_trans_label('image_preview'));
  164. $count++;
  165. $form->text('cover',admin_trans_label('video_cover'))->placeholder('为空则自动生成');
  166. $form->tradFile('video_src')
  167. ->retainable()//禁止删OSS图
  168. ->removable() // 可移除图片
  169. ->autoUpload() // 自动上传
  170. ->uniqueName()
  171. ->accept(config('admin.upload.oss_video.accept'))
  172. ->maxSize(config('admin.upload.oss_video.max_size'))
  173. ->dir(config("admin.upload.directory.video").'/uploads/'.date("Ymd"))
  174. ->chunkSize(1024)
  175. ->required();
  176. })->useTable()
  177. ->customFormat(function ($data) {return json_decode($data,true);})
  178. ->setView('admin.form_custom.hasmanytable')
  179. ->saving(function ($input) {
  180. $data = [];
  181. foreach ($input as $value) {
  182. if ($value['_remove_'] != 1){
  183. $data[] = ['cover'=>$value['cover'],'video_src'=>$value['video_src']];
  184. }
  185. }
  186. return json_encode($data);
  187. });
  188. })->tab(admin_trans_label('poster'), function (Form $form) {
  189. $form->multipleImage('poster')
  190. ->retainable()//禁止删OSS图
  191. ->sortable() // 可拖动排序
  192. ->removable() // 可移除图片
  193. ->autoUpload() // 自动上传
  194. ->uniqueName()
  195. ->limit(config('admin.upload.oss_image.limit'))
  196. ->accept(config('admin.upload.oss_image.accept'))
  197. ->maxSize(config('admin.upload.oss_image.max_size'))
  198. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  199. ->saving(function ($images) use ($form) {
  200. return json_encode($images);
  201. });
  202. })->tab(admin_trans_label('cert'), function (Form $form) {
  203. $form->multipleImage('cert')
  204. ->retainable()//禁止删OSS图
  205. ->sortable() // 可拖动排序
  206. ->removable() // 可移除图片
  207. ->autoUpload() // 自动上传
  208. ->uniqueName()
  209. ->limit(config('admin.upload.oss_image.limit'))
  210. ->accept(config('admin.upload.oss_image.accept'))
  211. ->maxSize(config('admin.upload.oss_image.max_size'))
  212. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  213. ->saving(function ($images) use ($form) {
  214. return json_encode($images);
  215. });
  216. })->tab(admin_trans_label('pdf'), function (Form $form) {
  217. $form->hasMany('pdf', function ($form) {
  218. $form->text('pdf_title')->required();
  219. $form->tradFile('pdf_src')
  220. ->retainable()//禁止删OSS图
  221. ->removable() // 可移除图片
  222. ->autoUpload() // 自动上传
  223. ->uniqueName()
  224. ->accept(config('admin.upload.oss_pdf.accept'))
  225. ->maxSize(config('admin.upload.oss_pdf.max_size'))
  226. ->dir(config("admin.upload.directory.pdf").'/uploads/'.date("Ymd"))
  227. ->chunkSize(1024)
  228. ->required();
  229. })->useTable()
  230. ->customFormat(function ($data) {
  231. return json_decode($data,true);
  232. })
  233. ->setView('admin.form_custom.hasmanytable')
  234. ->saving(function ($input) {
  235. $data = [];
  236. foreach ($input as $value) {
  237. if ($value['_remove_'] != 1){
  238. $data[] = ['pdf_title'=>$value['pdf_title'],'pdf_src'=>$value['pdf_src']];
  239. }
  240. }
  241. return json_encode($data);
  242. });
  243. });
  244. });
  245. }
  246. /*
  247. * 自动生成视频封面
  248. */
  249. private function autoGenerateCover($videoSrc)
  250. {
  251. $cover = $videoSrc.'?x-oss-process=video/snapshot,t_2000,f_jpg,h_500,m_fast';
  252. //TODO 上传到OSS
  253. $cover = CommonHelper::ossUrl($cover);
  254. $path = $this->upload($cover,'.jpg');
  255. return $path;
  256. }
  257. private function upload($file,$imgType='.jpg')
  258. {
  259. $disk = $this->disk('oss');
  260. $newName = uniqueCode("video_cover_").$imgType;
  261. $dir = config("admin.upload.directory.image").'/uploads/'.date("Ymd").'/'.$newName;
  262. $contents = file_get_contents($file);
  263. if (!$contents) {
  264. return ['status'=>false,'msg'=>'图片上传失败,请检查PHP配置'];
  265. }
  266. $disk->put($dir, $contents);
  267. return ['status'=>true,'path'=>$dir];
  268. }
  269. }