diff --git a/app/helpers.php b/app/helpers.php index c57bedf..41f30ad 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -22,4 +22,54 @@ function make_excerpt($value, $length = 200) { $excerpt = trim(preg_replace('/\r\n|\r|\n+/', ' ', strip_tags($value))); return str_limit($excerpt, $length); +} + +/** + * @param $title + * @param $model + * @return string + */ +function model_admin_link($title, $model) +{ + return model_link($title, $model, 'admin'); +} + +/** + * @param $title + * @param $model + * @param string $prefix + * @return string + */ +function model_link($title, $model, $prefix = '') +{ + // 获取数据模型的复数蛇形命名 + $model_name = model_plural_name($model); + + // 初始化前缀 + $prefix = $prefix ? "/$prefix/" : '/'; + + // 使用站点 URL 拼接全量 URL + $url = config('app.url') . $prefix . $model_name . '/' . $model->id; + + // 拼接 HTML A 标签,并返回 + return '' . $title . ''; +} + +/** + * @param $model + * @return string + */ +function model_plural_name($model) +{ + // 从实体中获取完整类名,例如:App\Models\User + $full_class_name = get_class($model); + + // 获取基础类名,例如:传参 `App\Models\User` 会得到 `User` + $class_name = class_basename($full_class_name); + + // 蛇形命名,例如:传参 `User` 会得到 `user`, `FooBar` 会得到 `foo_bar` + $snake_case_name = snake_case($class_name); + + // 获取子串的复数形式,例如:传参 `user` 会得到 `users` + return str_plural($snake_case_name); } \ No newline at end of file diff --git a/config/administrator.php b/config/administrator.php index 310afb8..77f4a5c 100644 --- a/config/administrator.php +++ b/config/administrator.php @@ -58,7 +58,14 @@ return array( 'menu' => [ '用户与权限' => [ 'users', + 'roles', + 'permissions' ], + '内容管理' => [ + 'categories', + 'topics', + 'replies' + ] ], diff --git a/config/administrator/categories.php b/config/administrator/categories.php new file mode 100644 index 0000000..1ab2794 --- /dev/null +++ b/config/administrator/categories.php @@ -0,0 +1,54 @@ + '分类', + 'single' => '分类', + 'model' => \App\Models\Category::class, + 'action_permissions' => [ + 'delete' => function () { + return Auth::user()->hasRole('Founder'); + } + ], + 'columns' => [ + 'id' => [ + 'title' => 'ID', + ], + 'name' => [ + 'title' => '名称', + 'sortable' => false + ], + 'description' => [ + 'title' => '描述', + 'sortable' => false, + ], + 'operation' => [ + 'title' => '管理', + 'sortable' => false, + ], + ], + 'edit_fields' => [ + 'name' => ['title' => '名称'], + 'description' => [ + 'title' => '描述', + 'type' => 'textarea', + ] + ], + 'filters' => [ + 'id' => ['title' => '分类ID'], + 'name' => ['title' => '名称'], + 'description' => ['title' => '描述'] + ], + 'rules' => [ + 'name' => 'required|min:1|unique:categories' + ], + 'messages' => [ + 'name.unique' => '分类名在数据库里有重复,请选用其他名称。', + 'name.required' => '请确保名字至少一个字符以上' + ] +]; \ No newline at end of file diff --git a/config/administrator/replies.php b/config/administrator/replies.php new file mode 100644 index 0000000..8c7a07a --- /dev/null +++ b/config/administrator/replies.php @@ -0,0 +1,98 @@ + '回复', + 'single' => '回复', + 'model' => Reply::class, + + 'columns' => [ + + 'id' => [ + 'title' => 'ID', + ], + 'content' => [ + 'title' => '内容', + 'sortable' => false, + 'output' => function ($value, $model) { + return '
' . $value . '
'; + }, + ], + 'user' => [ + 'title' => '作者', + 'sortable' => false, + 'output' => function ($value, $model) { + $avatar = $model->user->avatar; + $value = empty($avatar) ? 'N/A' : ' ' . $model->user->name; + return model_link($value, $model); + }, + ], + 'topic' => [ + 'title' => '话题', + 'sortable' => false, + 'output' => function ($value, $model) { + return '
' . model_admin_link($model->topic->title, $model->topic) . '
'; + }, + ], + 'operation' => [ + 'title' => '管理', + 'sortable' => false, + ], + ], + 'edit_fields' => [ + 'user' => [ + 'title' => '用户', + 'type' => 'relationship', + 'name_field' => 'name', + 'autocomplete' => true, + 'search_fields' => array("CONCAT(id, ' ', name)"), + 'options_sort_field' => 'id', + ], + 'topic' => [ + 'title' => '话题', + 'type' => 'relationship', + 'name_field' => 'title', + 'autocomplete' => true, + 'search_fields' => array("CONCAT(id, ' ', title)"), + 'options_sort_field' => 'id', + ], + 'content' => [ + 'title' => '回复内容', + 'type' => 'textarea', + ], + ], + 'filters' => [ + 'user' => [ + 'title' => '用户', + 'type' => 'relationship', + 'name_field' => 'name', + 'autocomplete' => true, + 'search_fields' => array("CONCAT(id, ' ', name)"), + 'options_sort_field' => 'id', + ], + 'topic' => [ + 'title' => '话题', + 'type' => 'relationship', + 'name_field' => 'title', + 'autocomplete' => true, + 'search_fields' => array("CONCAT(id, ' ', title)"), + 'options_sort_field' => 'id', + ], + 'content' => [ + 'title' => '回复内容', + ], + ], + 'rules' => [ + 'content' => 'required' + ], + 'messages' => [ + 'content.required' => '请填写回复内容', + ], +]; \ No newline at end of file diff --git a/config/administrator/topics.php b/config/administrator/topics.php new file mode 100644 index 0000000..148d49f --- /dev/null +++ b/config/administrator/topics.php @@ -0,0 +1,110 @@ + '话题', + 'single' => '话题', + 'model' => \App\Models\Topic::class, + 'columns' => [ + 'id' => [ + 'title' => 'ID', + ], + 'title' => [ + 'title' => '话题', + 'sortable' => false, + 'output' => function ($value, $model) { + return '
' . Model_link(e($value), $model) . '
'; + }, + ], + 'user' => [ + 'title' => '作者', + 'sortable' => false, + 'output' => function ($value, $model) { + $avatar = $model->user->avatar; + $value = empty($avatar) ? 'N/A' : ' ' . $model->user->name; + return model_link($value, $model); + }, + ], + 'category' => [ + 'title' => '分类', + 'sortable' => false, + 'output' => function ($value, $model) { + return model_admin_link($model->category->name, $model->category); + }, + ], + + 'reply_count' => [ + 'title' => '评论', + ], + 'operation' => [ + 'title' => '管理', + 'sortable' => false, + ], + ], + + 'edit_fields' => [ + 'title' => [ + 'title' => '标题', + ], + 'user' => [ + 'title' => '用户', + 'type' => 'relationship', + 'name_field' => 'name', + + // 自动补全,对于大数据量的对应关系,推荐开启自动补全, + // 可防止一次性加载对系统造成负担 + 'autocomplete' => true, + + // 自动补全的搜索字段 + 'search_fields' => ["CONCAT(id, ' ', name)"], + + // 自动补全排序 + 'options_sort_field' => 'id', + ], + 'category' => [ + 'title' => '分类', + 'type' => 'relationship', + 'name_field' => 'name', + 'search_fields' => ["CONCAT(id, ' ', name)"], + 'options_sort_field' => 'id', + ], + 'reply_count' => [ + 'title' => '评论', + ], + 'view_count' => [ + 'title' => '查看', + ], + ], + + 'filters' => [ + 'id' => [ + 'title' => '内容 ID', + ], + 'user' => [ + 'title' => '用户', + 'type' => 'relationship', + 'name_field' => 'name', + 'autocomplete' => true, + 'search_fields' => array("CONCAT(id, ' ', name)"), + 'options_sort_field' => 'id', + ], + 'category' => [ + 'title' => '分类', + 'type' => 'relationship', + 'name_field' => 'name', + 'search_fields' => array("CONCAT(id, ' ', name)"), + 'options_sort_field' => 'id', + ], + ], + 'rules' => [ + 'title' => 'required' + ], + 'messages' => [ + 'title.required' => '请填写标题', + ], +]; \ No newline at end of file diff --git a/database/seeds/UsersTableSeeder.php b/database/seeds/UsersTableSeeder.php index 52268da..1321edb 100644 --- a/database/seeds/UsersTableSeeder.php +++ b/database/seeds/UsersTableSeeder.php @@ -42,7 +42,7 @@ class UsersTableSeeder extends Seeder $user->password = bcrypt('123456'); $user->save(); $user->assignRole('Maintainer'); - $user = \App\Models\User::find(3); + $user = \App\Models\User::find(1); $user->assignRole('Founder'); }