ProductController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\Product;
  4. use App\Models\ProductCategory;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. class ProductController extends AdminController
  10. {
  11. /**
  12. * Make a grid builder.
  13. *
  14. * @return Grid
  15. */
  16. protected function grid()
  17. {
  18. return Grid::make(Product::with('cate'), function (Grid $grid) {
  19. $grid->model()->orderByDesc('sort');
  20. $grid->column('id')->sortable();
  21. $grid->column('name')->label('info');
  22. $grid->column('cate_id')->display(function (){
  23. return $this->cate->name;
  24. })->label('success');
  25. $grid->column('cover_img')->image('',80);
  26. $grid->column('cases')->display(function (){
  27. $html = '';
  28. foreach ($this->cases as $case){
  29. $html .= '<img data-action="preview-img" src="'.$case.'"
  30. style="max-width:80px;max-height:80px;cursor:pointer"
  31. class="img img-thumbnail">';
  32. }
  33. return $html;
  34. });
  35. $grid->column('tech_param')->display(function (){
  36. $html = '';
  37. foreach ($this->tech_param as $key => $tech_param){
  38. $index = $key+1;
  39. $html .= "<a href='{$tech_param}' download target='_blank'>参数文件{$index}</a><br>";
  40. }
  41. return $html;
  42. });
  43. $grid->column('cad_model')->display(function (){
  44. $html = '';
  45. foreach ($this->cad_model as $key => $tech_param){
  46. $index = $key+1;
  47. $html .= "<a href='{$tech_param}' download target='_blank'>CAD模型{$index}</a><br>";
  48. }
  49. return $html;
  50. });
  51. $grid->column('cad_design')->display(function (){
  52. $html = '';
  53. foreach ($this->cad_design as $key => $tech_param){
  54. $index = $key+1;
  55. $html .= "<a href='{$tech_param}' download target='_blank'>CAD设计$index</a><br>";
  56. }
  57. return $html;
  58. });
  59. $grid->column('su_model')->display(function (){
  60. $html = '';
  61. foreach ($this->su_model as $key => $tech_param){
  62. $index = $key+1;
  63. $html .= "<a href='{$tech_param}' download target='_blank'>SU模型{$index}</a><br>";
  64. }
  65. return $html;
  66. });
  67. $grid->column('other')->display(function (){
  68. $html = '';
  69. foreach ($this->other as $key => $tech_param){
  70. $index = $key+1;
  71. $html .= "<a href='{$tech_param}' download target='_blank'>其他文件{$index}</a><br>";
  72. }
  73. return $html;
  74. });
  75. $grid->column('is_opened')->switch();
  76. $grid->column('sort')->editable();
  77. $grid->column('created_at');
  78. $grid->actions(function (Grid\Displayers\Actions $actions) {
  79. $id = $actions->getKey();
  80. // append一个操作
  81. $actions->append('<a href="/admin/product/'.$id.'/spec"><i class="fa fa-align-left"></i> 规格管理</a>');
  82. });
  83. $grid->filter(function (Grid\Filter $filter) {
  84. $filter->panel();
  85. $filter->like('name')->width(2);
  86. });
  87. $grid->disableViewButton();
  88. });
  89. }
  90. /**
  91. * Make a show builder.
  92. *
  93. * @param mixed $id
  94. *
  95. * @return Show
  96. */
  97. protected function detail($id)
  98. {
  99. return Show::make($id, new Product(), function (Show $show) {
  100. $show->field('id');
  101. $show->field('name');
  102. $show->field('cover_img');
  103. $show->field('cases');
  104. $show->field('origin_price');
  105. $show->field('sale_price');
  106. $show->field('sort');
  107. $show->field('is_opened');
  108. $show->field('tech_param');
  109. $show->field('cad_model');
  110. $show->field('cad_design');
  111. $show->field('su_model');
  112. $show->field('other');
  113. $show->field('created_at');
  114. $show->field('updated_at');
  115. });
  116. }
  117. /**
  118. * Make a form builder.
  119. *
  120. * @return Form
  121. */
  122. protected function form()
  123. {
  124. return Form::make(new Product(), function (Form $form) {
  125. $form->display('id');
  126. $cates = ProductCategory::select(['id','name'])->where('is_opened',1)->get()->toArray();
  127. $form->select('cate_id')
  128. ->options(array_column($cates,'name','id'))
  129. ->required();;
  130. $form->text('name')->required();;;
  131. $form->image('cover_img')->saveFullUrl()
  132. ->uniqueName()->autoUpload()
  133. ->autoSave(false)
  134. ->removable(false)
  135. ->width(4)
  136. ->required();
  137. $form->multipleImage('cases')->saveFullUrl()
  138. ->uniqueName()->autoUpload()
  139. ->autoSave(false)
  140. ->removable(false)
  141. ->width(4);
  142. $form->multipleFile('tech_param')->saveFullUrl()
  143. ->uniqueName()->autoUpload()
  144. ->autoSave(false)
  145. ->removable(false)
  146. ->width(4);
  147. $form->multipleFile('cad_model')->saveFullUrl()
  148. ->uniqueName()->autoUpload()
  149. ->autoSave(false)
  150. ->removable(false)
  151. ->width(4);
  152. $form->multipleFile('cad_design')->saveFullUrl()
  153. ->uniqueName()->autoUpload()
  154. ->autoSave(false)
  155. ->removable(false)
  156. ->width(4);
  157. $form->multipleFile('su_model')->saveFullUrl()
  158. ->uniqueName()->autoUpload()
  159. ->autoSave(false)
  160. ->removable(false)
  161. ->width(4);
  162. $form->multipleFile('other')->saveFullUrl()
  163. ->uniqueName()->autoUpload()
  164. ->autoSave(false)
  165. ->removable(false)
  166. ->width(4);
  167. $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
  168. $form->number('sort');
  169. $form->disableViewButton();
  170. $form->disableDeleteButton();
  171. $form->disableListButton();
  172. $form->disableEditingCheck();
  173. $form->disableViewCheck();
  174. $form->disableCreatingCheck();
  175. });
  176. }
  177. }