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.
		
		
		
		
		
			
		
			
				
					
					
						
							121 lines
						
					
					
						
							3.7 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							121 lines
						
					
					
						
							3.7 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\AdminAgent\Controllers;
							 | 
						|
								
							 | 
						|
								use App\AdminAgent\Renderable\SelectAgentProduct;
							 | 
						|
								use App\AdminAgent\Repositories\Article;
							 | 
						|
								use App\Models\AgentProduct;
							 | 
						|
								use Dcat\Admin\Admin;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								
							 | 
						|
								class ArticleController extends AdminController
							 | 
						|
								{
							 | 
						|
								    /**
							 | 
						|
								     * Make a grid builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Grid
							 | 
						|
								     */
							 | 
						|
								    protected function grid()
							 | 
						|
								    {
							 | 
						|
								        return Grid::make(new Article(), function (Grid $grid) {
							 | 
						|
											$grid->model()->where('agent_id', Admin::user()->id)
							 | 
						|
												->orderBy('sort')->orderBy('id', 'desc');
							 | 
						|
								
							 | 
						|
								            $grid->column('id')->sortable();
							 | 
						|
								            $grid->column('author');
							 | 
						|
								            $grid->column('title');
							 | 
						|
								            $grid->column('image')->image('', 60, 60);
							 | 
						|
								            $grid->column('sort')->editable()->width(120)->help('数字超小越靠前');
							 | 
						|
								            $grid->column('type')->switch()->help('开启将以大图方式显示,关闭以普通列表显示');
							 | 
						|
								            $grid->column('created_at');
							 | 
						|
								            $grid->column('updated_at')->sortable();
							 | 
						|
								
							 | 
						|
								            $grid->filter(function (Grid\Filter $filter) {
							 | 
						|
												$filter->panel();
							 | 
						|
								
							 | 
						|
								                $filter->equal('id')->width(2);
							 | 
						|
												$filter->like('title')->width(3);
							 | 
						|
								            });
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a show builder.
							 | 
						|
								     *
							 | 
						|
								     * @param mixed $id
							 | 
						|
								     *
							 | 
						|
								     * @return Show
							 | 
						|
								     */
							 | 
						|
								    protected function detail($id)
							 | 
						|
								    {
							 | 
						|
								        return Show::make($id, new Article(), function (Show $show) {
							 | 
						|
											//不允许查看非自己的数据
							 | 
						|
											if ($show->model()->agent_id != Admin::user()->id) {
							 | 
						|
												Admin::exit('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$show->field('id');
							 | 
						|
								            $show->field('author');
							 | 
						|
								            $show->field('title');
							 | 
						|
								            $show->field('image')->image('', 80, 80);
							 | 
						|
								            $show->field('content')->unescape();
							 | 
						|
								            $show->field('sort');
							 | 
						|
								            $show->field('type')->using(['普通列表', '大图显示']);
							 | 
						|
								            $show->field('created_at');
							 | 
						|
								            $show->field('updated_at');
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a form builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Form
							 | 
						|
								     */
							 | 
						|
								    protected function form()
							 | 
						|
								    {
							 | 
						|
								        return Form::make(new Article(), function (Form $form) {
							 | 
						|
											//不允许查看非自己的数据
							 | 
						|
											if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$form->display('id');
							 | 
						|
								            $form->text('author')->default(Admin::user()->name);
							 | 
						|
								            $form->text('title')->required();
							 | 
						|
								            $form->image('image')->required()->removable(false)->uniqueName();
							 | 
						|
								            $form->editor('content')->required();
							 | 
						|
								            $form->text('sort')->default(255);
							 | 
						|
								            $form->select('type')->options(['普通列表', '大图显示'])->default(0)->required();
							 | 
						|
											$form->multipleSelectTable('agent_product_ids')
							 | 
						|
												->dialogWidth('80%;min-width:825px;')
							 | 
						|
												->from(new SelectAgentProduct)
							 | 
						|
												->model(AgentProduct::class);
							 | 
						|
										})->saving(function (Form $form) {
							 | 
						|
											//不允许修改非自己的数据
							 | 
						|
											if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//特殊字段处理
							 | 
						|
											$form->hidden(['agent_id']);
							 | 
						|
											$form->agent_id = Admin::user()->id;
							 | 
						|
											if (array_key_exists('sort', $form->input())) {
							 | 
						|
												$form->sort = $form->sort ?? 255;
							 | 
						|
											}
							 | 
						|
											if (array_key_exists('author', $form->input())) {
							 | 
						|
												$form->author = $form->author ?? '';
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//不允许编辑的字段
							 | 
						|
											$form->ignore(['id', 'created_at', 'updated_at']);
							 | 
						|
										})->deleting(function (Form $form) {
							 | 
						|
											//不允许删除非自己的数据
							 | 
						|
											if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								}
							 |