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

122 lines
4.1 KiB
PHP

<?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()
{
$this->checkAuth();
$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);
}
//查看文章分类
public function cate()
{
$cate = Db::name('ArticleCate')->order('id desc')->select()->toArray();
$this->apiSuccess('获取成功', $cate);
}
//查看文章详情
public function view()
{
$this->checkAuth();
$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){
$this->apiError('用户无权限', [], 2);
}
$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;
}
$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']);
$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);
}
}