Skip to content

Commit 080934c

Browse files
committed
新增:博客批量导入支持
1 parent 7c1c5f0 commit 080934c

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

module/Blog/Admin/Controller/BlogController.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use ModStart\Admin\Concern\HasAdminQuickCRUD;
99
use ModStart\Admin\Layout\AdminCRUDBuilder;
1010
use ModStart\Core\Dao\ModelUtil;
11+
use ModStart\Core\Exception\BizException;
1112
use ModStart\Core\Input\InputPackage;
12-
use ModStart\Core\Input\Request;
1313
use ModStart\Core\Input\Response;
1414
use ModStart\Core\Util\CRUDUtil;
1515
use ModStart\Field\AbstractField;
@@ -19,15 +19,17 @@
1919
use ModStart\Grid\GridFilter;
2020
use ModStart\Repository\RepositoryUtil;
2121
use ModStart\Support\Concern\HasFields;
22+
use ModStart\Widget\ButtonDialogRequest;
2223
use ModStart\Widget\TextLink;
2324
use Module\Blog\Core\BlogSiteUrlBiz;
2425
use Module\Blog\Core\BlogSuperSearchBiz;
25-
use Module\Blog\Core\BlogTagManagerBiz;
26+
use Module\Blog\Model\Blog;
2627
use Module\Blog\Type\BlogVisitMode;
2728
use Module\Blog\Util\BlogCategoryUtil;
2829
use Module\Blog\Util\BlogTagUtil;
2930
use Module\Blog\Util\UrlUtil;
3031
use Module\Vendor\Provider\SiteUrl\SiteUrlProvider;
32+
use Module\Vendor\QuickRun\Export\ImportHandle;
3133

3234
class BlogController extends Controller
3335
{
@@ -97,6 +99,7 @@ protected function crud(AdminCRUDBuilder $builder)
9799
$filter->eq('isRecommend', '推荐')->autoHide(true)->switchRadioYesNo();
98100
$filter->eq('isPublished', '发布')->autoHide(true)->switchRadioYesNo();
99101
})
102+
->gridOperateAppend(ButtonDialogRequest::primary('<i class="iconfont icon-upload"></i> 批量导入', action('\\' . __CLASS__ . '@import')))
100103
->pageJumpEnable(true)
101104
->hookSaving(function (Form $form) use (&$updatedCategoryIds) {
102105
if ($form->itemId()) {
@@ -127,6 +130,41 @@ protected function crud(AdminCRUDBuilder $builder)
127130
BlogSuperSearchBiz::syncDelete($item->id);
128131
});
129132
})
130-
->title('博客管理');
133+
->title('博客文章');
134+
}
135+
136+
public function import(ImportHandle $handle)
137+
{
138+
$templateData = [];
139+
$templateData[] = [
140+
'测试文章', '1', '文章摘要', '<p>文章内容,支持富文本HTML</p>'
141+
];
142+
return $handle
143+
->withPageTitle('批量导入博客文章')
144+
->withTemplateName('博客文章')
145+
->withTemplateData($templateData)
146+
->withHeadTitles([
147+
'标题', '分类ID', '摘要', '内容',
148+
])
149+
->handleImport(function ($data, $param) {
150+
$title = empty($data[0]) ? null : $data[0];
151+
BizException::throwsIfEmpty('标题为空', $title);
152+
$blog = ModelUtil::get(Blog::class, [
153+
'title' => $title,
154+
]);
155+
$update = [];
156+
$update['categoryId'] = intval(empty($data[1]) ? null : $data[1]);
157+
$update['summary'] = empty($data[2]) ? null : $data[2];
158+
$update['content'] = empty($data[3]) ? null : $data[3];
159+
$update['isPublished'] = true;
160+
if ($blog) {
161+
ModelUtil::update(Blog::class, $blog['id'], $update);
162+
} else {
163+
$update['title'] = $title;
164+
ModelUtil::insert(Blog::class, $update);
165+
}
166+
return Response::generateSuccess();
167+
})
168+
->performExcel();
131169
}
132170
}

module/Blog/Admin/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Route::match(['get', 'post'], 'blog/blog/edit', 'BlogController@edit');
1919
Route::match(['post'], 'blog/blog/delete', 'BlogController@delete');
2020
Route::match(['get'], 'blog/blog/show', 'BlogController@show');
21+
Route::match(['get', 'post'], 'blog/blog/import', 'BlogController@import');
2122

2223
Route::match(['get', 'post'], 'blog/comment', 'BlogCommentController@index');
2324
Route::match(['get', 'post'], 'blog/comment/add', 'BlogCommentController@add');

module/Blog/Core/ModuleServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function boot(Dispatcher $events)
7878
'sort' => 100,
7979
'children' => [
8080
[
81-
'title' => '博客管理',
81+
'title' => '博客文章',
8282
'url' => '\Module\Blog\Admin\Controller\BlogController@index',
8383
],
8484
[

module/Blog/Docs/release.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 3.7.0 博客批量导入支持
2+
3+
- 新增:博客批量导入支持
4+
5+
---
6+
17
## 3.6.0 适配标签云,定时发布,链接异常修复
28

39
- 新增:适配支持标签云 TagManager 模块

module/Blog/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"博客"
1919
],
2020
"modstartVersion": ">=4.0.0",
21-
"version": "3.6.0",
21+
"version": "3.7.0",
2222
"author": "ModStart",
2323
"description": "提供一个基础的博客系统",
2424
"suggest": [

module/Vendor/Api/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/* @var \Illuminate\Routing\Router $router */
33
$middlewares = [];
4-
if (class_exists(\Module\Member\Middleware\ApiAuthMiddleware::class)) {
4+
if (@class_exists(\Module\Member\Middleware\ApiAuthMiddleware::class)) {
55
$middlewares[] = \Module\Member\Middleware\ApiAuthMiddleware::class;
66
}
77
$router->group([

0 commit comments

Comments
 (0)