Files
new_ine/app/Helpers/function.php
2021-12-20 13:29:48 +08:00

268 lines
5.4 KiB
PHP

<?php
use App\Models\Advert;
use App\Models\Category;
use App\Models\Article;
use App\Models\Video;
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::where('status', 1)
->ByCategory($categoryId)
->latest('sort')->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 int $recommen
* @return \App\Models\Article
*/
function getArticlesBYCate($categoryId, int $take = 8, $recommen = 0)
{
$cate = Category::find($categoryId);
$ids = $cate ? $cate->getAllChildrenId() : [];
$articles = Article::where('status', 1)
->ByCategory([$categoryId])
->latest('sort')
->latest()
->when($recommen, function ($q) use ($recommen) {
$q->where('recommen', 1);
})
->take($take)
->get();
return $articles;
}
/**
* Notes: 获取有图片的文章
*
* @Author: 玄尘
* @Date: 2021/12/20 13:22
* @param $categoryId
* @param $take
* @return mixed
*/
function getHasCoverArticlesByCate($categoryId, $take = 8)
{
return Article::where('status', 1)
->ByCategory([$categoryId])
->whereNotNull('cover')
->latest('sort')
->latest()
->take($take)
->get();
}
//获取子分类
function getCateChild($categoryId, $take = '')
{
return Category::where('status', 1)
->where('parent_id', $categoryId)
->orderBy('order', 'asc')
->when($take, function ($q) use ($take) {
$q->take($take);
})
->get();
}
//获取顶级分类
function getTopCate($categoryId)
{
$parent = Category::find($categoryId);
while ($parent->parent_id != 0) {
$parent = $parent->parent;
}
return $parent;
}
/**
* Notes: 根据分类获取一张图片
*
* @Author: 玄尘
* @Date : 2021/10/8 13:53
* @param $categoryId
* @param string $result
* @return string
*/
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 '';
}
}
/**
* Notes: 获取图片
*
* @Author: 玄尘
* @Date: 2021/11/29 10:05
* @param $categoryId
* @param string $take
* @return mixed
*/
function getAdvertsByCate($categoryId, $take = '8')
{
return Advert::where('category_id', $categoryId)
->latest()
->take($take)
->get();
}
/**
* Notes: 根据分类获取视频
*
* @Author: 玄尘
* @Date: 2021/11/29 10:05
* @param $categoryId
* @param string $result
* @return string
*/
function getVideoByCate($categoryId, string $result = '')
{
$video = Video::where('category_id', $categoryId)->latest()->first();
if ($video) {
if ($result) {
return $video->{$result};
}
return $video;
} else {
return '';
}
}
/**
* Notes: 获取视频列表
*
* @Author: 玄尘
* @Date: 2021/11/29 10:21
* @param $categoryId
* @param string $take
* @return mixed
*/
function getVideosByCate($categoryId, $take = '8')
{
return Video::where('category_id', $categoryId)
->latest()
->take($take)
->get();
}
function dateLocalMonth($m): string
{
$months = [
'1' => '一月',
'2' => '二月',
'3' => '三月',
'4' => '四月',
'5' => '五月',
'6' => '六月',
'7' => '七月',
'8' => '八月',
'9' => '九月',
'10' => '十月',
'11' => '十一月',
'12' => '十二月',
];
return $months[$m] ?? $m;
}
/**
* Notes: 获取
*
* @Author: 玄尘
* @Date: 2021/12/8 10:31
* @param $article_id
*/
function getOneArticle($article_id, $key = '')
{
$info = Article::find($article_id);
if ($info) {
if ($key) {
return $info->{$key};
}
return $info;
}
return '';
}
/**
* Notes: 判断顶级分类
*
* @Author: 玄尘
* @Date: 2021/12/9 10:13
* @param $parent_id
* @param $category_id
* @return bool
*/
function isTopCategory($parent_id, $category_id): bool
{
$category_ids = [];
if (is_array($parent_id)) {
foreach ($parent_id as $id) {
$category_ids[] = (Category::getTopCategory($id))->id;
}
} else {
$category_ids[] = (Category::getTopCategory($parent_id))->id;
}
return in_array($category_id, $category_ids);
}