Browse Source
feat(article): 新增投稿文章管理功能
feat(article): 新增投稿文章管理功能
- 添加 CreationArticle 模型并定义与 ArticleCategory 的关联关系 - 创建 CreationArticleController 控制器实现增删改查接口 - 实现 CreationArticleService 服务层处理业务逻辑 - 添加 CreationArticleValidate 验证器确保数据合法性 - 在路由配置中注册文章管理相关接口地址 - 支持文章列表筛选、详情查看及软删除操作 - 使用事务保证数据一致性,增强异常处理机制master
5 changed files with 240 additions and 1 deletions
-
64app/controller/CreationArticleController.php
-
4app/model/CreationArticle.php
-
17app/route/route.php
-
131app/service/CreationArticleService.php
-
25app/validate/CreationArticleValidate.php
@ -0,0 +1,64 @@ |
|||
<?php |
|||
namespace app\controller; |
|||
|
|||
use app\service\CreationArticleService; |
|||
use app\validate\CreationArticleValidate; |
|||
use plugin\piadmin\app\utils\ArrayUtils; |
|||
use support\Response; |
|||
|
|||
class CreationArticleController |
|||
{ |
|||
public function save(CreationArticleService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'title' => '', |
|||
'article_category_id' => '', |
|||
'cover' => '', |
|||
'content' => '', |
|||
'status' => '', |
|||
]); |
|||
validate(CreationArticleValidate::class)->check($params, 'save'); |
|||
return success($service->saveData($params)); |
|||
} |
|||
|
|||
public function update(CreationArticleService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'id' => '', |
|||
'title' => '', |
|||
'article_category_id' => '', |
|||
'cover' => '', |
|||
'content' => '', |
|||
'status' => '', |
|||
]); |
|||
validate(CreationArticleValidate::class)->check($params, 'update'); |
|||
return success($service->updateData(ArrayUtils::filterNotEmpty($params))); |
|||
} |
|||
|
|||
public function index(CreationArticleService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'article_category' => '', |
|||
'task' => '', |
|||
'title' => '', |
|||
'status' => '', |
|||
'begin_time' => '', |
|||
'end_time' => '' |
|||
]); |
|||
return success($service->listData($params)); |
|||
} |
|||
|
|||
public function read(CreationArticleService $service): Response |
|||
{ |
|||
$id = input('id'); |
|||
return success($service->readData($id)); |
|||
} |
|||
|
|||
public function delete(CreationArticleService $service): Response |
|||
{ |
|||
$ids = input('ids'); |
|||
return success($service->deleteData($ids)); |
|||
} |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,131 @@ |
|||
<?php |
|||
|
|||
namespace app\service; |
|||
|
|||
use app\dao\ArticleCategoryDao; |
|||
use app\dao\CreationArticleDao; |
|||
use app\dao\CreationTaskDao; |
|||
use plugin\piadmin\app\base\BaseService; |
|||
use plugin\piadmin\app\exception\ApiException; |
|||
use support\think\Db; |
|||
|
|||
class CreationArticleService extends BaseService |
|||
{ |
|||
|
|||
protected $dao; |
|||
protected $articleCategoryDao; |
|||
protected $taskDao; |
|||
|
|||
public function __construct(CreationArticleDao $dao) |
|||
{ |
|||
$this->dao = $dao; |
|||
$this->articleCategoryDao = app()->make(ArticleCategoryDao::class); |
|||
$this->taskDao = app()->make(CreationTaskDao::class); |
|||
} |
|||
|
|||
/** |
|||
* 保存信息 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function saveData(array $params): array |
|||
{ |
|||
Db::startTrans(); |
|||
try { |
|||
$data = $this->dao->save($params); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return $data->toArray(); |
|||
} |
|||
|
|||
/** |
|||
* 修改信息 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function updateData(array $params): array |
|||
{ |
|||
// 落库
|
|||
Db::startTrans(); |
|||
try { |
|||
$this->dao->update(['id' => $params['id']], $params); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return $params; |
|||
} |
|||
|
|||
/** |
|||
* 获取列表 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function listData(array $params): array |
|||
{ |
|||
$query = [ |
|||
'delete_time' => 0 |
|||
]; |
|||
if (isNotBlank($params['article_category'])) { |
|||
$cids = $this->articleCategoryDao->getColumn([['name', 'like', '%' . $params['article_category'] . '%']], 'id'); |
|||
$query[] = ['article_category_id', 'in', $cids]; |
|||
} |
|||
if (isNotBlank($params['task'])) { |
|||
$tids = $this->taskDao->getColumn([['name', 'like', '%' . $params['task'] . '%']], 'id'); |
|||
$query[] = ['task_id', 'in', $tids]; |
|||
} |
|||
if (isNotBlank($params['title'])) { |
|||
$query[] = ['title', 'like', '%' . $params['title'] . '%']; |
|||
} |
|||
if (isNotBlank($params['status'])) { |
|||
$query[] = ['status', '=', $params['status']]; |
|||
} |
|||
if (isNotBlank($params['begin_time'])) { |
|||
$query[] = ['create_time', '>=', strtotime($params['begin_time'])]; |
|||
} |
|||
if (isNotBlank($params['end_time'])) { |
|||
$query[] = ['create_time', '<=', strtotime($params['end_time'] . ' 23:59:59')]; |
|||
} |
|||
$list = $this->dao->getList($query, 'id,title,status,create_time,article_category_id', 0, 0, 'id DESC', ['category']); |
|||
$count = $this->dao->getCount($query); |
|||
return compact('list', 'count'); |
|||
} |
|||
|
|||
/** |
|||
* 获取信息 |
|||
* @param mixed $id |
|||
* @return array |
|||
*/ |
|||
public function readData(mixed $id): array |
|||
{ |
|||
$aiCommand = $this->dao->get(['id' => $id]); |
|||
if (empty($aiCommand)) { |
|||
throw new ApiException('数据不存在'); |
|||
} |
|||
return $aiCommand->toArray(); |
|||
} |
|||
|
|||
/** |
|||
* 删除信息 |
|||
* @param mixed $id |
|||
* @return array |
|||
*/ |
|||
public function deleteData(array $ids): array |
|||
{ |
|||
// 落库
|
|||
Db::startTrans(); |
|||
try { |
|||
$this->dao->update([['id', 'in', $ids]], ['delete_time' => time()]); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return ['id' => $ids]; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
<?php |
|||
|
|||
namespace app\validate; |
|||
|
|||
use plugin\piadmin\app\base\BaseValidate; |
|||
|
|||
class CreationArticleValidate extends BaseValidate |
|||
{ |
|||
protected $group = [ |
|||
'save' => [ |
|||
'title' => 'require', |
|||
'content' => 'require' |
|||
], |
|||
'update' => [ |
|||
'id' => 'require', |
|||
'title' => 'require', |
|||
'content' => 'require' |
|||
], |
|||
]; |
|||
protected $message = [ |
|||
'id.require' => 'ID不能为空', |
|||
'title' => '标题不能为空', |
|||
'content' => '内容不能为空' |
|||
]; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue