210 lines
5.3 KiB
PHP
210 lines
5.3 KiB
PHP
<?php
|
|
|
|
use App\Models\Advert;
|
|
use App\Models\Category;
|
|
use App\Models\Article;
|
|
use Encore\Admin\Config\ConfigModel;
|
|
|
|
function getConfigList($name, $take = 8)
|
|
{
|
|
return ConfigModel::where('name', 'like', "{$name}%")->get();
|
|
}
|
|
|
|
function getConfig($name, $return = '')
|
|
{
|
|
$info = ConfigModel::where('name', $name)->first();
|
|
if ($info) {
|
|
if ($return) {
|
|
return $info->{$return};
|
|
}
|
|
|
|
return $info;
|
|
}
|
|
|
|
return new ConfigModel;
|
|
|
|
}
|
|
|
|
function getOneCategory($categoryId, $return = '')
|
|
{
|
|
$category = Category::find($categoryId);
|
|
if ($category) {
|
|
if ($return) {
|
|
return $category->{$return};
|
|
}
|
|
|
|
return $category;
|
|
}
|
|
|
|
return new Category;
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取文章分类详情
|
|
* @Author: 玄尘
|
|
* @Date : 2020/9/10 13:21
|
|
* @param $categoryId
|
|
* @param string $result
|
|
* @return \App\Models\Article
|
|
*/
|
|
function getOneArticleBYCate($categoryId, $result = '')
|
|
{
|
|
$info = Article::latest('sort')
|
|
->whereHas('categories', function ($q) use ($categoryId) {
|
|
$q->where('id', $categoryId);
|
|
})
|
|
->latest()
|
|
->first();
|
|
|
|
if ($info) {
|
|
if ($result) {
|
|
return $info->{$result};
|
|
}
|
|
|
|
return $info;
|
|
} else {
|
|
return '';
|
|
}
|
|
|
|
return new Article;
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取分类下的文章
|
|
* @Author: 玄尘
|
|
* @Date : 2020/9/10 10:05
|
|
* @param $categoryId
|
|
* @param int $take
|
|
* @param string $mark 显示多少当前分类还是当前分类+子分类
|
|
* @param bool $hasCover
|
|
* @param string $order
|
|
* @return \App\Models\Article
|
|
*/
|
|
function getArticlesBYCate($categoryId, $take = 8, $mark = 'one', $hasCover = false, $order = 'desc')
|
|
{
|
|
if ($mark == 'one') {
|
|
$articles = Article::where('status', 1)
|
|
->whereHas('categories', function ($q) use ($categoryId) {
|
|
$q->where('id', $categoryId);
|
|
})
|
|
->orderBy('sort', $order)
|
|
->when($hasCover, function ($q) {
|
|
$q->whereNotNull('cover');
|
|
})
|
|
->take($take)
|
|
->get();
|
|
} else {
|
|
$cate = Category::find($categoryId);
|
|
$ids = $cate->getAllChildrenId();
|
|
|
|
$articles = Article::where('status', 1)
|
|
->whereHas('categories', function ($q) use ($ids) {
|
|
$q->whereIn('id', $ids);
|
|
})
|
|
->when($hasCover, function ($q) {
|
|
$q->whereNotNull('cover');
|
|
})
|
|
->orderBy('sort', $order)
|
|
->take($take)
|
|
->get();
|
|
}
|
|
|
|
return $articles;
|
|
}
|
|
|
|
//获取子分类
|
|
function getCateChild($categoryId)
|
|
{
|
|
return Category::where('status', 1)
|
|
->where('parent_id', $categoryId)
|
|
->orderBy('order', 'asc')
|
|
->get();
|
|
}
|
|
|
|
//获取顶级分类
|
|
function getTopCate($categoryId)
|
|
{
|
|
$parent = Category::find($categoryId);
|
|
|
|
while ($parent->parent_id != 0) {
|
|
$parent = $parent->parent;
|
|
}
|
|
|
|
return $parent;
|
|
}
|
|
|
|
//获取一个广告
|
|
function getOneAdvertByCate($categoryId, $result = '')
|
|
{
|
|
$info = Advert::where('category_id', $categoryId)
|
|
->latest('sort')
|
|
->latest()
|
|
->first();
|
|
if ($info) {
|
|
if ($result) {
|
|
return $info->{$result};
|
|
}
|
|
|
|
return $info;
|
|
} else {
|
|
return '';
|
|
}
|
|
|
|
return new Advert;
|
|
}
|
|
|
|
function getAdvertsByCate($categoryId, $take = 8)
|
|
{
|
|
return Advert::where('category_id', $categoryId)
|
|
->latest('sort')
|
|
->latest()
|
|
->take($take)
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Notes: 根据分类id 获取文章
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/21 9:45
|
|
* @param int $take
|
|
* @param false $hasCover
|
|
* @return mixed
|
|
*/
|
|
function getArticlesByCateIds($take = 8, $hasCover = false)
|
|
{
|
|
//分院新闻 媒体报道 科研动态 交流合作 党建文化 专题学习
|
|
$ids = [7, 17, 8, 5, 12, 30];
|
|
|
|
$articles = Article::query()->where('status', 1)
|
|
->whereHas('categories', function ($q) use ($ids) {
|
|
$q->whereIn('id', $ids);
|
|
})
|
|
->latest()
|
|
->when($hasCover, function ($q) {
|
|
$q->whereNotNull('cover')->orWhere('cover', '<>', '');
|
|
})
|
|
->take($take)
|
|
->get();
|
|
|
|
return $articles;
|
|
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取推荐文章
|
|
* @Author: 玄尘
|
|
* @Date : 2021/4/16 13:31
|
|
* @param $postion
|
|
* @return mixed
|
|
*/
|
|
function getArticlesByPosition($postion)
|
|
{
|
|
$articles = Article::ofPosition($postion)
|
|
->latest()
|
|
->take(5)
|
|
->get();
|
|
|
|
return $articles;
|
|
}
|
|
|