6
0
Fork 0
jiqu-library-server/app/Admin/Extensions/Grid/Tools/Product/InitSkuBySpecs.php

73 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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();
}
}