73 lines
1.9 KiB
PHP
73 lines
1.9 KiB
PHP
<?php
|
||
|
||
namespace App\Admin\Extensions\Grid\Tools\Product;
|
||
|
||
use App\Models\ProductSku;
|
||
use App\Models\ProductSpu;
|
||
use Dcat\Admin\Grid\Tools\AbstractTool;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Throwable;
|
||
|
||
class InitSkuBySpecs extends AbstractTool
|
||
{
|
||
public function __construct($spu_id = 0, $title=null)
|
||
{
|
||
parent::__construct($title);
|
||
$this->setKey($spu_id);
|
||
}
|
||
|
||
protected function authorize($user): bool
|
||
{
|
||
return $user->can('dcat.admin.product_spus.init_sku_by_specs');
|
||
}
|
||
|
||
/**
|
||
* 按钮样式定义,默认 btn btn-white waves-effect
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $style = 'btn btn-primary';
|
||
|
||
/**
|
||
* 按钮文本
|
||
*
|
||
* @return string|void
|
||
*/
|
||
public function title()
|
||
{
|
||
return __('admin_message.extensions.grid.tools.init_sku_by_specs');
|
||
}
|
||
|
||
public function confirm()
|
||
{
|
||
// 显示标题和内容
|
||
return [__('admin_message.extensions.grid.init_sku_by_specs.confirm.title'), __('admin_message.extensions.grid.init_sku_by_specs.confirm.message')];
|
||
}
|
||
|
||
/**
|
||
* 处理请求
|
||
* 如果你的类中包含了此方法,则点击按钮后会自动向后端发起ajax请求,并且会通过此方法处理请求逻辑
|
||
*
|
||
* @param Request $request
|
||
*/
|
||
public function handle(Request $request)
|
||
{
|
||
$nowTime = now();
|
||
//获取SPU
|
||
$spu = ProductSpu::with('specs')->findOrFail($this->getKey());
|
||
try {
|
||
DB::beginTransaction();
|
||
ProductSku::createBySpu($spu);
|
||
DB::commit();
|
||
} catch (Throwable $th) {
|
||
DB::rollBack();
|
||
report($th);
|
||
return $this->response()->error(__('admin.update_failed').': '.$th->getMessage())->refresh();
|
||
}
|
||
|
||
|
||
return $this->response()->success(__('admin.update_succeeded'))->refresh();
|
||
}
|
||
}
|