HomeTemplateController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Admin\Controllers\Company;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Subsite;
  5. use App\Models\Tpl;
  6. use Encore\Admin\Controllers\HasResourceActions;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Layout\Content;
  11. use Encore\Admin\Show;
  12. use Illuminate\Support\Facades\Storage;
  13. class HomeTemplateController extends Controller
  14. {
  15. use HasResourceActions;
  16. /**
  17. * Index interface.
  18. *
  19. * @param Content $content
  20. * @return Content
  21. */
  22. public function index(Content $content)
  23. {
  24. return $content
  25. ->header('首页模板')
  26. ->description('列表')
  27. ->body($this->grid());
  28. }
  29. /**
  30. * Show interface.
  31. *
  32. * @param mixed $id
  33. * @param Content $content
  34. * @return Content
  35. */
  36. public function show($id, Content $content)
  37. {
  38. return $content
  39. ->header('首页模板')
  40. ->description('详情')
  41. ->body($this->detail($id));
  42. }
  43. /**
  44. * Edit interface.
  45. *
  46. * @param mixed $id
  47. * @param Content $content
  48. * @return Content
  49. */
  50. public function edit($id, Content $content)
  51. {
  52. return $content
  53. ->header('首页模板')
  54. ->description('编辑')
  55. ->body($this->editForm($id)->edit($id));
  56. }
  57. /**
  58. * Create interface.
  59. *
  60. * @param Content $content
  61. * @return Content
  62. */
  63. public function create(Content $content)
  64. {
  65. return $content
  66. ->header('首页模板')
  67. ->description('创建')
  68. ->body($this->createForm());
  69. }
  70. /**
  71. * Make a grid builder.
  72. *
  73. * @return Grid
  74. */
  75. protected function grid()
  76. {
  77. $grid = new Grid(new Tpl);
  78. $grid->model()->where('tpl_type', 4)->orderBy('id', 'desc');
  79. $grid->id('ID');
  80. $grid->images('模板缩略图')->image('', '150', '150');
  81. $grid->name('模板名称')->width(200);
  82. $grid->blade_name('压缩包名称');
  83. $grid->created_at('创建时间')->sortable();
  84. $grid->actions(function ($actions) use ($grid) {
  85. if (Admin::user()->can('template_home_edit')) {
  86. $actions->disableEdit(false);
  87. }
  88. if (Admin::user()->can('template_home_delete')) {
  89. $actions->disableDelete(false);
  90. }
  91. $actions->disableView(true);
  92. });
  93. if (Admin::user()->can('template_home_delete')) {
  94. $grid->tools(function ($tools) {
  95. $tools->batch(function ($batch) {
  96. $batch->disableDelete(false);
  97. });
  98. });
  99. $grid->disableRowSelector(false);
  100. }
  101. if (Admin::user()->can('template_home_create')) {
  102. $grid->disableCreateButton(false);
  103. }
  104. $grid->disableFilter();
  105. return $grid;
  106. }
  107. /**
  108. * Make a show builder.
  109. *
  110. * @param mixed $id
  111. * @return Show
  112. */
  113. protected function detail($id)
  114. {
  115. }
  116. /**
  117. * Make a form builder.
  118. *
  119. * @return Form
  120. */
  121. protected function editForm($id)
  122. {
  123. $form = new Form(new Tpl);
  124. $form->text('name', '模板名称')->rules([
  125. 'required',
  126. ])->setWidth(3)->setMustMark();
  127. $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]);
  128. $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->uniqueName()->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]);
  129. $form->saving(function (Form $form) use($id) {
  130. $form->old_blade_name = Tpl::find($id)->blade_name;
  131. });
  132. $form->saved(function (Form $form) {
  133. if($form->blade_name!=null && $form->blade_name!='_file_del_'){
  134. Storage::disk('public')->deleteDirectory('template/home/'.$form->old_blade_name);
  135. $model = $form->model();
  136. $new_blade = $model->blade_name;
  137. $zip = new \Chumper\Zipper\Zipper();
  138. $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/home/'.$form->old_blade_name));
  139. $zip->close();
  140. $model->blade_name = $form->old_blade_name;
  141. $model->save();
  142. Storage::disk('public')->delete($new_blade);
  143. }
  144. });
  145. return $form;
  146. }
  147. protected function createForm()
  148. {
  149. $form = new Form(new Tpl);
  150. $form->text('name', '模板名称')->rules([
  151. 'required',
  152. ])->setWidth(3)->setMustMark();
  153. $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->rules([
  154. 'required',
  155. ]);
  156. $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->rules('mimes:zip')->uniqueName()
  157. ->setWidth(8)->setMustMark()->rules([
  158. 'required',
  159. ]);
  160. $form->hidden('tpl_type')->default(4);
  161. $form->hidden('display')->default(1);
  162. $form->hidden('price')->default(0);
  163. $form->saved(function (Form $form) {
  164. $model = $form->model();
  165. $new_blade = $model->blade_name;
  166. $template_dir=uniqid();
  167. $zip = new \Chumper\Zipper\Zipper();
  168. $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/home/'.$template_dir));
  169. $zip->close();
  170. $model->blade_name = $template_dir;
  171. $model->save();
  172. Storage::disk('public')->delete($new_blade);
  173. });
  174. return $form;
  175. }
  176. public function store()
  177. {
  178. return $this->createForm()->store();
  179. }
  180. public function update($id)
  181. {
  182. return $this->editForm($id)->update($id);
  183. }
  184. /**
  185. * Make a form builder.
  186. *
  187. * @return Form
  188. */
  189. protected function form()
  190. {
  191. $form = new Form(new Tpl);
  192. $form->display('ID');
  193. $form->display('Created at');
  194. $form->display('Updated at');
  195. return $form;
  196. }
  197. public function destroy($id)
  198. {
  199. if (!$id) {
  200. $data = [
  201. 'status' => false,
  202. 'message' => '模板不存在!!',
  203. ];
  204. return response()->json($data);
  205. }
  206. $id_arr = explode(',', $id);
  207. \DB::beginTransaction();
  208. try {
  209. Tpl::whereIn('id', $id_arr)->delete();
  210. Subsite::whereIn('tpl', $id_arr)->update(['tpl'=>0]);
  211. $data = [
  212. 'status' => true,
  213. 'message' => '删除成功!',
  214. ];
  215. \DB::commit();
  216. return response()->json($data);
  217. } catch (\Exception $e) {
  218. \DB::rollback();
  219. $data = [
  220. 'status' => false,
  221. 'message' => '删除失败!',
  222. ];
  223. return response()->json($data);
  224. }
  225. }
  226. }