链街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.

329 lines
14 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
  1. <?php
  2. namespace App\Admin\Controllers\v3;
  3. use App\Admin\Actions\Grid\v3\GoodsCopy;
  4. use App\Admin\Actions\Grid\v3\GoodsImage;
  5. use App\Admin\Actions\Grid\v3\GoodsSpec;
  6. use App\Admin\Common\Auth;
  7. use App\Admin\Repositories\v3\Goods;
  8. use Dcat\Admin\Admin;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Controllers\AdminController;
  13. use App\Models\v3\Category as CategoryModel;
  14. use App\Models\v3\Store as StoreModel;
  15. use Dcat\Admin\Form\NestedForm;
  16. use App\Models\v3\Goods as GoodsModel;
  17. use App\Models\v3\Market as MarketModel;
  18. use App\Models\v3\GoodsCategory as GoodsCategoryModel;
  19. use App\Models\v3\GoodsBanners as GoodsBannersModel;
  20. class GoodsController extends AdminController
  21. {
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. $marketId = Auth::getMarket();
  30. if ($marketId){
  31. $build = new \App\Models\v3\Goods();
  32. $build = $build::where('market_id',$marketId);
  33. $storeList = StoreModel::getStoreArray([['market_id','=',$marketId]]);
  34. }else{
  35. $build = new Goods();
  36. $storeList = StoreModel::getStoreArray();
  37. }
  38. return Grid::make($build, function (Grid $grid) use($storeList){
  39. // 二级分类
  40. $categoryList = CategoryModel::getArray([['parent_id','>',0]],['选择分类'=>0]);
  41. // 店铺
  42. // $storeList = StoreModel::getStoreArray();
  43. // 商品类目
  44. $goodsCategoryList = GoodsCategoryModel::getArray([],['选择类目'=>0]);
  45. //市场
  46. $marketList = MarketModel::getMarketArray();
  47. $grid->id->sortable();
  48. $grid->cover_img_url->image('',50);
  49. $grid->column('banners')->display(function () {
  50. $id = $this->id;
  51. $banners = GoodsBannersModel::getArray([['goods_id','=',$id],['type','=',1]]);
  52. return $banners;
  53. })->image('', 50);
  54. $grid->name->editable();
  55. $grid->price->editable();
  56. $grid->original_price->editable();
  57. $grid->column('goods_unit','单位')->editable();
  58. $grid->column('spec','规格')->display(function($spec){
  59. $item = '';
  60. if(!empty($spec) && is_array($spec)){
  61. foreach($spec as $key => $value){
  62. $specKey = isset($value['spec_key'])?$value['spec_key']:'';
  63. $specValue = isset($value['spec_value'])?$value['spec_value']:'';
  64. $item .= $specKey.':'.$specValue.'</br>';
  65. }
  66. }
  67. return $item;
  68. });
  69. $grid->column('market_id')->display(function($marketId) use($marketList){
  70. return isset($marketList[$marketId]) ? $marketList[$marketId] : '';
  71. });
  72. $grid->store_id->display(function ($storeId) use($storeList){
  73. return isset($storeList[$storeId]) ? $storeList[$storeId] : '';
  74. })->width('12%');
  75. $grid->category_id->select($categoryList);
  76. // $grid->category_id->display(function ($categoryId) use($categoryList){
  77. // return isset($categoryList[$categoryId]) ? $categoryList[$categoryId] : '';
  78. // });
  79. // $grid->goods_category_id->display(function ($goodsCategoryId) use($goodsCategoryList){
  80. // return isset($goodsCategoryList[$goodsCategoryId]) ? $goodsCategoryList[$goodsCategoryId] : '';
  81. // });
  82. // 简介
  83. $grid->content->width(200);
  84. $grid->on_sale->switch();
  85. $grid->actions([new GoodsCopy(),new GoodsImage(),new GoodsSpec()]);
  86. $grid->filter(function (Grid\Filter $filter) use($storeList, $categoryList, $goodsCategoryList, $marketList){
  87. $filter->equal('id');
  88. $filter->like('name');
  89. $filter->equal('store_id')->select($storeList);
  90. $filter->equal('category_id')->select($categoryList);
  91. $filter->equal('goods_category_id')->select($goodsCategoryList);
  92. if (!(Admin::user()->isRole('market_service'))){
  93. $filter->equal('market_id')->select($marketList);
  94. }
  95. $filter->equal('on_sale')->select(GoodsModel::$_ONSALE);
  96. $filter->equal('price');
  97. $filter->equal('goods_unit');
  98. });
  99. $grid->model()->orderBy('id', 'desc');
  100. // 每页10条
  101. $grid->paginate(10);
  102. });
  103. }
  104. /**
  105. * Make a show builder.
  106. *
  107. * @param mixed $id
  108. *
  109. * @return Show
  110. */
  111. protected function detail($id)
  112. {
  113. // $g = GoodsModel::find(1572);
  114. // dd($g->toArray());
  115. return Show::make($id, new Goods(), function (Show $show) {
  116. $show->row(function (Show\Row $show) {
  117. $show->width(6)->field('id');
  118. $show->width(6)->field('name');
  119. $show->width(6)->category_id->as(function ($categoryId){
  120. $item = CategoryModel::getInfo($categoryId,'title');
  121. return empty($item) ? '' : $item->title;
  122. });
  123. $show->width(6)->store_id->as(function ($storeId){
  124. $item = storeModel::getStoreInfo($storeId,'name');
  125. return empty($item) ? '' : $item->name;
  126. });
  127. $show->width(6)->goods_category_id->as(function ($goodsCategoryId){
  128. $item = GoodsCategoryModel::getInfo($goodsCategoryId,'title');
  129. return empty($item) ? '' : $item->title;
  130. });
  131. $show->width(6)->cover_img->image();
  132. $show->width(6)->price;
  133. $show->width(6)->original_price;
  134. $show->width(6)->vip_price;
  135. $show->width(6)->inventory;
  136. $show->width(6)->goods_unit;
  137. });
  138. $show->row(function (Show\Row $show) {
  139. $show->width(6)->on_sale_text;
  140. $show->width(6)->content;
  141. $show->width(6)->sort;
  142. $show->width(6)->restrict_num;
  143. $show->width(6)->start_num;
  144. $show->width(6)->is_infinite_text;
  145. $show->width(6)->tags->as(function ($value){
  146. return $value;
  147. });
  148. $show->width(6)->spec->as(function ($value){
  149. $text = '';
  150. if($value && is_array($value)){
  151. foreach ($value as $v){
  152. $specKey = isset($v['spec_key'])?$v['spec_key']:'';
  153. $specValue = isset($v['spec_value'])?$v['spec_value']:'';
  154. $text .= '【'.$specKey .':'.$specValue.'】 ';
  155. }
  156. }
  157. return $text;
  158. });
  159. $goodsModel = new GoodsModel();
  160. $show->html(function () use($goodsModel){
  161. if(empty($this->details_imgs)){
  162. return '';
  163. }else{
  164. // 还需要优化
  165. $value = $this->details_imgs;
  166. $text = '';
  167. foreach ($value as $v){
  168. $img = $goodsModel->imageUrl($v);
  169. $text .= "<img src='$img' />";
  170. }
  171. return $text;
  172. }
  173. });
  174. // $show->created_at;
  175. // $show->updated_at;
  176. });
  177. });
  178. }
  179. /**
  180. * Make a form builder.
  181. *
  182. * @return Form
  183. */
  184. protected function form()
  185. {
  186. $builder = GoodsModel::with(['ImageBanners','VideoBanners']);
  187. $marketId = Auth::getMarket();
  188. return Form::make($builder, function (Form $form) use($marketId){
  189. $form->hidden('id');
  190. // 二级分类
  191. $categoryList = CategoryModel::getArray([['parent_id','>',0]]);
  192. // 商品类目
  193. // $goodsCategoryList = GoodsCategoryModel::getArray();
  194. if($marketId){
  195. $storeList = StoreModel::getStoreArray([['market_id','=',$marketId]]);
  196. }else{
  197. $storeList = StoreModel::getStoreArray();
  198. }
  199. //市场
  200. // $marketList = MarketModel::getMarketArray();
  201. $form->column(6, function (Form $form) use($storeList,$categoryList){
  202. $form->hidden('market_id')->default(0);
  203. $form->select('category_id')->required()->options($categoryList)->load('goods_category_id', '/api/goods_category_list');
  204. $form->select('goods_category_id');
  205. $form->select('store_id')->required()->options($storeList);
  206. $form->text('name')->required()->maxLength(20);
  207. if($form->isCreating()){
  208. $form->image('cover_img')->autoUpload()->retainable()->required();
  209. }else{
  210. $form->image('cover_img')->autoUpload()->required();
  211. }
  212. $form->number('sort');
  213. });
  214. $form->column(6, function (Form $form) {
  215. $form->currency('price')->required()->symbol('¥');
  216. $form->currency('original_price')->required()->symbol('¥');
  217. $form->currency('vip_price')->required()->symbol('¥');
  218. $form->text('goods_unit')->help('如:50克,100克,250克,500克,1000克,1500克等');
  219. $form->radio('is_infinite')->options(['关闭','开启'])->default(1);
  220. $form->number('inventory')->required()->attribute('min', 0)->default(1);
  221. $form->number('restrict_num')->attribute('min', 0)->default(0)->help('0表示不限购');
  222. $form->number('start_num')->attribute('min', 1)->default(1);
  223. });
  224. $form->column(12, function (Form $form) {
  225. $form->hasMany('image_banners', function (Form\NestedForm $form) {
  226. $form->image('path')->autoUpload()->required()->width(2);
  227. $form->number('sort')->width(2)->default(0);
  228. $form->hidden('type')->value(1)->default(1);
  229. })->required();
  230. $form->divider();
  231. $form->hasMany('video_banners', function (Form\NestedForm $form) {
  232. $form->file('path')->autoUpload()->width(2);
  233. $form->number('sort')->width(2)->default(0);
  234. $form->hidden('type')->value(2)->default(2);
  235. });
  236. $form->divider();
  237. $form->multipleImage('details_imgs')->help('按图片顺序上传');
  238. $form->text('content')->maxLength(50);
  239. $form->text('remark')->maxLength(50);
  240. $form->tags('tags','标签')->options(['新品','热销','新鲜']);
  241. $form->table('spec', function (NestedForm $table) {
  242. $table->text('spec_key')->required()->help('如:净含量:500克,保质期:120天,包装:12个/盒等');
  243. $table->text('spec_value')->required();
  244. });
  245. $form->switch('on_sale')
  246. ->customFormat(function ($v) {
  247. return $v == 1 ? 1 : 0;
  248. })
  249. ->saving(function ($v) {
  250. return $v == 1 ? 1 : 0;
  251. })->default(1);
  252. $form->disableResetButton();
  253. $form->disableViewCheck();
  254. $form->disableEditingCheck();
  255. $form->disableCreatingCheck();
  256. });
  257. $form->saving(function (Form $form){
  258. $id = $form->getKey;
  259. $storeId = $form->input('store_id');
  260. $tags = $form->input('tags');
  261. $spec = $form->input('spec');
  262. $detailsImgs = $form->input('details_imgs');
  263. $goodsCategoryId = $form->input('goods_category_id');
  264. $isInfinite = $form->input('is_infinite');
  265. $inventory = $form->input('inventory');
  266. $onSale = $form->input('on_sale');
  267. $remark = $form->input('remark');
  268. $OldInventory = $form->model()->inventory;
  269. $OldIsInfinite = $form->model()->is_infinite;
  270. $isInfinite = empty($isInfinite)?$OldIsInfinite:$isInfinite;
  271. $inventory = empty($inventory)?$OldInventory:$inventory;
  272. if(!empty($onSale) && $onSale == 1 && $inventory <= 0 && $isInfinite == 0){
  273. return $form->error('商品上架且关闭无限库存时,库存必须为大于0的数字');
  274. }
  275. if(!empty($storeId)){
  276. // 商品所属市场,根据店铺查询市场
  277. $info = StoreModel::getStoreInfo($storeId,'market_id');
  278. $form->market_id = $info->market_id;
  279. $form->remark = $remark ? $remark : '';
  280. }
  281. $tagsArr = [];
  282. if(!empty($tags)){
  283. foreach($tags as $kt => $tag){
  284. if(empty($tag)){
  285. unset($tags[$kt]);
  286. continue;
  287. }
  288. $tagsArr[] = $tag;
  289. }
  290. };
  291. $form->tags = json_encode($tagsArr);
  292. if($storeId && empty($spec)){
  293. $form->spec = [];
  294. }
  295. if($storeId && empty($detailsImgs)){
  296. $form->details_imgs = [];
  297. }
  298. if($storeId && empty($goodsCategoryId)){
  299. $form->goods_category_id = 0;
  300. }
  301. });
  302. });
  303. }
  304. }