diff --git a/app/Admin/Controllers/SupplierController.php b/app/Admin/Controllers/SupplierController.php new file mode 100644 index 0000000..db8263b --- /dev/null +++ b/app/Admin/Controllers/SupplierController.php @@ -0,0 +1,140 @@ +disableDeleteButton(); + + //如果是审核页面,多加where条件判断 + if (strpos(Route::current()->uri, 'audit')) { + $grid->model()->where('status', UserStatus::UNAUDITED); + } + + $grid->column('id')->sortable(); + $grid->column('account'); + $grid->column('supplier_name'); + $grid->column('company_name'); + $grid->column('logo')->image(60, 60); + $grid->column('address'); + $grid->column('license_pic'); + $grid->column('director'); + $grid->column('contact_phone'); + $grid->column('created_at')->display(fn($v) => $v->format('Y-m-d')); + $grid->column('updated_at')->display(fn($v) => $v->format('Y-m-d')); + + $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->equal('supplier_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('account'); + $show->field('supplier_name'); + $show->field('company_name'); + $show->field('logo')->image(60, 60); + $show->field('address'); + $show->field('license_pic')->image(60, 60); + $show->field('director'); + $show->field('contact_phone'); + $show->field('created_at')->as(fn($v) => date('Y-m-d H:i:s', $v)); + $show->field('updated_at')->as(fn($v) => date('Y-m-d H:i:s', $v)); + }); + } + + /** + * 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('account')->required(); + $form->text('password')->required(); + } + else if ($form->isEditing()) { + $form->display('account'); + $form->text('password')->customFormat(fn() => ''); + } + $form->text('supplier_name')->required(); + $form->select('status', '状态') + ->options(UserStatus::array()) + ->default(UserStatus::NORMAL) + ->required(); + $form->text('company_name'); + $form->image('logo'); + $form->text('address'); + $form->image('license_pic'); + $form->text('director'); + $form->mobile('contact_phone'); + })->saving(function (Form $form) { + //不允许编辑的字段 + if ($form->isEditing()) { + $form->ignore(['id', 'account', 'created_at', 'created_at', 'deleted_at']); + } + + //过滤null字段 + foreach($form->input() as $k => $v) { + if (is_null($form->$k)) { + $form->$k = ''; + } + } + }); + } +} diff --git a/app/Admin/Extensions/Grid/AuditSupplier.php b/app/Admin/Extensions/Grid/AuditSupplier.php new file mode 100644 index 0000000..d188a2d --- /dev/null +++ b/app/Admin/Extensions/Grid/AuditSupplier.php @@ -0,0 +1,56 @@ +action = $action; //$action:1=通过;2=拒绝 + $this->title = $action == 1 ? '通过' : '拒绝'; + } + + protected function html() + { + $class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger'; + $this->appendHtmlAttribute('class', $class); + $this->defaultHtmlAttribute('href', 'javascript:;'); + + return "formatHtmlAttributes()}>{$this->title}"; + } + + public function handle(Request $request) + { + try { + $user = Supplier::find($this->getKey()); + $user->status = $request->action == 1 ? UserStatus::NORMAL : UserStatus::REFUSE; + $user->save(); + + return $this->response()->success("审核成功")->refresh(); + } catch (\Exception $e) { + return $this->response()->error($e->getMessage()); + } + } + + public function confirm() + { + return ['确定要'.$this->title.'该用户吗?', '']; + } + + public function parameters() + { + return ['action' => $this->action]; + } +} diff --git a/app/Admin/Repositories/Supplier.php b/app/Admin/Repositories/Supplier.php new file mode 100644 index 0000000..ec412e5 --- /dev/null +++ b/app/Admin/Repositories/Supplier.php @@ -0,0 +1,16 @@ +host . $value : ''; + } + + public function getLicensePicAttribute($value): string + { + return $value ? $this->host . $value : ''; + } + + public function setPasswordAttribute($value) + { + //新增时 + if ($value && empty($this->attributes['password'])) { + $this->attributes['password'] = $this->passMd5($value); + } + //编辑时 + else if ($value && !empty($this->attributes['password']) && $this->attributes['password'] != $value) { + $this->attributes['password'] = $this->passMd5($value); + } + } } diff --git a/resources/lang/zh_CN/supplier.php b/resources/lang/zh_CN/supplier.php new file mode 100644 index 0000000..ae8b1d2 --- /dev/null +++ b/resources/lang/zh_CN/supplier.php @@ -0,0 +1,20 @@ + [ + 'Supplier' => '供应商', + 'supplier' => '供应商', + ], + 'fields' => [ + 'account' => '账号', + 'password' => '密码', + 'supplier_name' => '供应商名称', + 'company_name' => '公司名称', + 'logo' => 'LOGO', + 'address' => '公司地址', + 'license_pic' => '营业执照', + 'director' => '负责人', + 'contact_phone' => '联系电话', + ], + 'options' => [ + ], +];