307 lines
9.7 KiB
PHP
307 lines
9.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace app\model;
|
||
|
|
||
|
class Category extends Base
|
||
|
{
|
||
|
const AboutMarketId = 56;//关于市场顶级id
|
||
|
const ProductCategoryId = 57;//产品顶级id
|
||
|
const AquaticSupplyAndDemandId = 62;//水产供求顶级id
|
||
|
const NewsId = 58;//新闻顶级id
|
||
|
|
||
|
const indexAquaticSupplyDemandId = 74;//首页水产供求id(不是顶级栏目)
|
||
|
const indexAquacultureInformationId = 76;//首页水产养殖资讯id(不是顶级栏目)
|
||
|
|
||
|
const indexAquaticSeedlingId = 66;//首页水产鱼苗id(不是顶级栏目)
|
||
|
const indexFisheryFeedId = 67;//首页渔用饲料id(不是顶级栏目)
|
||
|
const indexDomesticFishWholesaleId = 68;//首页家鱼批发id(不是顶级栏目)
|
||
|
const indexSeafoodWholesaleId = 69;//首页海鲜批发id(不是顶级栏目)
|
||
|
const indexFisherySuppliesId = 70;//首页渔需物资id(不是顶级栏目)
|
||
|
|
||
|
|
||
|
//获取首页栏目ID
|
||
|
public static function getIndex()
|
||
|
{
|
||
|
return self::where('is_index', 1)->findOrEmpty()->toArray();
|
||
|
}
|
||
|
|
||
|
//根据上级ID和语言获取下级栏目
|
||
|
public static function getChildrenByParentId($parentId)
|
||
|
{
|
||
|
if ($parentId <= 0) {
|
||
|
return [];
|
||
|
}
|
||
|
$category = self::getById($parentId);
|
||
|
if (empty($category)) {
|
||
|
return [];
|
||
|
}
|
||
|
return self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.template, m.name as modelName')
|
||
|
->where('c.parent_id', $parentId)
|
||
|
->order('c.sort', 'asc')
|
||
|
->select()
|
||
|
->toArray();
|
||
|
}
|
||
|
|
||
|
//重写方法
|
||
|
public static function getById($categoryId)
|
||
|
{
|
||
|
return self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id = m.id')
|
||
|
->where('c.id', $categoryId)
|
||
|
->field('c.*, m.template')
|
||
|
->findOrEmpty()
|
||
|
->toArray();
|
||
|
}
|
||
|
|
||
|
//查看是否有下级栏目
|
||
|
public static function hasChildren($categoryId)
|
||
|
{
|
||
|
if (is_array($categoryId)) {
|
||
|
$count = self::where('parent_id', 'in', $categoryId)->count();
|
||
|
} else {
|
||
|
$count = self::where(['parent_id' => $categoryId])->count();
|
||
|
}
|
||
|
return $count ? true : false;
|
||
|
}
|
||
|
|
||
|
//获取前台菜单
|
||
|
public static function getListForFrontMenu()
|
||
|
{
|
||
|
$items = self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.template')
|
||
|
->where('c.state', 1)
|
||
|
->order('is_index desc, sort asc')
|
||
|
->select()
|
||
|
->toArray();
|
||
|
return self::getCates($items);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取栏目
|
||
|
* @param bool $limit 是否限制查询
|
||
|
* @param array $cates 需要限制查询的栏目IDs
|
||
|
*/
|
||
|
public static function getList($limit = false, $cates = [])
|
||
|
{
|
||
|
if ($limit && empty($cates)) {
|
||
|
return [];
|
||
|
}
|
||
|
return self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.name as modelName')
|
||
|
->when($limit, function ($query) use ($cates) {
|
||
|
$query->whereIn('c.id', $cates);
|
||
|
})
|
||
|
->order('sort', 'asc')
|
||
|
->select()
|
||
|
->toArray();
|
||
|
}
|
||
|
|
||
|
//获取栏目分级列表
|
||
|
public static function getListTree($isMenu = false)
|
||
|
{
|
||
|
if ($isMenu) {
|
||
|
$items = self::where('c.state', 1)->order('sort', 'asc')->select()->toArray();
|
||
|
} else {
|
||
|
$items = self::order('sort', 'asc')->select()->toArray();
|
||
|
}
|
||
|
return self::getCates($items);
|
||
|
}
|
||
|
|
||
|
//获取递归栏目
|
||
|
public static function getCates($cates, $parentId = 0)
|
||
|
{
|
||
|
$menus = [];
|
||
|
foreach ($cates as $cate) {
|
||
|
if ($cate['parent_id'] == $parentId) {
|
||
|
$children = self::getCates($cates, $cate['id']);
|
||
|
if (!empty($children)) {
|
||
|
$cate['children'] = $children;
|
||
|
}
|
||
|
$menus[] = $cate;
|
||
|
}
|
||
|
}
|
||
|
return $menus;
|
||
|
}
|
||
|
|
||
|
public static function onAfterInsert($category)
|
||
|
{
|
||
|
$category->sort = $category->id;
|
||
|
$category->save();
|
||
|
}
|
||
|
|
||
|
//递归获取栏目名称面包屑
|
||
|
public static function getCatesCrumbs($currentId = 0)
|
||
|
{
|
||
|
$crumbs = [];
|
||
|
$category = self::getById($currentId);
|
||
|
if ($category) {
|
||
|
if ($category['parent_id'] != 0) {
|
||
|
$categoryIds = explode(',', trim($category['path'], ','));
|
||
|
$categories = self::where('id', 'in', $categoryIds)->column('*', 'id');
|
||
|
foreach ($categoryIds as $id) {
|
||
|
if (isset($categories[$id])) {
|
||
|
$crumbs[] = $categories[$id]['title'];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
$crumbs[] = $category['title'];
|
||
|
}
|
||
|
return $crumbs;
|
||
|
}
|
||
|
|
||
|
//获取栏目中涉及到的文件
|
||
|
public static function getFilesInUse()
|
||
|
{
|
||
|
$items = self::select()->toArray();
|
||
|
$data = [];
|
||
|
foreach ($items as $item) {
|
||
|
$src = trim($item['src']);
|
||
|
if (!empty($src)) {
|
||
|
$key = getKeyByPath($src);
|
||
|
$data[$key] = $src;
|
||
|
}
|
||
|
}
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
//当前分类的最高级分类Id
|
||
|
public static function firstGradeById($id)
|
||
|
{
|
||
|
$res = 0;
|
||
|
$item = self::getById($id);
|
||
|
if ($item) {
|
||
|
$res = $id;
|
||
|
if ($item['parent_id'] > 0) {
|
||
|
$items = self::select()->toArray();
|
||
|
$first = self::getFirstGrade($items, $item['parent_id']);
|
||
|
if (!empty($first)) {
|
||
|
$res = $first['id'];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $res;
|
||
|
}
|
||
|
|
||
|
public static function getFirstGrade($items, $parentId)
|
||
|
{
|
||
|
$data = [];
|
||
|
foreach ($items as $key => $item) {
|
||
|
if ($item['id'] == $parentId) {
|
||
|
if ($item['parent_id'] > 0) {
|
||
|
unset($items[$key]);
|
||
|
$parent = self::getFirstGrade($items, $item['parent_id']);
|
||
|
if (!empty($parent)) {
|
||
|
$data = $parent;
|
||
|
} else {
|
||
|
$data = $item;
|
||
|
}
|
||
|
} else {
|
||
|
$data = $item;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
public static function getFooterMenu()
|
||
|
{
|
||
|
$items = self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.template')
|
||
|
->where('c.state', 1)
|
||
|
->where('c.id', "in", self::footer_menu_ids)
|
||
|
->whereOr('c.parent_id', "in", self::footer_menu_ids)
|
||
|
->order('is_index desc, sort asc')
|
||
|
->select()
|
||
|
->toArray();
|
||
|
return self::getCates($items);
|
||
|
}
|
||
|
|
||
|
public static function getChildrenIds($parentId)
|
||
|
{
|
||
|
return self::where("parent_id", $parentId)->column("id");
|
||
|
}
|
||
|
|
||
|
//获取当前位置
|
||
|
public static function getPosition($categoryId, $isIndex = true)
|
||
|
{
|
||
|
$position = "";
|
||
|
$getFirstGrade = self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.template, m.name as modelName')
|
||
|
->where("c.id", $categoryId)
|
||
|
->find()
|
||
|
->toArray();
|
||
|
|
||
|
if (!$getFirstGrade) return $position;
|
||
|
|
||
|
$position = '<a href="' . getUri($getFirstGrade) . '">' . $getFirstGrade["title"] . "</a> " . $position;
|
||
|
if (!in_array($getFirstGrade["parent_id"], [0, 1])) {
|
||
|
$position = (self::getPosition($getFirstGrade["parent_id"], false) . $position);
|
||
|
}
|
||
|
return ($isIndex ? "<a href='/'>首页</a>" : "") . $position;
|
||
|
|
||
|
}
|
||
|
|
||
|
public static function getProductNewsChildrenIds()
|
||
|
{
|
||
|
return self::where("parent_id","in",[
|
||
|
self::NewsId,
|
||
|
self::ProductCategoryId,
|
||
|
])->column("id");
|
||
|
}
|
||
|
// ----------web -------
|
||
|
//关于市场
|
||
|
public static function getAboutMarket()
|
||
|
{
|
||
|
$ids = self::where("parent_id", self::AboutMarketId)->column("id");
|
||
|
return self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.template, m.name as modelName')
|
||
|
->where('c.id', "in", $ids)
|
||
|
->order('c.sort', 'asc')
|
||
|
->select()
|
||
|
->toArray();
|
||
|
}
|
||
|
|
||
|
//产品展示
|
||
|
public static function getProduct()
|
||
|
{
|
||
|
return self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.template, m.name as modelName')
|
||
|
->where('c.parent_id', "=", self::ProductCategoryId)
|
||
|
->order('c.sort', 'asc')
|
||
|
->select()
|
||
|
->toArray();
|
||
|
}
|
||
|
|
||
|
//水产供求
|
||
|
public static function getAquaticSupplyAndDemand()
|
||
|
{
|
||
|
return self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.template, m.name as modelName')
|
||
|
->where('c.parent_id', "=", self::AquaticSupplyAndDemandId)
|
||
|
->order('c.sort', 'asc')
|
||
|
->select()
|
||
|
->toArray();
|
||
|
}
|
||
|
|
||
|
//新闻 市场动态
|
||
|
public static function getNews()
|
||
|
{
|
||
|
return self::alias('c')
|
||
|
->leftJoin('model m', 'c.model_id=m.id')
|
||
|
->field('c.*, m.manager, m.template, m.name as modelName')
|
||
|
->where('c.parent_id', "=", self::NewsId)
|
||
|
->order('c.sort', 'asc')
|
||
|
->select()
|
||
|
->toArray();
|
||
|
}
|
||
|
}
|