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

131 lines
4.1 KiB

  1. <?php
  2. namespace App\Admin\Forms\v3;
  3. use Dcat\Admin\Widgets\Form;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use App\Models\v3\Store as StoreModel;
  6. use App\Models\v3\Goods as GoodsModel;
  7. use App\Models\v3\GoodsBanners as GoodsBannerModel;
  8. use App\Admin\Common\CustomFileController;
  9. class GoodsCopyForm extends Form
  10. {
  11. /**
  12. * Handle the form request.
  13. *
  14. * @param array $input
  15. *
  16. * @return Response
  17. */
  18. public function handle(array $input)
  19. {
  20. // 获取外部传递参数
  21. $goodsId = $input['goods_id'];
  22. $storeIds= $input['store_ids'];
  23. $ossImageDir = config('admin.upload.directory.image');
  24. $goods = GoodsModel::find($goodsId);
  25. $goodsBanners = GoodsBannerModel::where('goods_id',$goodsId)->get();
  26. $markets = StoreModel::whereIn('id',$storeIds)->pluck('market_id','id');
  27. foreach($storeIds as $key =>$value){
  28. $storeId = $value;
  29. $newCoverImg = $this->copyImage($ossImageDir,$goods->cover_img);
  30. $model = new GoodsModel();
  31. $model->store_id = $storeId;
  32. $model->market_id = $markets[$storeId];
  33. $model->category_id = $goods->category_id;
  34. $model->name = $goods->name;
  35. $model->cover_img = $newCoverImg;
  36. $model->goods_unit = $goods->goods_unit;
  37. $model->tags = $goods->tags;
  38. $model->spec = $goods->spec;
  39. $model->details_imgs = $goods->details_imgs;
  40. $model->content = $goods->content;
  41. $model->details = $goods->details;
  42. $model->price = $goods->price;
  43. $model->original_price = $goods->original_price;
  44. $model->vip_price = $goods->vip_price;
  45. $model->start_num = $goods->start_num;
  46. $model->restrict_num = $goods->restrict_num;
  47. $model->is_infinite = $goods->is_infinite;
  48. $model->inventory = $goods->inventory;
  49. $model->on_sale = $goods->on_sale;
  50. $model->sort = $goods->sort;
  51. $model->remark = $goods->remark;
  52. if($model->save() && !empty($goodsBanners)){
  53. $goodsId = $model->getKey();
  54. foreach($goodsBanners as $kb => $vb){
  55. $newBanner = $this->copyImage($ossImageDir, $vb->path);
  56. if(!empty($newBanner)){
  57. $banners = [
  58. 'goods_id' => $goodsId,
  59. 'type' => $vb->type,
  60. 'path' => $newBanner,
  61. 'sort' => $vb->sort,
  62. 'created_at' => time(),
  63. 'updated_at' => time(),
  64. ];
  65. GoodsBannerModel::insert($banners);
  66. }
  67. }
  68. };
  69. }
  70. return $this->success('复制成功', '/goods');
  71. }
  72. /**
  73. * Build a form here.
  74. */
  75. public function form()
  76. {
  77. $id = $this->getKey();
  78. $goods = GoodsModel::select('name')->find($id);
  79. $this->hidden('goods_id')->value($id);
  80. $name = empty($goods->name)?'':$goods->name;
  81. $this->display('name')->value($name);
  82. $stores = StoreModel::getStoreArray();
  83. $this->multipleSelect('store_ids','选择店铺')->required()->options($stores)->help('选择店铺,将当前商品复制到所选店铺。注意选择重复店铺问题!!!');
  84. }
  85. /**
  86. * The data of the form.
  87. *
  88. * @return array
  89. */
  90. public function default()
  91. {
  92. return [];
  93. }
  94. /**
  95. * 复制的商品图片
  96. * @param $ossImageDir oss 存储文件夹
  97. * @param $imagePath 图片绝对路径
  98. */
  99. public function copyImage($ossImageDir, $imagePath)
  100. {
  101. $return = '';
  102. $oldCoverImg = $imagePath;
  103. $customFile = new CustomFileController();
  104. $result = $customFile->autoCopyFile($ossImageDir, $oldCoverImg);
  105. if($result['status']){
  106. $return = $result['path'];
  107. }
  108. return $return;
  109. }
  110. }