* Date: 2018-4-3
*/
namespace app\common\logic;
use think\Db;
use think\Model;
use app\common\logic\ArctypeLogic;
/**
* 菜单逻辑定义
* @package common\Logic
*/
class NavigationLogic extends Model
{
/**
* 构造方法
*/
public function initialize(){
parent::initialize();
$this->arctypeLogic = new ArctypeLogic();
}
/**
* 全部菜单
*/
public function GetAllArctype($type_id = 0)
{
$where = [
'is_del' => 0,
'status' => 1,
'lang' => get_admin_lang(),
];
$field = 'id, parent_id, typename, dirname, litpic,grade';
// 查询所有可投稿的菜单
$ArcTypeData = Db::name('arctype')->field($field)->where($where)->select();
$oneLevel = $twoLevel = $threeLevel = [];
foreach ($ArcTypeData as $k => $v) {
if (0 == $v['grade']) {
$oneLevel[] = $v;
} elseif (1 == $v['grade']) {
$twoLevel[] = $v;
} elseif (2 == $v['grade']) {
$threeLevel[] = $v;
}
}
static $seo_pseudo = null;
if (null === $seo_pseudo) {
$seoConfig = tpCache('seo');
$seo_pseudo = !empty($seoConfig['seo_pseudo']) ? $seoConfig['seo_pseudo'] : config('ey_config.seo_pseudo');
}
// 下拉框拼装
$HtmlCode = '';
return $HtmlCode;
}
// 获取全部可用于快速生成菜单的栏目列表
public function getAllArctypeList($type_id = 0)
{
// 栏目最大层级数
$arctype_max_level = intval(config('global.arctype_max_level'));
// 获取全部可用栏目
$options = $this->arctypeLogic->arctype_list(0, 0, false, $arctype_max_level - 1, ['is_del' => 0]);
// URL模式
static $seo_pseudo = null;
if (null === $seo_pseudo) {
$seoConfig = tpCache('seo');
$seo_pseudo = !empty($seoConfig['seo_pseudo']) ? $seoConfig['seo_pseudo'] : config('ey_config.seo_pseudo');
}
// 栏目选择内容
$arctypeHtml = '';
// 返回内容
// dump($arctypeHtml);exit;
return $arctypeHtml;
}
// 获取全部导航菜单列表
public function getAllNavList($position_id = 0, $nav_id = 0)
{
// 查询所有可投稿的菜单的顶级菜单
$where = [
'c.is_del' => 0,
'c.status' => 1,
'c.position_id' => $position_id
];
$navList = $this->nav_list(0, 0, false, 0, $where, false);
$navHtml = '';
// dump($navHtml);exit;
return $navHtml;
}
/**
* 获得指定菜单下的子菜单的数组
*
* @access public
* @param int $id 菜单的ID
* @param int $selected 当前选中菜单的ID
* @param boolean $re_type 返回的类型: 值为真时返回下拉列表,否则返回数组
* @param int $level 限定返回的级数。为0时返回所有级数
* @param array $map 查询条件
* @return mix
*/
public function nav_list($id = 0, $selected = 0, $re_type = true, $level = 0, $map = array(), $is_cache = true)
{
$fields = "c.*, c.nav_id as typeid, count(s.nav_id) as has_children, '' as children";
$args = [$fields, $map, $is_cache];
$cacheKey = 'nav_list-'.md5(__CLASS__.__FUNCTION__.json_encode($args));
$res = cache($cacheKey);
if (empty($res) || empty($is_cache)) {
$res = Db::name('nav_list')
->field($fields)
->alias('c')
->join('nav_list s', 's.parent_id = c.nav_id', 'LEFT')
->where($map)
->group('c.nav_id')
->order('c.parent_id asc, c.sort_order asc, c.nav_id')
// ->cache($is_cache, EYOUCMS_CACHE_TIME, "nav_list")
->select();
cache($cacheKey, $res, null, 'nav_list');
}
if (empty($res) == true) {
return $re_type ? '' : array();
}
$options = $this->nav_options($id, $res); // 获得指定菜单下的子菜单的数组
/* 截取到指定的缩减级别 */
if ($level > 0) {
if ($id == 0) {
$end_level = $level;
} else {
$first_item = reset($options); // 获取第一个元素
$end_level = $first_item['level'] + $level;
}
/* 保留level小于end_level的部分 */
foreach ($options AS $key => $val) {
if ($val['level'] >= $end_level) {
unset($options[$key]);
}
}
}
$pre_key = 0;
$select = '';
foreach ($options AS $key => $value) {
$options[$key]['has_children'] = 0;
if ($pre_key > 0) {
if ($options[$pre_key]['nav_id'] == $options[$key]['parent_id']) {
$options[$pre_key]['has_children'] = 1;
}
}
$pre_key = $key;
if ($re_type == true) {
$select .= '';
}
}
if ($re_type == true) {
return $select;
} else {
return $options;
}
}
/**
* 过滤和排序所有菜单,返回一个带有缩进级别的数组
*
* @access private
* @param int $id 上级菜单ID
* @param array $arr 含有所有菜单的数组
* @param int $level 级别
* @return void
*/
public function nav_options($spec_id, $arr)
{
$cat_options = array();
if (isset($cat_options[$spec_id])) {
return $cat_options[$spec_id];
}
if (!isset($cat_options[0])) {
$level = $last_id = 0;
$options = $id_array = $level_array = array();
while (!empty($arr)) {
foreach ($arr AS $key => $value) {
$id = $value['nav_id'];
if ($level == 0 && $last_id == 0) {
if ($value['parent_id'] > 0) {
break;
}
$options[$id] = $value;
$options[$id]['level'] = $level;
$options[$id]['nav_id'] = $id;
$options[$id]['nav_name'] = htmlspecialchars_decode($value['nav_name']);
unset($arr[$key]);
if ($value['has_children'] == 0) {
continue;
}
$last_id = $id;
$id_array = array($id);
$level_array[$last_id] = ++$level;
continue;
}
if ($value['parent_id'] == $last_id) {
$options[$id] = $value;
$options[$id]['level'] = $level;
$options[$id]['nav_id'] = $id;
$options[$id]['nav_name'] = htmlspecialchars_decode($value['nav_name']);
unset($arr[$key]);
if ($value['has_children'] > 0) {
if (end($id_array) != $last_id) {
$id_array[] = $last_id;
}
$last_id = $id;
$id_array[] = $id;
$level_array[$last_id] = ++$level;
}
} elseif ($value['parent_id'] > $last_id) {
break;
}
}
$count = count($id_array);
if ($count > 1) {
$last_id = array_pop($id_array);
} elseif ($count == 1) {
if ($last_id != end($id_array)) {
$last_id = end($id_array);
} else {
$level = 0;
$last_id = 0;
$id_array = array();
continue;
}
}
if ($last_id && isset($level_array[$last_id])) {
$level = $level_array[$last_id];
} else {
$level = 0;
break;
}
}
$cat_options[0] = $options;
} else {
$options = $cat_options[0];
}
if (!$spec_id) {
return $options;
} else {
if (empty($options[$spec_id])) {
return array();
}
$spec_id_level = $options[$spec_id]['level'];
foreach ($options AS $key => $value) {
if ($key != $spec_id) {
unset($options[$key]);
} else {
break;
}
}
$spec_id_array = array();
foreach ($options AS $key => $value) {
if (($spec_id_level == $value['level'] && $value['nav_id'] != $spec_id) ||
($spec_id_level > $value['level'])) {
break;
} else {
$spec_id_array[$key] = $value;
}
}
$cat_options[$spec_id] = $spec_id_array;
return $spec_id_array;
}
}
// 前台功能列表
public function ForegroundFunction()
{
return $ReturnData = [
0 => [
'title' => '首页',
'url' => "web_cmsurl"
],
1 => [
'title' => '个人中心',
'url' => "index"
],
2 => [
'title' => '我的信息',
'url' => "user_info"
],
3 => [
'title' => '我的收藏',
'url' => "my_collect"
],
4 => [
'title' => '财务明细',
'url' => "consumer_details"
],
5 => [
'title' => '购物车',
'url' => 'shop_cart_list'
],
6 => [
'title' => '收货地址',
'url' => "shop_address_list"
],
7 => [
'title' => '我的订单',
'url' => "shop_centre"
],
8 => [
'title' => '我的评价',
'url' => "my_comment"
],
9 => [
'title' => '投稿列表',
'url' => "release_centre"
],
10 => [
'title' => '我要投稿',
'url' => "article_add"
],
11 => [
'title' => '外部链接',
'url' => "external_link"
],
];
}
}