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.
166 lines
5.9 KiB
166 lines
5.9 KiB
<?php
|
|
|
|
namespace App\AdminSupplier\Controllers;
|
|
|
|
use App\AdminSupplier\Repositories\Product;
|
|
use App\Common\ProductStatus;
|
|
use App\Models\AgentProduct;
|
|
use App\Models\AgentProductItem;
|
|
use App\Models\AgentSetting;
|
|
use App\Models\Category;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ProductController extends AdminController
|
|
{
|
|
protected $title = '产品';
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Product(['category:id,name']), function (Grid $grid) {
|
|
$grid->model()->where('supplier_id', Admin::user()->id);
|
|
|
|
$grid->column('id')->sortable();
|
|
$grid->column('category.name', '产品分类');
|
|
$grid->column('title');
|
|
$grid->column('picture')->image('', 60, 60);
|
|
$grid->column('price');
|
|
$grid->column('original_price');
|
|
$grid->column('stock');
|
|
$grid->column('sale');
|
|
$grid->column('status')->using(ProductStatus::array());
|
|
$grid->column('verify_mobile','核销员手机');
|
|
$grid->column('created_at');
|
|
$grid->column('updated_at');
|
|
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->equal('id');
|
|
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Product(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('supplier_id');
|
|
$show->field('category_id');
|
|
$show->field('title');
|
|
$show->field('price');
|
|
$show->field('original_price');
|
|
$show->field('pictures')->image('', 80, 80);
|
|
$show->field('stock');
|
|
$show->field('sale');
|
|
$show->field('status');
|
|
$show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
|
|
$show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
|
|
$show->field('verify_mobile','核销员手机');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new Product(), function (Form $form) {
|
|
//不允许编辑非自己数据
|
|
if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
$form->display('id');
|
|
|
|
$options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
|
|
$form->select('category_id')->options(array_slice($options, 1, null, true))->required();
|
|
$form->text('title')->required();
|
|
$form->text('price')->required();
|
|
$form->text('original_price')->required();
|
|
$form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
|
|
$form->text('stock')->default(9999)->required();
|
|
$form->editor('know');
|
|
$form->editor('content')->required();
|
|
$form->text('verify_mobile','核销员手机');
|
|
if ($form->isEditing()) {
|
|
$form->confirm('提示', '修改标题、价格、产品图片、旅游须知、产品详情需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
|
|
}
|
|
})->saving(function (Form $form) {
|
|
//不允许编辑非自己数据
|
|
if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
//不允许编辑的字段,忽略字段不起作用?
|
|
$form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
|
|
|
|
//null字段转为''
|
|
foreach ($form->input() as $k => $v) {
|
|
if (is_null($v)) {
|
|
$form->$k = '';
|
|
}
|
|
}
|
|
|
|
//特殊字段处理
|
|
$form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
|
|
$form->supplier_id = Admin::user()->id;
|
|
if ($form->isCreating()) {
|
|
$form->status = ProductStatus::UNAUDITED;
|
|
}
|
|
})->saved(function (Form $form, $result) {
|
|
if ($form->isEditing() && $result) {
|
|
$ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray();
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
//如果修改标题、价格、产品图片、旅游须知、产品详情,状态将变为未审核
|
|
if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) {
|
|
$form->model()->update(['status' => ProductStatus::UNAUDITED]);
|
|
|
|
//下架所有代理商产品,未审核的产品,不能同步信息到代理商产品
|
|
AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update(['status' => ProductStatus::SOLD_OUT]);
|
|
} else {
|
|
//不需要审核的修改信息同步信息到代理商产品,注:组合产品不同步
|
|
AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([
|
|
'title' => $form->title,
|
|
'pictures' => explode(',', $form->pictures),
|
|
'know' => $form->know,
|
|
'content' => $form->content,
|
|
]);
|
|
}
|
|
|
|
DB::commit();
|
|
return $form->response()->success('更新成功!')->script('history.go(-1)');
|
|
} catch (\Exception $exception) {
|
|
DB::rollBack();
|
|
return $form->response()->error($exception->getMessage());
|
|
}
|
|
}
|
|
})->deleting(function (Form $form) {
|
|
//不允许删除非自己的数据
|
|
if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
});
|
|
}
|
|
}
|