ProductController.php 10 KB

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