ProductController.php 9.6 KB

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