Files
new_haai/app/Helpers/function.php
2022-08-19 17:30:44 +08:00

240 lines
4.9 KiB
PHP

<?php
use App\Models\Advert;
use App\Models\Category;
use App\Models\Article;
use App\Models\Patent;
use App\Models\Talent;
use Encore\Admin\Auth\Database\Menu;
use Illuminate\Support\Arr;
/**
* Notes: 获取一个分类
*
* @Author: 玄尘
* @Date : 2021/2/3 16:36
* @param $categoryId
* @param string $return
* @return \App\Models\Category
*/
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 $take
* @return \App\Models\Article
*/
function getArticlesBYCate($categoryId, $take)
{
$articles = Article::where('status', 1)
->ByCategory($categoryId)
->latest('sort')
->latest()
->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;
}
/**
* Notes: 获取所有下级
*
* @Author: 玄尘
* @Date: 2022/8/19 17:24
* @param $array
* @param $categoryId
* @return array
*/
function getAllChild($categories, $categoryId)
{
$arr = array();
foreach ($categories as $category) {
if ($category['parent_id'] == $categoryId) {
$arr[] = $category['id'];
$arr = array_merge($arr, getAllChild($categories, $category['id']));
};
};
return $arr;
}
function getSonNode($pid = 0, $SonNode = array())
{
$SonNode[] = $pid;
foreach ($data as $k => $v) {
if ($v['source_member'] == $pid) {
$SonNode = $this->getSonNode($data, $v['member_id'], $SonNode);
}
}
unset($SonNode[0]);
return $SonNode;
}
//获取专利和论文
function getPatent($take, $type = '')
{
return Patent::where('status', 1)
->when($type, function ($q) use ($type) {
$q->where('type', $type);
})
->get();
}
//获取一个图片
function getOneAdvert($category_id, $value = '')
{
$info = Advert::where('category_id', $category_id)->first();
if ($value) {
return $info->{$value};
}
return $info;
}
//获取人才数据
function getAllTalent($category_id = '')
{
return Talent::latest('sort')
->when($category_id, function ($q) use ($category_id) {
$q->where('category_id', $category_id);
})
->get();
}
//获取一篇人才文章
function getOneRenById($article_id)
{
return Talent::find($article_id);
}
//获取菜单信息
function getCate($id, $value = '')
{
$info = Menu::find($id);
if ($value) {
return $info->{$value};
}
return $info;
}
/**
* Notes: 获取所有上级
*
* @Author: 玄尘
* @Date : 2021/2/3 13:22
* @param $categoryId
* @return mixed
*/
function getAllParentCate($categoryId): array
{
$res = collect();
$parent = Category::find($categoryId);
$res->prepend($parent);
while ($parent->parent_id != 0) {
$parent = $parent->parent;
$res->prepend($parent);
}
return $res->all();
}
/**
* Notes: 获得分类
*
* @Author: 玄尘
* @Date : 2021/2/3 16:38
* @param $cate_id
* @param $
*/
function getArtilesByCates($cate_id, $take = 8)
{
$cate_ids = Category::where('parent_id', $cate_id)
->orWhere('id', $cate_id)
->where('status', 1)
->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW])
->pluck('id')
->toArray();
return Article::where('status', 1)
->latest()
->ByCategory($cate_ids)
->take($take)
->get();
}
// 根据定位获取文章
function getArtileByPos($position)
{
return Article::where('position', $position)->first();
}