ProductController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Grid\BatchProduct;
  4. use App\Models\Product;
  5. use App\Models\ProductCategory;
  6. use App\Models\ProductSpec;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Layout\Content;
  10. use Dcat\Admin\Layout\Row;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use Dcat\Admin\Widgets\Tab;
  14. class ProductController extends AdminController
  15. {
  16. /**
  17. * Make a grid builder.
  18. *
  19. * @return Grid
  20. */
  21. protected function grid()
  22. {
  23. return Grid::make(Product::with(['cate','specs']), function (Grid $grid) {
  24. $grid->model()->orderByDesc('sort');
  25. $grid->column('id')->sortable();
  26. $grid->column('name')->label('info');
  27. $grid->column('cate_id')->display(function (){
  28. return $this->cate->name;
  29. })->label('success');
  30. $grid->column('cover_img')->image('',80);
  31. $grid->column('cases')->display(function (){
  32. $html = '';
  33. foreach ($this->cases as $case){
  34. $html .= '<img data-action="preview-img" src="'.$case.'"
  35. style="max-width:80px;max-height:80px;cursor:pointer"
  36. class="img img-thumbnail">';
  37. }
  38. return $html;
  39. })->width(300);
  40. $grid->column('tech_param')->display(function (){
  41. $html = '';
  42. foreach ($this->tech_param as $key => $item){
  43. $html .= "<a href='{$item['url']}' download target='_blank'>技术参数</a><br>";
  44. }
  45. return $html;
  46. });
  47. $grid->column('cad_model')->display(function (){
  48. $html = '';
  49. foreach ($this->cad_model as $key => $item){
  50. $html .= "<a href='{$item['url']}' download target='_blank'>CAD模型</a><br>";
  51. }
  52. return $html;
  53. });
  54. $grid->column('cad_design')->display(function (){
  55. $html = '';
  56. foreach ($this->cad_design as $key => $item){
  57. $html .= "<a href='{$item['url']}' download target='_blank'>CAD设计</a><br>";
  58. }
  59. return $html;
  60. });
  61. $grid->column('su_model')->display(function (){
  62. $html = '';
  63. foreach ($this->su_model as $key => $item){
  64. $html .= "<a href='{$item['url']}' download target='_blank'>SU模型</a><br>";
  65. }
  66. return $html;
  67. });
  68. $grid->column('other')->display(function (){
  69. $html = '';
  70. foreach ($this->other as $key => $item){
  71. $html .= "<a href='{$item['url']}' download target='_blank'>{$item['name']}</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->column('spec','规格')->display(function (){
  79. $url = '/admin/product/'.$this->id.'/spec';
  80. return "<a href='$url'>规格管理</a>";
  81. });
  82. $grid->filter(function (Grid\Filter $filter) {
  83. $filter->panel();
  84. $filter->equal('cate_id')->select(function (){
  85. return ProductCategory::select(['id','name'])->get()->pluck('name','id')->toArray();
  86. })->width(2);
  87. $filter->like('name')->width(2);
  88. });
  89. $grid->batchActions([new BatchProduct()]);
  90. $grid->disableViewButton();
  91. $grid->export()
  92. ->titles([
  93. 'name' => '产品名称',
  94. 'cate_name' => '产品分类',
  95. 'cover_img' => '产品截面图',
  96. 'cases' => '案例',
  97. 'tech_param' => '技术参数文件',
  98. 'cad_model' => 'CAD模型文件',
  99. 'cad_design' => 'CAD设计文件',
  100. 'su_model' => 'SU模型文件',
  101. 'other' => '其他文件',
  102. 'is_opened' => '上架状态',
  103. 'sort' => '排序(越大越靠前)',
  104. 'specs' => '规格',
  105. ])->rows(function ($rows){
  106. foreach ($rows as $index => &$row) {
  107. $specs = [];
  108. foreach ($row['specs'] as $spec){
  109. $arr = [];
  110. foreach ($spec['specs'] as $item){
  111. $arr[] = "{$item['name']}(¥{$item['price']})";
  112. }
  113. $specs[] = $spec['name'].':('.implode(",",$arr).')';
  114. }
  115. $row['cate_name'] = $row['cate']['name'];
  116. $row['cases'] = implode(",",$row['cases']);
  117. $row['tech_param'] = implode(",",$row['tech_param']);
  118. $row['cad_model'] = implode(",",$row['cad_model']);
  119. $row['cad_design'] = implode(",",$row['cad_design']);
  120. $row['su_model'] = implode(",",$row['su_model']);
  121. $row['other'] = implode(',', $row['other']);
  122. $row['is_opened'] = config('global.bool_status')[$row['is_opened']];
  123. $row['specs'] = implode(",",$specs);
  124. }
  125. return $rows;
  126. })
  127. ->xlsx()
  128. ->disableExportSelectedRow();
  129. });
  130. }
  131. /**
  132. * Make a show builder.
  133. *
  134. * @param mixed $id
  135. *
  136. * @return Show
  137. */
  138. protected function detail($id)
  139. {
  140. return Show::make($id, new Product(), function (Show $show) {
  141. $show->field('id');
  142. $show->field('name');
  143. $show->field('cover_img');
  144. $show->field('cases');
  145. $show->field('origin_price');
  146. $show->field('sale_price');
  147. $show->field('sort');
  148. $show->field('is_opened');
  149. $show->field('tech_param');
  150. $show->field('cad_model');
  151. $show->field('cad_design');
  152. $show->field('su_model');
  153. $show->field('other');
  154. $show->field('created_at');
  155. $show->field('updated_at');
  156. });
  157. }
  158. /**
  159. * Make a form builder.
  160. *
  161. * @return Form
  162. */
  163. protected function form()
  164. {
  165. return Form::make(Product::with(['specs']), function (Form $form) {
  166. $form->display('id');
  167. $cates = ProductCategory::select(['id','name'])->where('is_opened',1)->get()->toArray();
  168. /* @var Form $form*/
  169. $form->select('cate_id')
  170. ->options(array_column($cates,'name','id'))
  171. ->required();
  172. $form->text('name')->required();
  173. $form->image('cover_img')->saveFullUrl()
  174. ->uniqueName()->autoUpload()
  175. ->autoSave(false)
  176. ->removable(false)
  177. ->width(4)
  178. ->required();
  179. $form->multipleImage('cases')->saveFullUrl()
  180. ->uniqueName()->autoUpload()
  181. ->autoSave(false)
  182. ->removable(false)
  183. ->width(4)->sortable();
  184. $form->table('tech_param', function (Form\NestedForm $table) {
  185. $table->url('url','链接')->required();
  186. });
  187. $form->table('cad_model', function (Form\NestedForm $table) {
  188. $table->url('url','链接')->required();
  189. });
  190. $form->table('cad_design', function (Form\NestedForm $table) {
  191. $table->url('url','链接')->required();
  192. });
  193. $form->table('su_model', function (Form\NestedForm $table) {
  194. $table->url('url','链接')->required();
  195. });
  196. $form->table('other', function (Form\NestedForm $table) {
  197. $table->text('name','名称')->required();
  198. $table->url('url','链接')->required();
  199. });
  200. $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
  201. $form->number('sort');
  202. $form->disableViewButton();
  203. $form->disableDeleteButton();
  204. $form->disableListButton();
  205. $form->disableEditingCheck();
  206. $form->disableViewCheck();
  207. $form->disableCreatingCheck();
  208. });
  209. }
  210. }