CompanyTemplateController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace App\Admin\Controllers\Company;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Company;
  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 CompanyTemplateController 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', 1)->orderBy('id', 'desc');
  79. $grid->id('ID');
  80. $grid->images('模板缩略图')->image('', '150', '150');
  81. $grid->name('模板名称')->width(200);
  82. $states = [
  83. 'on' => ['value' => 1, 'text' => '是', 'color' => 'primary'],
  84. 'off' => ['value' => 0, 'text' => '否', 'color' => 'default'],
  85. ];
  86. $grid->display('是否显示')->switch($states);
  87. $grid->price('价格')->display(function ($display) {
  88. return $display.'积分';
  89. });
  90. $grid->blade_name('压缩包名称');
  91. $grid->created_at('创建时间')->sortable();
  92. $grid->actions(function ($actions) use ($grid) {
  93. if (Admin::user()->can('template_company_edit')) {
  94. $actions->disableEdit(false);
  95. }
  96. if (Admin::user()->can('template_company_delete')) {
  97. $actions->disableDelete(false);
  98. }
  99. $actions->disableView(true);
  100. });
  101. if (Admin::user()->can('template_company_delete')) {
  102. $grid->tools(function ($tools) {
  103. $tools->batch(function ($batch) {
  104. $batch->disableDelete(false);
  105. });
  106. });
  107. $grid->disableRowSelector(false);
  108. }
  109. if (Admin::user()->can('template_company_create')) {
  110. $grid->disableCreateButton(false);
  111. }
  112. $grid->disableFilter();
  113. return $grid;
  114. }
  115. /**
  116. * Make a show builder.
  117. *
  118. * @param mixed $id
  119. * @return Show
  120. */
  121. protected function detail($id)
  122. {
  123. }
  124. /**
  125. * Make a form builder.
  126. *
  127. * @return Form
  128. */
  129. protected function editForm($id)
  130. {
  131. $form = new Form(new Tpl);
  132. $form->text('name', '模板名称')->rules([
  133. 'required',
  134. ])->setWidth(3)->setMustMark();
  135. $form->radio('display', '是否显示')->options([0=>'否',1=>'是'])->default(1);
  136. $form->number('price','价格')->default(0)->min(0)->help('企业会员使用此模板需要支付的积分,0为免费');
  137. $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]);
  138. $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->uniqueName()->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]);
  139. $form->saving(function (Form $form) use($id) {
  140. $form->old_blade_name = Tpl::find($id)->blade_name;
  141. if ($form->display=='on') {
  142. $form->display=1;
  143. } elseif ($form->display=='off') {
  144. $form->display=0;
  145. }
  146. });
  147. $form->saved(function (Form $form) {
  148. if($form->blade_name!=null && $form->blade_name!='_file_del_'){
  149. Storage::disk('public')->deleteDirectory('template/company/'.$form->old_blade_name);
  150. $model = $form->model();
  151. $new_blade = $model->blade_name;
  152. $zip = new \Chumper\Zipper\Zipper();
  153. $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/company/'.$form->old_blade_name));
  154. $zip->close();
  155. $model->blade_name = $form->old_blade_name;
  156. $model->save();
  157. Storage::disk('public')->delete($new_blade);
  158. }
  159. });
  160. return $form;
  161. }
  162. protected function createForm()
  163. {
  164. $form = new Form(new Tpl);
  165. $form->text('name', '模板名称')->rules([
  166. 'required',
  167. ])->setWidth(3)->setMustMark();
  168. $form->radio('display', '是否显示')->options([0=>'否',1=>'是'])->default(1);
  169. $form->number('price','价格')->default(0)->min(0)->help('企业会员使用此模板需要支付的积分,0为免费');
  170. $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->rules([
  171. 'required',
  172. ]);
  173. $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->rules('mimes:zip')->uniqueName()
  174. ->setWidth(8)->setMustMark()->rules([
  175. 'required',
  176. ]);
  177. $form->saved(function (Form $form) {
  178. $model = $form->model();
  179. $new_blade = $model->blade_name;
  180. $template_dir=uniqid();
  181. $zip = new \Chumper\Zipper\Zipper();
  182. $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/company/'.$template_dir));
  183. $zip->close();
  184. $model->blade_name = $template_dir;
  185. $model->save();
  186. Storage::disk('public')->delete($new_blade);
  187. });
  188. return $form;
  189. }
  190. public function store()
  191. {
  192. return $this->createForm()->store();
  193. }
  194. public function update($id)
  195. {
  196. return $this->editForm($id)->update($id);
  197. }
  198. /**
  199. * Make a form builder.
  200. *
  201. * @return Form
  202. */
  203. protected function form()
  204. {
  205. $form = new Form(new Tpl);
  206. $form->display('ID');
  207. $form->display('Created at');
  208. $form->display('Updated at');
  209. return $form;
  210. }
  211. public function destroy($id)
  212. {
  213. if (!$id) {
  214. $data = [
  215. 'status' => false,
  216. 'message' => '模板不存在!!',
  217. ];
  218. return response()->json($data);
  219. }
  220. $id_arr = explode(',', $id);
  221. $blade_name = Tpl::whereIn('id', $id_arr)->pluck('blade_name')->toArray();
  222. \DB::beginTransaction();
  223. try {
  224. Tpl::whereIn('id', $id_arr)->delete();
  225. if ($blade_name){
  226. Company::whereIn('tpl', $blade_name)->update(['tpl'=>'default']);
  227. }
  228. $data = [
  229. 'status' => true,
  230. 'message' => '删除成功!',
  231. ];
  232. \DB::commit();
  233. return response()->json($data);
  234. } catch (\Exception $e) {
  235. \DB::rollback();
  236. $data = [
  237. 'status' => false,
  238. 'message' => '删除失败!',
  239. ];
  240. return response()->json($data);
  241. }
  242. }
  243. }