BaseProductController.php 9.1 KB

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