BaseProductController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\BaseProduct;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Form\NestedForm;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Layout\Content;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use App\Admin\Repositories\BaseProductCategory;
  12. use App\Admin\Repositories\BaseProductImage;
  13. use Illuminate\Http\Request;
  14. use App\Libraries\CommonHelper;
  15. class BaseProductController extends AdminController
  16. {
  17. public function index(Content $content)
  18. {
  19. return $content
  20. ->header(' Product Management')
  21. ->description('all')
  22. ->breadcrumb(['text'=>'Product Management','url'=>''])
  23. ->body($this->grid());
  24. }
  25. /**
  26. * Make a grid builder.
  27. *
  28. * @return Grid
  29. */
  30. protected function grid()
  31. {
  32. return Grid::make(BaseProduct::with(['baseProductCategory','images']), function (Grid $grid) {
  33. $grid->column('id')->sortable();
  34. $grid->column('title');
  35. $grid->column('sku');
  36. $grid->column('base_product_category.name',admin_trans_label('category_name'));
  37. $grid->column('issuance_date');
  38. $grid->column('images')->display(function ($images) {
  39. $images = $images->toArray();
  40. $dataImages = array_column($images, 'image_url');
  41. return CommonHelper::displayImage($dataImages,100);
  42. });
  43. $grid->column('order')->orderable();
  44. $grid->column('is_pinned')->switch();
  45. $grid->column('enabled')->switch();
  46. $grid->column('created_at');
  47. $grid->column('updated_at')->sortable();
  48. // 筛选
  49. $grid->filter(function (Grid\Filter $filter) {
  50. $filter->panel();
  51. $filter->expand();
  52. $filter->equal('sku')->width(2);
  53. $filter->like('title')->width(2);
  54. $filter->equal('category_id',admin_trans_label('category_name'))->select(BaseProductCategory::selectOptions())->width(2);
  55. $filter->equal('enabled')->select(config('dictionary.enabled'))->width(2);
  56. });
  57. //排序
  58. $grid->model()->orderBy("is_pinned",'desc')->orderBy("order",'desc');
  59. });
  60. }
  61. /**
  62. * Make a show builder.
  63. *
  64. * @param mixed $id
  65. *
  66. * @return Show
  67. */
  68. protected function detail($id)
  69. {
  70. return Show::make($id, BaseProduct::with(['baseProductCategory','images']), function (Show $show) {
  71. $show->field('id');
  72. $show->field('title');
  73. $show->field('keywords');
  74. $show->field('description');
  75. $show->field('sku');
  76. $show->field('base_product_category.name',admin_trans_label('category_name'));
  77. $show->field('issuance_date');
  78. $show->field('parameters')->as(function ($items) {
  79. if (is_array($items)) {
  80. // 创建表格的表头
  81. $table = '<table class="table">';
  82. $table .= '<tr><th>key</th><th>value</th></tr>';
  83. // 遍历数组并将数据填充到表格中
  84. foreach ($items as $item) {
  85. $table .= '<tr>';
  86. $table .= '<td>' . $item['key'] . '</td>'; // 商品名称
  87. $table .= '<td>' . $item['value'] . '</td>'; // 数量
  88. $table .= '</tr>';
  89. }
  90. $table .= '</table>';
  91. return $table;
  92. }
  93. return ''; // 当没有数组数据时
  94. })->unescape();
  95. $show->field('images')->as(function ($images) {
  96. // 开始生成 HTML
  97. $dataImages = array_column($images, 'image_url');
  98. return CommonHelper::displayImage($dataImages,150);
  99. })->unescape();
  100. $show->field('content')->unescape();
  101. $show->field('order');
  102. $show->field('enabled')->using(config('dictionary.enabled'));
  103. $show->field('created_at');
  104. $show->field('updated_at');
  105. });
  106. }
  107. /**
  108. * Make a form builder.
  109. *
  110. * @return Form
  111. */
  112. protected function form()
  113. {
  114. return Form::make(BaseProduct::with('images'), function (Form $form) {
  115. $form->display('id');
  116. $form->select('category_id', admin_trans_label('category_name'))
  117. ->options(BaseProductCategory::selectOptions())
  118. ->required();
  119. $form->text('title')->required();
  120. $form->text('keywords');
  121. $form->textarea('description');
  122. $form->text('sku')->required();
  123. $form->date('issuance_date');
  124. $form->table('parameters',admin_trans_label('parameter_name'), function (Form\NestedForm $table) {
  125. $table->text('key')->required();
  126. $table->text('value')->required();
  127. });
  128. // 多图上传
  129. $form->multipleImage('images', admin_trans_label('images'))
  130. ->sortable() // 可拖动排序
  131. ->removable() // 可移除图片
  132. ->autoUpload() // 自动上传
  133. ->uniqueName()
  134. ->accept(config('admin.upload.oss_image.accept'))
  135. ->maxSize(config('admin.upload.oss_image.max_size'))
  136. ->dir('images/product/'.date("Ymd"))
  137. ->customFormat(function () {
  138. // 数据格式化为数组['1.jpg','2.jpg'] 编辑时用到
  139. return array_column($this->images, 'image_url');
  140. })
  141. ->saving(function ($images) {
  142. return array_map(function($image) {
  143. return ['image_url' => $image];
  144. }, $images);
  145. });
  146. $form->editor('content');
  147. $form->switch('is_pinned')->default(0);
  148. $form->switch('enabled')->default(1);
  149. //插入JS
  150. $this->addParametersJs();
  151. //保存前回调
  152. $form->saving(function ($form) {
  153. //检查sku是否重复
  154. $baseProduct = new BaseProduct();
  155. if ($form->isCreating()) {
  156. $count = $baseProduct->model()->where('sku', $form->sku)->count();
  157. } else {
  158. $count = $baseProduct->model()->where('sku', $form->sku)->where('id', '!=', $form->getKey())->count();
  159. }
  160. if ($count > 0) {
  161. return $form->response()->error('sku already exists');
  162. }
  163. //保存前回调删除图片
  164. if (!$form->isCreating()) {
  165. //清空图片
  166. $id = $form->getKey();
  167. $baseProductImage = new BaseProductImage();
  168. $baseProductImage->model()->where('product_id', $id)->delete();
  169. }
  170. });
  171. });
  172. }
  173. /*
  174. * 以json型式返回产品参数
  175. */
  176. public static function parameter(Request $request)
  177. {
  178. $id = $request->query('q');
  179. $content = BaseProductCategory::getParameter($id);
  180. return $content;
  181. }
  182. /**
  183. * 分类与参数联动JS
  184. * @return void
  185. */
  186. private function addParametersJs()
  187. {
  188. $prefix = config('admin.route.prefix');
  189. //插入JS
  190. Admin::script(
  191. <<<JS
  192. var fill_param = function (key,val) {
  193. lastForm = $(".has-many-table-parameters-form:last");
  194. lastForm.find('input').eq(0).val(key);
  195. lastForm.find('input').eq(1).val(val);
  196. }
  197. $('select[name="category_id"]').on('change', function() {
  198. var category_id = $(this).val();
  199. // 清空现有的表格行
  200. $('.has-many-table-parameters-form').remove();
  201. if (category_id > 0) {
  202. $.ajax({
  203. url: '/{$prefix}/base-product/parameter',
  204. data: { q: category_id},
  205. dataType: 'json',
  206. type: 'GET', // GET
  207. success: function(data) { // success
  208. if (Array.isArray(data) && data.length === 0) {
  209. return null;
  210. }
  211. // 动态添加新数据到表格
  212. $.each(data, function(index, item) {
  213. $(".has-many-table-parameters").find(".add").click();
  214. fill_param(item.key,item.value);
  215. });
  216. },
  217. error: function(error) { // 错误时执行的代码
  218. console.log('error:', error);
  219. }
  220. });
  221. }
  222. });
  223. JS
  224. );
  225. }
  226. }