BaseProductController.php 8.0 KB

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