ProductController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. $filter->between('name')->datetime()->width(4);
  89. });
  90. $grid->batchActions([new BatchProduct()]);
  91. $grid->disableViewButton();
  92. $grid->export()
  93. ->titles([
  94. 'name' => '产品名称',
  95. 'cate_name' => '产品分类',
  96. 'cover_img' => '产品截面图',
  97. 'cases' => '案例',
  98. 'tech_param' => '技术参数文件',
  99. 'cad_model' => 'CAD模型文件',
  100. 'cad_design' => 'CAD设计文件',
  101. 'su_model' => 'SU模型文件',
  102. 'other' => '其他文件',
  103. 'is_opened' => '上架状态',
  104. 'sort' => '排序(越大越靠前)',
  105. 'specs' => '规格',
  106. ])->rows(function ($rows){
  107. foreach ($rows as $index => &$row) {
  108. $specs = [];
  109. foreach ($row['specs'] as $spec){
  110. $arr = [];
  111. foreach ($spec['specs'] as $item){
  112. $arr[] = "{$item['name']}(¥{$item['price']})";
  113. }
  114. $specs[] = $spec['name'].':('.implode(",",$arr).')';
  115. }
  116. $row['cate_name'] = $row['cate']['name'];
  117. $row['cases'] = implode(",",$row['cases']);
  118. $row['tech_param'] = implode(",",$row['tech_param']);
  119. $row['cad_model'] = implode(",",$row['cad_model']);
  120. $row['cad_design'] = implode(",",$row['cad_design']);
  121. $row['su_model'] = implode(",",$row['su_model']);
  122. $row['other'] = implode(',', $row['other']);
  123. $row['is_opened'] = config('global.bool_status')[$row['is_opened']];
  124. $row['specs'] = implode(",",$specs);
  125. }
  126. return $rows;
  127. })
  128. ->xlsx()
  129. ->disableExportSelectedRow();
  130. });
  131. }
  132. /**
  133. * Make a show builder.
  134. *
  135. * @param mixed $id
  136. *
  137. * @return Show
  138. */
  139. protected function detail($id)
  140. {
  141. return Show::make($id, new Product(), function (Show $show) {
  142. $show->field('id');
  143. $show->field('name');
  144. $show->field('cover_img');
  145. $show->field('cases');
  146. $show->field('origin_price');
  147. $show->field('sale_price');
  148. $show->field('sort');
  149. $show->field('is_opened');
  150. $show->field('tech_param');
  151. $show->field('cad_model');
  152. $show->field('cad_design');
  153. $show->field('su_model');
  154. $show->field('other');
  155. $show->field('created_at');
  156. $show->field('updated_at');
  157. });
  158. }
  159. /**
  160. * Make a form builder.
  161. *
  162. * @return Form
  163. */
  164. protected function form()
  165. {
  166. return Form::make(Product::with(['specs']), function (Form $form) {
  167. $form->display('id');
  168. $cates = ProductCategory::select(['id','name'])->where('is_opened',1)->get()->toArray();
  169. /* @var Form $form*/
  170. $form->select('cate_id')
  171. ->options(array_column($cates,'name','id'))
  172. ->required();
  173. $form->text('name')->required();
  174. $form->image('cover_img','产品截面图(宽高比 0.9:1)')->saveFullUrl()
  175. ->uniqueName()->autoUpload()
  176. ->autoSave(false)
  177. ->removable(false)
  178. ->width(4)
  179. ->required();
  180. $form->multipleImage('cases','案例(宽高比 7.5:x)')->saveFullUrl()
  181. ->uniqueName()->autoUpload()
  182. ->autoSave(false)
  183. ->removable(false)
  184. ->width(4)->sortable();
  185. $form->table('tech_param', function (Form\NestedForm $table) {
  186. $table->url('url','链接')->required();
  187. });
  188. $form->table('cad_model', function (Form\NestedForm $table) {
  189. $table->url('url','链接')->required();
  190. });
  191. $form->table('cad_design', function (Form\NestedForm $table) {
  192. $table->url('url','链接')->required();
  193. });
  194. $form->table('su_model', function (Form\NestedForm $table) {
  195. $table->url('url','链接')->required();
  196. });
  197. $form->table('other', function (Form\NestedForm $table) {
  198. $table->text('name','名称')->required();
  199. $table->url('url','链接')->required();
  200. });
  201. $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
  202. $form->number('sort');
  203. $form->disableViewButton();
  204. $form->disableDeleteButton();
  205. $form->disableListButton();
  206. $form->disableEditingCheck();
  207. $form->disableViewCheck();
  208. $form->disableCreatingCheck();
  209. });
  210. }
  211. }