forked from modstart/ModStartBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavUtil.php
More file actions
78 lines (68 loc) · 1.91 KB
/
Copy pathNavUtil.php
File metadata and controls
78 lines (68 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Module\Nav\Util;
use Illuminate\Support\Facades\Cache;
use ModStart\Core\Dao\ModelUtil;
use ModStart\Core\Util\TreeUtil;
use Module\Nav\Type\NavPosition;
class NavUtil
{
const CACHE_KEY_PREFIX = 'nav:';
public static function add($position, $name, $link)
{
$sort = intval(ModelUtil::max('nav', 'sort')) + 1;
ModelUtil::insert('nav', [
'position' => $position,
'name' => $name,
'link' => $link,
'sort' => $sort,
]);
}
public static function tree()
{
$nodes = TreeUtil::modelToTree('nav', [
'position' => 'position',
'name' => 'name',
'openType' => 'openType',
'link' => 'link',
'icon' => 'icon',
], 'id', 'pid', 'sort', ['enable' => true]);
return $nodes;
}
/**
* 根据位置获取
*
* @param string $position
* @return mixed
*/
public static function listByPosition($position = 'head')
{
$nodes = self::tree();
return array_filter($nodes, function ($item) use ($position) {
return $item['position'] == $position;
});
}
/**
* 根据位置获取,有缓存
*
* @param string $position
* @param int $minutes
* @return mixed
*/
public static function listByPositionWithCache($position = 'head', $minutes = 600)
{
return Cache::remember(self::CACHE_KEY_PREFIX . $position, $minutes, function () use ($position) {
return self::listByPosition($position);
});
}
public static function hasData($position = 'head')
{
$values = self::listByPositionWithCache($position);
return !empty($values);
}
public static function clearCache()
{
foreach (NavPosition::getList() as $k => $_) {
Cache::forget(self::CACHE_KEY_PREFIX . $k);
}
}
}