ProductController.php 9.1 KB

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