Skip to content

Commit 337a66e

Browse files
committed
新增:内容区块模块
1 parent 882dc3d commit 337a66e

File tree

15 files changed

+502
-0
lines changed

15 files changed

+502
-0
lines changed

config/module.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
'VisitStatistic' => [
3535
'enable' => true,
3636
],
37+
'ContentBlock' => [
38+
'enable' => true,
39+
],
3740
// 'BlogAdminApi' => [
3841
// 'enable' => true,
3942
// ],
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
4+
namespace Module\ContentBlock\Admin\Controller;
5+
6+
7+
use Illuminate\Routing\Controller;
8+
use ModStart\Admin\Concern\HasAdminQuickCRUD;
9+
use ModStart\Admin\Layout\AdminCRUDBuilder;
10+
use ModStart\Field\AbstractField;
11+
use ModStart\Field\AutoRenderedFieldValue;
12+
use ModStart\Grid\GridFilter;
13+
use ModStart\Support\Concern\HasFields;
14+
use Module\ContentBlock\Model\ContentBlock;
15+
use Module\ContentBlock\Type\ContentBlockType;
16+
17+
class ContentBlockController extends Controller
18+
{
19+
use HasAdminQuickCRUD;
20+
21+
protected function crud(AdminCRUDBuilder $builder)
22+
{
23+
$builder
24+
->init(ContentBlock::class)
25+
->field(function ($builder) {
26+
/** @var HasFields $builder */
27+
28+
$builder->id('id', 'ID');
29+
$builder->text('name', '标识')->required();
30+
$builder->radio('type', '类型')
31+
->optionType(ContentBlockType::class)
32+
->defaultValue(ContentBlockType::IMAGE)
33+
->required()
34+
->when('=', ContentBlockType::IMAGE, function ($builder) {
35+
/** @var HasFields $builder */
36+
$builder->image('image', '封面');
37+
$builder->text('title', '标题');
38+
$builder->link('link', '链接');
39+
})
40+
->when('=', ContentBlockType::HTML, function ($builder) {
41+
/** @var HasFields $builder */
42+
$builder->richHtml('content', '内容')->htmlFilter(false);
43+
});
44+
45+
$builder->display('_content', '内容')
46+
->hookRendering(function (AbstractField $field, $item, $index) {
47+
return AutoRenderedFieldValue::makeView('module::ContentBlock.View.admin.content', [
48+
'item' => $item
49+
]);
50+
})
51+
->listable(true)->showable(false);
52+
53+
$builder->number('sort', '排序')->defaultValue(999)->help('数字越小越靠前')->required();
54+
$builder->datetime('startTime', '开始时间')->help('留空表示不限制');
55+
$builder->datetime('endTime', '结束时间')->help('留空表示不限制');
56+
$builder->switch('enable', '启用')->defaultValue(true)->required();
57+
58+
$builder->display('created_at', L('Created At'))->listable(false);
59+
$builder->display('updated_at', L('Updated At'))->listable(false);
60+
})
61+
->gridFilter(function (GridFilter $filter) {
62+
$filter->eq('id', L('ID'));
63+
$filter->eq('type', '类型')->select(ContentBlockType::class);
64+
$filter->eq('name', '标识');
65+
})
66+
->title('内容区块');
67+
}
68+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/* @var \Illuminate\Routing\Router $router */
4+
5+
$router->match(['get', 'post'], 'content_block/config', 'ConfigController@index');
6+
7+
$router->match(['get', 'post'], 'content_block/category', 'ContentBlockCategoryController@index');
8+
$router->match(['get', 'post'], 'content_block/category/add', 'ContentBlockCategoryController@add');
9+
$router->match(['get', 'post'], 'content_block/category/edit', 'ContentBlockCategoryController@edit');
10+
$router->match(['post'], 'content_block/category/delete', 'ContentBlockCategoryController@delete');
11+
$router->match(['get'], 'content_block/category/show', 'ContentBlockCategoryController@show');
12+
$router->match(['post'], 'content_block/category/sort', 'ContentBlockCategoryController@sort');
13+
14+
$router->match(['get', 'post'], 'content_block', 'ContentBlockController@index');
15+
$router->match(['get', 'post'], 'content_block/add', 'ContentBlockController@add');
16+
$router->match(['get', 'post'], 'content_block/edit', 'ContentBlockController@edit');
17+
$router->match(['post'], 'content_block/delete', 'ContentBlockController@delete');
18+
$router->match(['get'], 'content_block/show', 'ContentBlockController@show');
19+
$router->match(['post'], 'content_block/sort', 'ContentBlockController@sort');
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
4+
use Illuminate\Support\Facades\Cache;
5+
use ModStart\Core\Assets\AssetsUtil;
6+
7+
/**
8+
* @Util 内容区块
9+
*/
10+
class MContentBlock
11+
{
12+
const CACHE_KEY_PREFIX = 'ContentBlock:';
13+
14+
/**
15+
* @Util 根据name获取内容区块,60分钟缓存
16+
* @param $name string name
17+
* @return array|null
18+
*/
19+
public static function getCached($name)
20+
{
21+
return Cache::remember(self::CACHE_KEY_PREFIX . "Name:" . $name, 60, function () use ($name) {
22+
return self::get($name);
23+
});
24+
}
25+
26+
/**
27+
* @Util 根据id获取内容区块,60分钟缓存
28+
* @param $id integer ID
29+
* @return array|null
30+
*/
31+
public static function getByIdCached($id)
32+
{
33+
return Cache::remember(self::CACHE_KEY_PREFIX . "Id:" . $id, 60, function () use ($id) {
34+
return self::getById($id);
35+
});
36+
}
37+
38+
private static function getBy($where)
39+
{
40+
$m = \Module\ContentBlock\Model\ContentBlock::where($where)
41+
->where(function ($query) {
42+
$query->whereNull('startTime')
43+
->orWhere('startTime', '<=', date('Y-m-d H:i:s'));
44+
})
45+
->where(function ($query) {
46+
$query->whereNull('endTime')
47+
->orWhere('endTime', '>=', date('Y-m-d H:i:s'));
48+
})
49+
->orderBy('sort', 'asc')
50+
->first();
51+
if (empty($m)) {
52+
return null;
53+
}
54+
if ($m['image']) {
55+
$m['image'] = AssetsUtil::fixFull($m['image']);
56+
}
57+
return $m->toArray();
58+
}
59+
60+
public static function getById($id)
61+
{
62+
return self::getBy([
63+
'id' => $id,
64+
'enable' => true,
65+
]);
66+
}
67+
68+
public static function get($name)
69+
{
70+
return self::getBy([
71+
'name' => $name,
72+
'enable' => true,
73+
]);
74+
}
75+
76+
/**
77+
* @Util 根据name获取内容区块列表,60分钟缓存
78+
* @param $name string 名称
79+
* @param $limit int 最多返回多少个
80+
* @return array
81+
*/
82+
public static function allCached($name, $limit = 0)
83+
{
84+
return Cache::remember(self::CACHE_KEY_PREFIX . $name . ':' . $limit, 60, function () use ($name, $limit) {
85+
return self::all($name, $limit);
86+
});
87+
}
88+
89+
private static function allBy($where, $limit)
90+
{
91+
$query = \Module\ContentBlock\Model\ContentBlock::where($where)
92+
->where(function ($query) {
93+
$query->whereNull('startTime')
94+
->orWhere('startTime', '<=', date('Y-m-d H:i:s'));
95+
})
96+
->where(function ($query) {
97+
$query->whereNull('endTime')
98+
->orWhere('endTime', '>=', date('Y-m-d H:i:s'));
99+
})
100+
->orderBy('sort', 'asc');
101+
if ($limit > 0) {
102+
$query = $query->limit($limit);
103+
}
104+
$ms = $query->get()->toArray();
105+
AssetsUtil::recordsFixFullOrDefault($ms, ['image']);
106+
return $ms;
107+
}
108+
109+
public static function all($name, $limit = 0)
110+
{
111+
return self::allBy([
112+
'name' => $name,
113+
'enable' => true,
114+
], $limit);
115+
}
116+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Module\ContentBlock\Core;
4+
5+
use Illuminate\Events\Dispatcher;
6+
use Illuminate\Support\ServiceProvider;
7+
use ModStart\Admin\Config\AdminMenu;
8+
use ModStart\Module\ModuleClassLoader;
9+
10+
class ModuleServiceProvider extends ServiceProvider
11+
{
12+
/**
13+
* Bootstrap any application services.
14+
*
15+
* @return void
16+
*/
17+
public function boot(Dispatcher $events)
18+
{
19+
ModuleClassLoader::addClass('MContentBlock', __DIR__ . '/MContentBlock.php');
20+
21+
AdminMenu::register(function () {
22+
return [
23+
[
24+
'title' => '内容管理',
25+
'icon' => 'file',
26+
'sort' => 150,
27+
'children' => [
28+
[
29+
'title' => '内容区块',
30+
'url' => '\Module\ContentBlock\Admin\Controller\ContentBlockController@index',
31+
],
32+
]
33+
34+
],
35+
];
36+
});
37+
}
38+
39+
/**
40+
* Register any application services.
41+
*
42+
* @return void
43+
*/
44+
public function register()
45+
{
46+
47+
}
48+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## 模块介绍
2+
3+
「内容区块」是一个后台提供便捷的的内容块管理代码,方便前端调用展示
4+
5+
6+
```mind
7+
功能特性
8+
区块类型
9+
图片
10+
富文本
11+
功能配置
12+
开始时间
13+
结束时间
14+
启用/禁用
15+
```
16+
17+
## 区块类型
18+
19+
- `image`: 图片
20+
- `html`: 富文本
21+
22+
## 使用说明
23+
24+
### 获取一个区块内容
25+
26+
```php
27+
<?php $cb = \MContentBlock::getCached('标识'); ?>
28+
<p>类型:{{$cb['type']}}</p>
29+
<p>名称:{{$cb['name']}}</p>
30+
<p>标题:{{$cb['title']}}</p>
31+
<p>图片:{{$cb['image']}}</p>
32+
<p>链接:{{$cb['link']}}</p>
33+
<p>HTML:{{$cb['content']}}</p>
34+
```
35+
36+
### 获取多个区块内容
37+
38+
```php
39+
<?php $cbList = \MContentBlock::allCached('标识',5); ?>
40+
@foreach($cbLists as $cb)
41+
<p>类型:{{$cb['type']}}</p>
42+
<p>名称:{{$cb['name']}}</p>
43+
<p>标题:{{$cb['title']}}</p>
44+
<p>图片:{{$cb['image']}}</p>
45+
<p>链接:{{$cb['link']}}</p>
46+
<p>HTML:{{$cb['content']}}</p>
47+
@endsection
48+
```
49+
50+
{ADMIN_MENUS}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## 1.1.0 新增ID获取方法,接口文档信息完善
2+
3+
- 新增:增加根据ID获取内容块的方法
4+
- 优化:增加调用接口文档信息
5+
6+
---
7+
8+
## 1.0.0 内容区块
9+
10+
- 后台提供便捷的的内容块管理代码,方便前端调用展示
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
6+
class ContentBlockCreate extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
16+
Schema::create('content_block', function (Blueprint $table) {
17+
18+
$table->bigIncrements('id');
19+
$table->timestamps();
20+
21+
$table->string('type', 20)->nullable()->comment('');
22+
23+
$table->string('name', 50)->nullable()->comment('');
24+
$table->string('title', 200)->nullable()->comment('');
25+
$table->string('image', 200)->nullable()->comment('');
26+
$table->string('link', 200)->nullable()->comment('');
27+
$table->text('content')->nullable()->comment('');
28+
29+
$table->integer('sort')->nullable()->comment('');
30+
$table->dateTime('startTime')->nullable()->comment('');
31+
$table->dateTime('endTime')->nullable()->comment('');
32+
$table->tinyInteger('enable')->nullable()->comment('');
33+
34+
});
35+
36+
}
37+
38+
/**
39+
* Reverse the migrations.
40+
*
41+
* @return void
42+
*/
43+
public function down()
44+
{
45+
}
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Module\ContentBlock\Model;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class ContentBlock extends Model
8+
{
9+
protected $table = 'content_block';
10+
}

0 commit comments

Comments
 (0)