lihai-oa/app/api/controller/ArticleIndex.php

122 lines
4.1 KiB
PHP
Raw Permalink Normal View History

2023-10-30 17:37:45 +08:00
<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\api\controller;
use app\api\ApiController;
use app\api\middleware\Auth;
use app\article\model\Article as ArticleList;
use app\article\validate\ArticleCheck;
use think\exception\ValidateException;
use think\facade\Db;
class ArticleIndex extends ApiController
{
protected $middleware = [
Auth::class => ['except' => []]
];
//查看文章列表
public function index()
{
2023-10-30 17:40:34 +08:00
$this->checkAuth();
2023-10-30 17:37:45 +08:00
$param = get_params();
$uid = JWT_UID;
$loginAdmin = Db::name('Admin')->where(['id' => $uid])->find();
$did = $loginAdmin['did'];
$where = array();
$whereOr = array();
if (!empty($param['keyword'])) {
$where[] = ['a.id|a.title|a.keywords|a.desc|a.content|c.title', 'like', '%' . $param['keyword'] . '%'];
}
if (!empty($param['cate_id'])) {
$where[] = ['a.cate_id', '=', $param['cate_id']];
}
$where[] = ['a.delete_time', '=', 0];
$whereOr[] = ['a.is_share', '=', 1];
$whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$did}',a.share_dids)")];
$whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',a.share_uids)")];
$rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
$content = ArticleList::where($where)
->where(function ($query) use($whereOr) {
$query->whereOr($whereOr);
})
->field('a.*,a.id as id,c.title as cate_title,a.title as title,d.title as department,u.name as user')
->alias('a')
->join('article_cate c', 'a.cate_id = c.id')
->join('admin u', 'a.uid = u.id','LEFT')
->join('department d', 'a.did = d.id','LEFT')
->order('a.create_time desc')
->paginate($rows, false, ['query' => $param]);
$this->apiSuccess('获取成功', $content);
}
2023-11-04 13:37:27 +08:00
//查看文章分类
public function cate()
{
$cate = Db::name('ArticleCate')->order('id desc')->select()->toArray();
$this->apiSuccess('获取成功', $cate);
}
2023-10-30 17:37:45 +08:00
//查看文章详情
public function view()
{
2023-10-30 17:40:34 +08:00
$this->checkAuth();
2023-10-30 17:37:45 +08:00
$id = get_params("id");
if (empty($id)) {
$this->apiError("文章id不能为空");
}
$uid = JWT_UID;
$loginAdmin = Db::name('Admin')->where(['id' => $uid])->find();
$did = $loginAdmin['did'];
$detail = (new ArticleList())->detail($id);
$share_uids = [];
if(!empty($detail['share_uids'])){
$share_uids = explode(',', $detail['share_uids']);
}
$share_dids = [];
if(!empty($detail['share_dids'])){
$share_dids = explode(',', $detail['share_dids']);
}
if($detail['uid'] !=$uid && !in_array($uid,$share_uids) && !in_array($did,$share_dids) && $detail['is_share'] !=1){
2023-11-06 11:23:10 +08:00
$this->apiError('用户无权限', [], 2);
2023-10-30 17:37:45 +08:00
}
$detail['cate_title'] = Db::name('ArticleCate')->where(['id' => $detail['cate_id']])->value('title');
if($detail['file_ids'] !=''){
$fileArray = Db::name('File')->where('id','in',$detail['file_ids'])->select();
$detail['fileArray'] = $fileArray;
}
2023-11-02 16:19:06 +08:00
$detail['create_time'] = date('Y-m-d H:i:s', $detail['create_time']);
$detail['update_time'] = date('Y-m-d H:i:s', $detail['update_time']);
2023-10-30 17:37:45 +08:00
$comment = Db::name('ArticleComment')
->field('a.*,u.name,u.thumb')
->alias('a')
->join('Admin u', 'u.id = a.admin_id')
->order('a.create_time desc')
->where(['a.article_id'=>$detail['id'],'a.delete_time' => 0])
->select()->toArray();
foreach ($comment as $k => &$v) {
$v['times'] = time_trans($v['create_time']);
$v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
if($v['update_time']>0){
$v['update_time'] = ',最后编辑时间:'.time_trans($v['update_time']);
}
else{
$v['update_time'] = '';
}
}
$detail['comment'] = $comment;
Db::name('article')->where('id', $id)->inc('read')->update();
$this->apiSuccess('操作成功', $detail);
}
}