ProductController.php 9.5 KB

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