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.
195 lines
6.3 KiB
195 lines
6.3 KiB
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Extensions\Grid\AuditSupplier;
|
|
use App\Admin\Repositories\Supplier;
|
|
use App\Common\ProductStatus;
|
|
use App\Common\UserStatus;
|
|
use App\Models\Product;
|
|
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\Route;
|
|
|
|
class SupplierController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Supplier(), function (Grid $grid) {
|
|
$grid->disableDeleteButton();
|
|
$grid->disableRowSelector();
|
|
|
|
//如果是审核页面,多加where条件判断
|
|
if (strpos(Route::current()->uri, 'audit')) {
|
|
$grid->model()->where('status', UserStatus::UNAUDITED);
|
|
}
|
|
$grid->model()->where('id', '>', 1); //隐藏ID=1的领峰云管理员
|
|
|
|
$grid->column('id')->sortable();
|
|
$grid->column('username');
|
|
// $grid->column('name');
|
|
$grid->column('company_name');
|
|
$grid->column('logo')->image('', 60, 60);
|
|
$grid->column('address');
|
|
$grid->column('business_license')->image('', 60,60);
|
|
$grid->column('director');
|
|
$grid->column('contact_phone');
|
|
//$grid->column('rate')->editable()->help('分成百分比,如10%,则输入10');
|
|
$grid->column('created_at');
|
|
|
|
$grid->column('status', '状态')
|
|
->if(fn() => $this->status == UserStatus::UNAUDITED)
|
|
->display('')
|
|
->then(function ($column) {
|
|
$column->append((new AuditSupplier(null, 1))->setKey($this->id))->append(' ');
|
|
$column->append((new AuditSupplier(null, 2))->setKey($this->id));
|
|
})
|
|
->else()
|
|
->using(UserStatus::array())
|
|
->dot([
|
|
UserStatus::NORMAL => 'success',
|
|
UserStatus::UNAUDITED => '',
|
|
UserStatus::REFUSE => 'danger',
|
|
UserStatus::DISABLED => 'warning',
|
|
], 'primary');
|
|
|
|
$grid->filter(function ($filter) {
|
|
$filter->panel();
|
|
|
|
$filter->equal('id')->width(2);
|
|
$filter->like('company_name')->width(3);
|
|
$filter->equal('status', '用户状态')->select(UserStatus::array())->width(2);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Supplier(), function (Show $show) {
|
|
$show->disableDeleteButton();
|
|
|
|
$show->field('id');
|
|
$show->field('username');
|
|
$show->field('name');
|
|
$show->field('company_name');
|
|
$show->field('logo')->image('', 60, 60);
|
|
$show->field('address');
|
|
$show->field('business_license')->image('', 60, 60);
|
|
$show->field('director');
|
|
$show->field('contact_phone');
|
|
//$show->field('rate');
|
|
$show->field('publish_type')
|
|
->as(function ($value) {
|
|
if (!is_array($value)) return '';
|
|
$publish_type = admin_trans('product.options.publish_type');
|
|
$value = array_map(function ($v) use ($publish_type) {
|
|
return $publish_type[$v] ?? '';
|
|
}, $value);
|
|
return join(',', $value);
|
|
});
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new Supplier(), function (Form $form) {
|
|
$form->disableDeleteButton();
|
|
|
|
$form->display('id');
|
|
//账号只允许新增,不允许编辑
|
|
if ($form->isCreating()) {
|
|
$form->text('username')->required();
|
|
$form->text('password')->required();
|
|
}
|
|
else if ($form->isEditing()) {
|
|
$form->display('username');
|
|
$form->text('password')->minLength(6)->customFormat(fn() => '');
|
|
}
|
|
$form->text('name')->required();
|
|
$form->select('status', '状态')
|
|
->options(UserStatus::array())
|
|
->default(UserStatus::NORMAL)
|
|
->help('如果禁用供应商,其下的<span style="color:#f00;">所有产品都会跟着下架</span>,包括代理商在售的产品')
|
|
->required();
|
|
$form->text('company_name')->required();
|
|
$form->distpicker(['province_id', 'city_id', 'area_id'], '请选择区域')->required();
|
|
$form->image('logo')->removable(false)->uniqueName();
|
|
$form->text('address')->required();
|
|
$form->image('business_license')->removable(false)->uniqueName();
|
|
$form->text('director');
|
|
$form->mobile('contact_phone');
|
|
//$form->number('rate')->min(0)->max(100)->help('分成百分比,如10%,则输入10');
|
|
$form->checkbox('publish_type')
|
|
->options(admin_trans('product.options.publish_type'))
|
|
->default(0)
|
|
->help('供应商可发布的产品类型');
|
|
})->saving(function (Form $form) {
|
|
//判断账号是否唯一
|
|
if ($form->isCreating()) {
|
|
if ($form->repository()->model()->where('username', $form->username)->exists()) {
|
|
return $form->response()->error($form->username . ' 的账号已经存在');
|
|
}
|
|
}
|
|
|
|
//抽成比例
|
|
//if ($form->rate < 0 || $form->rate > 100) {
|
|
// return $form->response()->error('抽成比例在 0 ~ 100 之间');
|
|
//}
|
|
|
|
//不允许编辑的字段
|
|
if ($form->isEditing()) {
|
|
$form->ignore(['id', 'username', 'created_at', 'created_at', 'deleted_at', 'deleted_at']);
|
|
}
|
|
|
|
//过滤null字段
|
|
foreach($form->input() as $k => $v) {
|
|
if (is_null($form->$k)) {
|
|
$form->$k = '';
|
|
}
|
|
}
|
|
})->saved(function (Form $form) {
|
|
//禁用供应商将下架所有供应商的产品
|
|
if ($form->status == UserStatus::DISABLED) {
|
|
Product::where('supplier_id', $form->getKey())->update(['status' => ProductStatus::SOLD_OUT]);
|
|
}
|
|
|
|
//插入权限表
|
|
if ($form->status == UserStatus::NORMAL) {
|
|
DB::table(config('admin-supplier.database.role_users_table'))
|
|
->insertOrIgnore(['role_id' => 2, 'user_id' => $form->getKey()]);
|
|
}
|
|
})->deleting(function (Form $form) {
|
|
//禁止批量删除
|
|
if (strpos($form->getKey(), ',')) {
|
|
return $form->response()->error('操作禁止');
|
|
}
|
|
|
|
//禁止删除已发布有产品的供应商
|
|
if (Product::where('supplier_id', $form->getKey())->exists()) {
|
|
return $form->response()->error('该供应商已发布有产品,禁止删除');
|
|
}
|
|
});
|
|
}
|
|
}
|