作者 Richer

功能优化

<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 管理端控制层: Category 控制类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Controllers
* @package App\Admin\Controllers
* @author Richer <yangzi1028@163.com>
* @date 2023年4月20日14:26:14
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Controllers;
use App\Admin\Forms\FirstCategoryForm;
use App\Admin\Grids\FirstCategoryGrid;
use App\Jobs\Category\CategoryLabelCreatedJob;
use App\Models\Category\Category;
use App\Models\Category\CategoryExample;
use App\Models\Category\CategoryLabel;
use App\Models\Category\CategoryLabelItem;
use App\Models\Label;
use App\Models\LabelItem;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
/**
* Class FirstCategoriesController.
*
* @package App\Admin\Controllers
*/
class FirstCategoriesController extends BaseController
{
use FirstCategoryGrid, FirstCategoryForm;
/**
* FirstCategoriesController constructor.
*
* @param Category $model 注入model
*/
public function __construct(Category $model)
{
// 资源显示的中名称
$this->title = $model::OBJ_NAME_ZH;
// 是否可查看
$this->can_view = false;
// 是否可新增
$this->can_create = true;
// 是否可编辑
$this->can_edit = true;
// 是否可删除
$this->can_delete = true;
// 是否开启下拉菜单
$this->dropdownActions = false;
// 执行父类构造方法
parent::__construct($model);
}
/**
* 获取 实例
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|JsonResponse
*/
public function examples($id)
{
$list = null;
try {
// 获取数据,如果没有获取到抛出异常
$model = $this->model->findOrFail($id);
$list = $model->examples()->get();
return view('admin::category.examples', compact('id', 'list')) ;
} catch (ModelNotFoundException $e) {
return view('admin::category.examples', compact('id', 'list')) ;
}
}
/**
* 添加实例界面
*
* @param $id
*/
public function examplePage($id)
{
$example_id = request('example_id', 0);
$example = null;
if ($example_id) {
$example = CategoryExample::find($example_id);
}
// 获取数据,如果没有获取到抛出异常
return view('admin::category.example', compact('id', 'example_id','example')) ;
}
/**
* 添加站点
*
* @param integer $id 主键id
*
* @return JsonResponse
*/
public function addExample($id)
{
try {
$example_id = request('example_id', 0);
if ($example_id > 0) {
// 获取数据,如果没有获取到抛出异常
$model = CategoryExample::findOrFail($example_id);
$model->question = request('question');
$model->answer = request('answer');
$model->save();
} else {
// 获取数据,如果没有获取到抛出异常
$model = $this->model->findOrFail($id);
$model->examples()->create([
'question' => request('question'),
'answer' => request('answer'),
]);
}
return response()->json(
[
'code' => 0,
'status' => 1,
'message' => __('operate_succeeded'),
]
);
} catch (\Exception $e) {
return response()->json(
[
'code' => 1,
'status' => 0,
'message' => $e->getMessage(),
]
);
}
}
/**
* 线路更新站点的顺序
*
* @param integer $id 主键id
*
* @return JsonResponse
*/
public function deleteExample($id)
{
try {
// 获取数据,如果没有获取到抛出异常
$model = $this->model->findOrFail($id);
$result = $model->examples()->where('id', request('id'))->delete();
if ($result) {
return response()->json(
[
'code' => 0,
'status' => 1,
'message' =>__('operate_succeeded'),
]
);
}
return response()->json(
[
'code' => 1,
'status' => 1,
'message' =>__('操作失败。'),
]
);
} catch (\Exception $e) {
return response()->json(
[
'code' => 1,
'status' => 0,
'message' => $e->getMessage(),
]
);
}
}
/**
* 获取 实例
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|JsonResponse
*/
public function labels($id)
{
$list = null;
try {
// 获取数据,如果没有获取到抛出异常
$model = $this->model->findOrFail($id);
$list = $model->labels()->get();
return view('admin::category.labels', compact('id', 'list')) ;
} catch (ModelNotFoundException $e) {
return view('admin::category.labels', compact('id', 'list')) ;
}
}
/**
* 添加 标签 界面
*
* @param $id
*/
public function labelPage($id)
{
$label_id = request('label_id', 0);
$label = null;
if ($label_id) {
$label = CategoryLabel::find($label_id);
}
// 获取数据,如果没有获取到抛出异常
return view('admin::category.label', compact('id', 'label_id','label')) ;
}
/**
* 添加 标签
*
* @param integer $id 主键id
*
* @return JsonResponse
*/
public function addLabel($id)
{
try {
$label_id = request('label_id', 0);
if ($label_id > 0) {
// 获取数据,如果没有获取到抛出异常
$model = CategoryLabel::findOrFail($label_id);
$model->name = request('name');
$model->can_multiple = request('can_multiple', 0);
$model->semantics = request('semantics');
$model->description = request('description');
$model->save();
} else {
// 获取数据,如果没有获取到抛出异常
$model = $this->model->findOrFail($id);
$result = $model->labels()->create([
'name' => request('name'),
'semantics' => request('semantics'),
'description' => request('description'),
'can_multiple' => request('can_multiple', 0),
]);
// 同步到远程服务器
// CategoryLabelCreatedJob::dispatch($result);
}
return response()->json(
[
'code' => 0,
'status' => 1,
'message' => __('operate_succeeded'),
]
);
} catch (\Exception $e) {
return response()->json(
[
'code' => 1,
'status' => 0,
'message' => $e->getMessage(),
]
);
}
}
/**
* 删除 标签
*
* @param integer $id 主键id
*
* @return JsonResponse
*/
public function deleteLabel($id)
{
try {
// 获取数据,如果没有获取到抛出异常
$model = $this->model->findOrFail($id);
$result = $model->labels()->where('id', request('id'))->delete();
if ($result) {
return response()->json(
[
'code' => 0,
'status' => 1,
'message' =>__('operate_succeeded'),
]
);
}
return response()->json(
[
'code' => 1,
'status' => 1,
'message' =>__('操作失败。'),
]
);
} catch (\Exception $e) {
return response()->json(
[
'code' => 1,
'status' => 0,
'message' => $e->getMessage(),
]
);
}
}
/**
* 添加 标签明细 界面
*
* @param $id
*/
public function labelItemPage($id)
{
$label_item_id = request('label_item_id', 0);
$label_item = null;
if ($label_item_id) {
$label_item = CategoryExample::find($label_item_id);
}
// 获取数据,如果没有获取到抛出异常
return view('admin::category.label-item', compact('id', 'label_item_id','label_item')) ;
}
/**
* 添加 标签明细
*
* @param integer $id 主键id
*
* @return JsonResponse
*/
public function addLabelItem($id)
{
try {
$label_id = request('label_item_id', 0);
if ($label_id > 0) {
// 获取数据,如果没有获取到抛出异常
$model = CategoryLabelItem::findOrFail($label_id);
$model->name = request('name');
$model->description = request('description');
$model->save();
} else {
// 获取数据,如果没有获取到抛出异常
$model = CategoryLabel::findOrFail($id);
$model->items()->create([
'name' => request('name'),
'description' => request('description'),
]);
}
return response()->json(
[
'code' => 0,
'status' => 1,
'message' => __('operate_succeeded'),
]
);
} catch (\Exception $e) {
return response()->json(
[
'code' => 1,
'status' => 0,
'message' => $e->getMessage(),
]
);
}
}
/**
* 删除 标签明细
*
* @param integer $id 主键id
*
* @return JsonResponse
*/
public function deleteLabelItem($id)
{
try {
// 获取数据,如果没有获取到抛出异常
$result = CategoryLabelItem::where('id', request('id'))->delete();
if ($result) {
return response()->json(
[
'code' => 0,
'status' => 1,
'message' =>__('operate_succeeded'),
]
);
}
return response()->json(
[
'code' => 1,
'status' => 1,
'message' =>__('操作失败。'),
]
);
} catch (\Exception $e) {
return response()->json(
[
'code' => 1,
'status' => 0,
'message' => $e->getMessage(),
]
);
}
}
// /**
// * 获取 实例
// *
// * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|JsonResponse
// */
// public function labels($id)
// {
// $list = null;
// try {
// // 获取数据,如果没有获取到抛出异常
// $model = $this->model->findOrFail($id);
//
// $list = $model->labels()->get();
// return view('admin::category.labels', compact('id', 'list')) ;
//
// } catch (ModelNotFoundException $e) {
// return view('admin::category.labels', compact('id', 'list')) ;
// }
// }
/**
* 添加 标签 界面
*
* @param $id
*/
public function childPage($id)
{
$label_id = request('id', 0);
$label = null;
if ($label_id) {
$label = CategoryLabel::find($label_id);
}
// 获取数据,如果没有获取到抛出异常
return view('admin::category.child', compact('id', 'label_id','label')) ;
}
/**
* 添加 标签
*
* @param integer $id 主键id
*
* @return JsonResponse
*/
public function addChild($id)
{
try {
$label_id = request('label_id', 0);
if ($label_id > 0) {
// 获取数据,如果没有获取到抛出异常
$model = CategoryLabel::findOrFail($label_id);
$model->name = request('name');
$model->can_multiple = request('can_multiple', 0);
$model->semantics = request('semantics');
$model->description = request('description');
$model->save();
} else {
// 获取数据,如果没有获取到抛出异常
$model = $this->model->findOrFail($id);
$result = $model->labels()->create([
'name' => request('name'),
'semantics' => request('semantics'),
'description' => request('description'),
'can_multiple' => request('can_multiple', 0),
]);
// 同步到远程服务器
// CategoryLabelCreatedJob::dispatch($result);
}
return response()->json(
[
'code' => 0,
'status' => 1,
'message' => __('operate_succeeded'),
]
);
} catch (\Exception $e) {
return response()->json(
[
'code' => 1,
'status' => 0,
'message' => $e->getMessage(),
]
);
}
}
}
... ...
... ... @@ -40,19 +40,11 @@ trait CategoryForm
// $this->formDisplayField('id', __('ID'));
$this->formTextField('name', '', 20, 1);
$this->formSingleSelectField('pid', '一级分类', Category::getParents('store'))
->when('>=', 1, function () {
$this->formNumberField('consume_times')
->setGroupClass('col-sm-12')->setWidth(8, 2)
;
$this->formRadioField('context', '', ChatRecord::CONTEXT_OPTIONS)
->setGroupClass('col-sm-12')->setWidth(8, 2)
;
$this->formTextAreaField('start_statement', '', 0, 5)
->setGroupClass('col-sm-12')->setWidth(8, 2)
;
$this->formSingleSelectField('pid', '一级分类', Category::getParents());
});
$this->formNumberField('consume_times');
$this->formRadioField('context', '', ChatRecord::CONTEXT_OPTIONS);
$this->formTextAreaField('start_statement', '', 0, 5);
$required = 1;
if ($id > 0) {
$required = 0;
... ...
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 管理端表单 trait层:渲染生成 Category form 表单
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Forms
* @package App\Admin\Forms
* @author Richer <yangzi1028@163.com>
* @date 2023年4月20日14:26:14
* @copyright 2022-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Forms;
use App\Admin\Rewrite\Form;
use App\Models\Category\Category;
use App\Models\Chat\ChatRecord;
use Illuminate\Support\Str;
/**
* Trait FirstCategoryForm
*
* @package App\Admin\Forms\Category
*/
trait FirstCategoryForm
{
/**
* 渲染表单字段
*
* @param Int $id 用户id
*
* @return void
*/
public function renderFormFields($id)
{
// $this->formDisplayField('id', __('ID'));
$this->formTextField('name', '', 20, 1);
$required = 1;
if ($id > 0) {
$required = 0;
}
$this->formSingleImageField('icon', '图标', $required);
$this->formTextAreaField('body', '内容', 0, 5);
// $this->formTextAreaField('description', '', 255, 3);
$this->formCommonFields(1, 0, 1, 1);
}
/**
* 保存前回调
*
* @return void
*/
public function saving()
{
parent::saving();
//保存前回调
$this->form->saving(function (Form $form) {
//
});
}
/**
* 保存前回调
*
* @return void
*/
public function saved()
{
parent::saved();
//保存前回调
$this->form->saved(function (Form $form) {
$actions = request()->route()->getAction();
// 如果是以@store结尾则为新增操作
if (Str::endsWith($actions['uses'], '@store')) {
//
}
});
}
}
... ...
... ... @@ -51,7 +51,7 @@ trait CategoryGrid
// 执行父类方法,设置默认的查询条件
parent::setGridQuery();
$this->grid->model()->with(['parent', 'children'])->withCount(['labels','examples', 'children']);
$this->grid->model()->where('pid','>', 0)->with(['parent', 'children'])->withCount(['labels','examples', 'children']);
}
/**
... ... @@ -112,7 +112,7 @@ trait CategoryGrid
return '--';
}
return $value;
});
})->limit(100);
// $this->gridBoolField('context');
$this->gridTextField('context')->display(function ($value) {
if ($this->pid == 0) {
... ...
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 管理端列表grid trait层:渲染生成 Category grid 列表
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Grids
* @package App\Admin\Grids
* @author Richer <yangzi1028@163.com>
* @date 2023年4月20日14:26:14
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Grids;
use App\Admin\Extensions\CustomActions;
use App\Admin\Grids\Modal\CategoryExampleModal;
use App\Models\Category\Category;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Table;
use function Clue\StreamFilter\fun;
/**
* Class FirstCategoryGrid.
*
* @package App\Admin\Grids
*/
trait FirstCategoryGrid
{
/**
* 设置默认排序规则
*/
public function setGridOrder()
{
if (!request('_sort')) {
$this->grid->model()->orderByDesc('sort')->latest();
} else {
$this->setGridOrderSpecial();
}
}
/**
* 设置默认查询条件
* 增加数据权限的判断
*/
public function setGridQuery()
{
// 执行父类方法,设置默认的查询条件
parent::setGridQuery();
$this->grid->model()->where(function ($query) {
$query->whereNull('pid')->orWhere('pid', 0)->orWhere('pid', '');
});//with(['parent', 'children'])->withCount(['labels','examples', 'children']);
}
/**
* 自定义每行的操作
* 根据每个模块设置的权限来判断当前的操作
*
* @param $actions
* @return void $grid grid
*/
public function setRowCustomActions($actions)
{
// 当前数据的主键
$id = $actions->getKey();
// 当前的url
$url = $actions->getResource();
// if (!$actions->row->pid) {
//// $actions->append(CustomActions::renderChildCategory($id, $url));
// } else {
// $actions->append(CustomActions::renderAddExample($id, $url));
// $actions->append(CustomActions::renderAddLabel($id, $url));
// }
}
/**
* 为grid增加筛选条件
*
* @return Grid
*/
public function renderGridFilter()
{
$this->grid->filter(
function ($filter) {
// 筛选条件默认展开
$filter->expand();
$this->filterText($filter, 'name');
// $this->filterSingleSelect($filter, 'pid', '一级分类', Category::getParents());
//去掉默认的搜索
$filter->disableIdFilter();
}
);
return $this->grid;
}
/**
* 渲染grid字段
*
* @return void
*/
public function renderGridFields()
{
$this->gridRowNo();
$this->gridSingleImageField('icon');
$this->gridTextField('name');
$this->gridCommonFields(1, 0, 1, 1);
}
}
... ...
... ... @@ -214,6 +214,16 @@ Route::group([
| AI分类 模块
|-------------------------------------------------------------------------------------------------------------------
*/
$router->resource('first-categories', 'FirstCategoriesController');
$router->group(['prefix' => 'first-categories'], function ($router){
});
/*
|-------------------------------------------------------------------------------------------------------------------
| AI分类 模块
|-------------------------------------------------------------------------------------------------------------------
*/
$router->resource('categories', 'CategoriesController');
$router->group(['prefix' => 'categories'], function ($router){
$router->get('{id}/example', 'CategoriesController@examplePage')->where('id', '[0-9]+');
... ... @@ -230,10 +240,6 @@ Route::group([
$router->get('{id}/children', 'CategoriesController@children')->where('id', '[0-9]+');
$router->post('{id}/children', 'CategoriesController@addChild')->where('id', '[0-9]+');
$router->delete('{id}/labels', 'CategoriesController@deleteLabel')->where('id', '[0-9]+');
// $router->get('{id}/label-item', 'CategoriesController@labelItemPage')->where('id', '[0-9]+');
// $router->post('{id}/label-items', 'CategoriesController@addLabelItem')->where('id', '[0-9]+');
// $router->delete('{id}/label-items', 'CategoriesController@deleteLabelItem')->where('id', '[0-9]+');
});
/*
... ...
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `permission`, `visible`, `created_at`, `updated_at`, `deleted_at`) VALUES (NULL, 12, 4, '分类列表', 'fa-tag', '/first-categories', '*', 1, '2023-5-18 13:58:42', '2023-05-18 13:57:31', NULL);
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `permission`, `visible`, `created_at`, `updated_at`, `deleted_at`) VALUES (NULL, 12, 5, '场景列表', 'fa-tags', '/categories', '*', 1, '2023-5-18 13:58:42', '2023-5-18 13:58:42', NULL);
... ...
不能预览此文件类型