201 lines
6.6 KiB
PHP
201 lines
6.6 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\FileService;
|
||
|
||
use app\common\model\user\{User,UserNotice};
|
||
|
||
/**
|
||
* 资讯管理逻辑
|
||
* 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)
|
||
{
|
||
$langs = $params['langs'];
|
||
|
||
foreach ($langs as &$content_lang) {
|
||
$content_lang['image'] = FileService::setFileUrl($content_lang['image']);
|
||
$content_lang['content'] = clear_file_domain($content_lang['content']);
|
||
}
|
||
|
||
$article = Article::create([
|
||
'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_popup' => $params['is_popup'],
|
||
'is_notice' => $params['is_notice'],
|
||
'is_sys_notice' => $params['is_sys_notice'],
|
||
'content' => $params['content'] ?? '',
|
||
'langs' => json_encode($langs, JSON_UNESCAPED_UNICODE),
|
||
]);
|
||
|
||
if($params['is_sys_notice'] == 1){
|
||
$users = User::select()->toArray();
|
||
foreach ($users as &$user) {
|
||
UserNotice::create([
|
||
'user_id' => $user['id'],
|
||
'article_id' => $article['id'],
|
||
'title' => $article['title'],
|
||
'content' => $article['content'] ?? '',
|
||
'langs' => $article['langs'],
|
||
]);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 编辑资讯
|
||
* @param array $params
|
||
* @return bool
|
||
* @author heshihu
|
||
* @date 2022/2/22 10:12
|
||
*/
|
||
public static function edit(array $params) : bool
|
||
{
|
||
try {
|
||
$langs = $params['langs'];
|
||
|
||
foreach ($langs as &$content_lang) {
|
||
$content_lang['image'] = FileService::setFileUrl($content_lang['image']);
|
||
$content_lang['content'] = clear_file_domain($content_lang['content']);
|
||
}
|
||
$article = 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_popup' => $params['is_popup'],
|
||
'is_notice' => $params['is_notice'],
|
||
'is_sys_notice' => $params['is_sys_notice'],
|
||
'content' => $params['content'] ?? '',
|
||
'langs' => json_encode($langs, JSON_UNESCAPED_UNICODE),
|
||
]);
|
||
|
||
if($params['is_sys_notice'] == 1){
|
||
$notices = UserNotice::where(['article_id' => $params['id']])->select()->toArray();
|
||
foreach ($notices as &$notice) {
|
||
UserNotice::update([
|
||
'id' => $notice['id'],
|
||
'title' => $article['title'],
|
||
'content' => $article['content'] ?? '',
|
||
'langs' => $article['langs'],
|
||
]);
|
||
}
|
||
}
|
||
|
||
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']);
|
||
}
|
||
|
||
/**
|
||
* @notes 查看资讯详情
|
||
* @param $params
|
||
* @return array
|
||
* @author heshihu
|
||
* @date 2022/2/22 10:15
|
||
*/
|
||
public static function detail($params) : array
|
||
{
|
||
$article = Article::findOrEmpty($params['id'])->toArray();
|
||
|
||
$langs = json_decode($article['langs'], true);
|
||
|
||
if(!empty($langs)){
|
||
foreach ($langs as &$content_lang) {
|
||
$content_lang['image'] = FileService::getFileUrl($content_lang['image']);
|
||
$content_lang['content'] = get_file_domain($content_lang['content']);
|
||
}
|
||
}else{
|
||
$langs = [];
|
||
}
|
||
$article['langs'] = $langs;
|
||
return $article;
|
||
}
|
||
|
||
/**
|
||
* @notes 更改资讯状态
|
||
* @param array $params
|
||
* @return bool
|
||
* @author heshihu
|
||
* @date 2022/2/22 10:18
|
||
*/
|
||
public static function updateStatus(array $params)
|
||
{
|
||
Article::update([
|
||
'id' => $params['id'],
|
||
'is_show' => $params['is_show']
|
||
]);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @notes 资讯列表
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2024/03/17 17:01
|
||
*/
|
||
public static function all($params) : array
|
||
{
|
||
$field = ['id', 'title'];
|
||
$articles = Article::field($field)
|
||
->where(['cid' => $params['cid'],'is_show' => 1])
|
||
->order(['sort' => 'desc','id' => 'desc'])
|
||
->select()->toArray();
|
||
return $articles;
|
||
}
|
||
} |