Files
2026-06-11 12:15:29 +08:00

168 lines
5.5 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\adminapi\logic\article;
use app\common\logic\BaseLogic;
use app\common\model\article\Article;
use app\common\service\ai\KbSyncService;
use app\common\service\FileService;
/**
* 资讯管理逻辑
* Class ArticleLogic
* @package app\adminapi\logic\article
*/
class ArticleLogic extends BaseLogic
{
/**
* @notes 添加资讯
* @param array $params
* @author heshihu
* @date 2022/2/22 9:57
*/
public static function add(array $params)
{
if ((int) ($params['is_show'] ?? 0) === 1 && !Article::hasPublishableContent($params['content'] ?? '')) {
self::setError('正文未拉取完成,暂不能发布,请先保存为稿子');
return false;
}
$article = Article::create([
'title' => $params['title'],
'desc' => $params['desc'] ?? '',
'author' => $params['author'] ?? '', //作者
'sort' => !empty($params['sort']) ? $params['sort'] : time(), // 排序
'abstract' => $params['abstract'], // 文章摘要
'click_virtual' => $params['click_virtual'] ?? 0,
'image' => $params['image'] ? FileService::setFileUrl($params['image']) : '',
'cid' => $params['cid'],
'is_show' => $params['is_show'],
'is_recommend' => $params['is_recommend'] ?? 0,
'content' => $params['content'] ?? '',
]);
KbSyncService::enqueue('article', 'article', (int) $article->id, 'upsert', 40);
return true;
}
/**
* @notes 编辑资讯
* @param array $params
* @return bool
* @author heshihu
* @date 2022/2/22 10:12
*/
public static function edit(array $params): bool
{
try {
if ((int) ($params['is_show'] ?? 0) === 1 && !Article::hasPublishableContent($params['content'] ?? '')) {
throw new \Exception('正文未拉取完成,暂不能发布,请先保存为稿子');
}
Article::update([
'id' => $params['id'],
'title' => $params['title'],
'desc' => $params['desc'] ?? '', // 简介
'author' => $params['author'] ?? '', //作者
'sort' => $params['sort'] ?? 0, // 排序
'abstract' => $params['abstract'], // 文章摘要
'click_virtual' => $params['click_virtual'] ?? 0,
'image' => $params['image'] ? FileService::setFileUrl($params['image']) : '',
'cid' => $params['cid'],
'is_show' => $params['is_show'],
'is_recommend' => $params['is_recommend'] ?? 0,
'content' => $params['content'] ?? '',
]);
KbSyncService::enqueue('article', 'article', (int) $params['id'], 'upsert', 40);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除资讯
* @param array $params
* @author heshihu
* @date 2022/2/22 10:17
*/
public static function delete(array $params)
{
Article::destroy($params['id']);
KbSyncService::enqueue('article', 'article', (int) $params['id'], 'delete', 20);
}
/**
* @notes 查看资讯详情
* @param $params
* @return array
* @author heshihu
* @date 2022/2/22 10:15
*/
public static function detail($params): array
{
return Article::findOrEmpty($params['id'])->toArray();
}
/**
* @notes 更改资讯状态
* @param array $params
* @return bool
* @author heshihu
* @date 2022/2/22 10:18
*/
public static function updateStatus(array $params)
{
$article = Article::findOrEmpty($params['id']);
if ($article->isEmpty()) {
self::setError('资讯不存在');
return false;
}
if ((int) $params['is_show'] === 1 && !Article::hasPublishableContent($article->content ?? '')) {
self::setError('正文未拉取完成,暂不能发布,请先保持稿子状态');
return false;
}
Article::update([
'id' => $params['id'],
'is_show' => $params['is_show']
]);
KbSyncService::enqueue(
'article',
'article',
(int) $params['id'],
(int) $params['is_show'] === 1 ? 'upsert' : 'delete',
20
);
return true;
}
public static function batchRecommend(array $params): bool
{
try {
Article::whereIn('id', $params['ids'])
->update(['is_recommend' => $params['is_recommend']]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
}