8 changed files with 174 additions and 17 deletions
			
			
		- 
					5MySQL_change.sql
 - 
					111app/AdminAgent/Controllers/ArticleController.php
 - 
					16app/AdminAgent/Repositories/Article.php
 - 
					1app/AdminAgent/routes.php
 - 
					26app/Http/Controllers/Api/ArticleController.php
 - 
					8dcat_admin_ide_helper.php
 - 
					22resources/lang/zh_CN/article.php
 - 
					2resources/lang/zh_CN/product.php
 
@ -0,0 +1,111 @@ | 
				
			|||
<?php | 
				
			|||
 | 
				
			|||
namespace App\AdminAgent\Controllers; | 
				
			|||
 | 
				
			|||
use App\AdminAgent\Repositories\Article; | 
				
			|||
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')->using(['普通列表', '大图显示']); | 
				
			|||
            $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(); | 
				
			|||
            $form->editor('content')->required(); | 
				
			|||
            $form->text('sort')->default(255); | 
				
			|||
            $form->select('type')->options(['普通列表', '大图显示'])->default(0)->required(); | 
				
			|||
		})->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; | 
				
			|||
			$form->sort = $form->sort ?? 255; | 
				
			|||
			$form->type = $form->type ? 1 : 0; | 
				
			|||
 | 
				
			|||
			//不允许编辑的字段
 | 
				
			|||
			$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('数据不存在'); | 
				
			|||
			} | 
				
			|||
		}); | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
@ -0,0 +1,16 @@ | 
				
			|||
<?php | 
				
			|||
 | 
				
			|||
namespace App\AdminAgent\Repositories; | 
				
			|||
 | 
				
			|||
use App\Models\Article as Model; | 
				
			|||
use Dcat\Admin\Repositories\EloquentRepository; | 
				
			|||
 | 
				
			|||
class Article extends EloquentRepository | 
				
			|||
{ | 
				
			|||
    /** | 
				
			|||
     * Model. | 
				
			|||
     * | 
				
			|||
     * @var string | 
				
			|||
     */ | 
				
			|||
    protected $eloquentClass = Model::class; | 
				
			|||
} | 
				
			|||
@ -0,0 +1,22 @@ | 
				
			|||
<?php | 
				
			|||
return [ | 
				
			|||
    'labels' => [ | 
				
			|||
        'Article' => '文章', | 
				
			|||
        'article' => '文章', | 
				
			|||
    ], | 
				
			|||
    'fields' => [ | 
				
			|||
        'agent_id' => '代理商ID', | 
				
			|||
        'author' => '作者', | 
				
			|||
        'title' => '文章标题', | 
				
			|||
        'image' => '文章缩略图', | 
				
			|||
        'content' => '文章内容', | 
				
			|||
        'sort' => '排序', | 
				
			|||
        'type' => '显示方式', | 
				
			|||
    ], | 
				
			|||
    'options' => [ | 
				
			|||
    	'type' => [ | 
				
			|||
    		0 => '普通列表', | 
				
			|||
			1 => '大图显示', | 
				
			|||
		], | 
				
			|||
    ], | 
				
			|||
]; | 
				
			|||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue