链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Admin\Controllers\v3;
  3. use App\Admin\Repositories\v3\Goods;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Controllers\AdminController;
  8. use App\Models\v3\Category as CategoryModel;
  9. use App\Models\v3\Store as StoreModel;
  10. use Dcat\Admin\Form\NestedForm;
  11. use App\Models\v3\Goods as GoodsModel;
  12. use App\Models\v3\Market as MarketModel;
  13. class GoodsController extends AdminController
  14. {
  15. /**
  16. * Make a grid builder.
  17. *
  18. * @return Grid
  19. */
  20. protected function grid()
  21. {
  22. return Grid::make(new Goods(), function (Grid $grid) {
  23. $grid->id->sortable();
  24. $grid->cover_img_url->image('',50);
  25. $grid->name;
  26. $grid->category_id->display(function ($categoryId){
  27. $category = CategoryModel::getCategoryInfo($categoryId,'title');
  28. return empty($category) ? '' : $category->title;
  29. });
  30. $grid->store_id->display(function ($storeId){
  31. $store = StoreModel::getStoreInfo($storeId,'name');
  32. return empty($store) ? '' : $store->name;
  33. });
  34. $grid->price;
  35. $grid->sort->sortable();
  36. $grid->on_sale->switch();
  37. $grid->filter(function (Grid\Filter $filter) {
  38. $filter->equal('id');
  39. });
  40. $grid->model()->orderBy('id', 'desc');
  41. // 每页10条
  42. $grid->paginate(10);
  43. });
  44. }
  45. /**
  46. * Make a show builder.
  47. *
  48. * @param mixed $id
  49. *
  50. * @return Show
  51. */
  52. protected function detail($id)
  53. {
  54. // $g = GoodsModel::find(1572);
  55. // dd($g->toArray());
  56. return Show::make($id, new Goods(), function (Show $show) {
  57. $show->row(function (Show\Row $show) {
  58. $show->width(6)->field('id');
  59. $show->width(6)->field('name');
  60. $show->width(6)->category_id->as(function ($categoryId){
  61. $item = CategoryModel::getCategoryInfo($categoryId,'title');
  62. return empty($item) ? '' : $item->title;
  63. });
  64. $show->width(6)->store_id->as(function ($storeId){
  65. $item = StoreModel::getStoreInfo($storeId,'name');
  66. return empty($item) ? '' : $item->name;
  67. });
  68. $show->width(6)->cover_img->image();
  69. $show->width(6)->price;
  70. $show->width(6)->original_price;
  71. $show->width(6)->vip_price;
  72. $show->width(6)->inventory;
  73. $show->width(6)->goods_unit;
  74. });
  75. $show->row(function (Show\Row $show) {
  76. $show->width(6)->on_sale_text;
  77. $show->width(6)->content;
  78. $show->width(6)->sort;
  79. $show->width(6)->restrict_num;
  80. $show->width(6)->start_num;
  81. $show->width(6)->is_infinite_text;
  82. $show->width(6)->tags->as(function ($value){
  83. return $value;
  84. });
  85. $show->width(6)->spec->as(function ($value){
  86. $text = '';
  87. foreach ($value as $v){
  88. $text .= '【'.$v['spec_key'] .':'.$v['spec_value'].'】 ';
  89. }
  90. return $text;
  91. });
  92. $goodsModel = new GoodsModel();
  93. $show->html(function () use($goodsModel){
  94. if(empty($this->details_imgs)){
  95. return '';
  96. }else{
  97. // 还需要优化
  98. $value = $this->details_imgs;
  99. $text = '';
  100. foreach ($value as $v){
  101. $img = $goodsModel->imageUrl($v);
  102. $text .= "<img src='$img' />";
  103. }
  104. return $text;
  105. }
  106. });
  107. // $show->created_at;
  108. // $show->updated_at;
  109. });
  110. });
  111. }
  112. /**
  113. * Make a form builder.
  114. *
  115. * @return Form
  116. */
  117. protected function form()
  118. {
  119. $builder = GoodsModel::with(['ImageBanners','VideoBanners']);
  120. return Form::make($builder, function (Form $form) {
  121. $form->hidden('id');
  122. // 二级分类
  123. $categoryList = CategoryModel::getCategoryArray([['parent_id','>',0]]);
  124. // 店铺
  125. $storeList = StoreModel::getStoreArray();
  126. //市场
  127. $marketList = MarketModel::getMarket();
  128. $form->column(6, function (Form $form) use($marketList,$storeList,$categoryList ){
  129. $form->select('market_id')->required()->options($marketList);
  130. $form->select('category_id')->required()->options($categoryList);
  131. $form->select('store_id')->required()->options($storeList);
  132. $form->text('name')->required()->maxLength(20);
  133. $form->image('cover_img')->required();
  134. $form->number('sort');
  135. });
  136. $form->column(6, function (Form $form) {
  137. $form->currency('price')->required()->floatTwo()->symbol('¥');
  138. $form->currency('original_price')->required()->floatTwo()->symbol('¥');
  139. $form->currency('vip_price')->required()->floatTwo()->symbol('¥');
  140. $form->text('goods_unit')->help('如:斤,个,盒,500克,1000克,1500克等');
  141. $form->radio('is_infinite')->options(['关闭','开启'])->default(1);
  142. $form->number('inventory')->required()->attribute('min', 1)->default(1);
  143. $form->number('restrict_num')->attribute('min', 0)->default(0)->help('0表示不限购');
  144. $form->number('start_num')->attribute('min', 1)->default(1);
  145. });
  146. $form->column(12, function (Form $form) {
  147. $form->hasMany('image_banners', function (Form\NestedForm $form) {
  148. $form->image('path')->width(2);
  149. $form->number('sort')->width(2)->default(0);
  150. $form->hidden('type')->value(1)->default(1);
  151. })->required();
  152. $form->divider();
  153. $form->hasMany('video_banners', function (Form\NestedForm $form) {
  154. $form->file('path')->width(2);
  155. $form->number('sort')->width(2)->default(0);
  156. $form->hidden('type')->value(2)->default(2);
  157. });
  158. $form->divider();
  159. $form->multipleImage('details_imgs')->help('按图片顺序上传');
  160. $form->text('content')->maxLength(50);
  161. $form->text('remark')->maxLength(50);
  162. $form->tags('tags','标签')->options(['新品','热销','新鲜']);
  163. $form->table('spec', function (NestedForm $table) {
  164. $table->text('spec_key')->help('如:净含量:500克,保质期:120天,包装:12个/盒等');
  165. $table->text('spec_value');
  166. });
  167. $form->switch('on_sale')
  168. ->customFormat(function ($v) {
  169. return $v == '上架' ? 1 : 0;
  170. })
  171. ->saving(function ($v) {
  172. return $v;
  173. });
  174. $form->disableResetButton();
  175. $form->disableViewCheck();
  176. $form->disableEditingCheck();
  177. $form->disableCreatingCheck();
  178. });
  179. });
  180. }
  181. }