-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.php
80 lines (72 loc) · 1.8 KB
/
common.php
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
/**
* Created by FHYI.
* Date 2020/10/29
* Time 16:39
*/
use think\facade\Db;
use think\facade\Cache;
/**
* 获取数据库配置
* @param $key
* @return mixed
*/
function getDbConfig($key = null)
{
$config = \think\facade\Config::get('database');
if (!empty($key)) {
$config = $config['connections'][$config['default']];
return $config[$key];
}
return $config['connections'][$config['default']];
}
// 设置栏目缓存全部
function getAllCategory(){
}
/**
* 获取栏目相关信息
* @param string $catid 栏目id
* @param string $field 返回的字段,默认返回全部,数组
* @param boolean $newCache 是否强制刷新
* @return boolean
*/
function getCategory($catid, $field = '', $newCache = false)
{
if (empty($catid)) {
return false;
}
$key = 'getCategory_' . $catid;
//强制刷新缓存
if ($newCache) {
cache($key, NULL);
}
$cache = cache($key);
if ($cache === 'false') {
return false;
}
if (empty($cache)) {
//读取数据
$cache = \app\cms\model\category\Category::where('catid', $catid)->findOrEmpty();
if (empty($cache)) {
cache($key, 'false', 60);
return false;
} else {
//扩展配置
$cache['setting'] = unserialize($cache['setting']);
//栏目扩展字段
$cache['extend'] = isset($cache['setting']['extend']) ?: [];
cache($key, $cache, 3600);
}
}
if ($field) {
//支持var.property,不过只支持一维数组
if (false !== strpos($field, '.')) {
$vars = explode('.', $field);
return $cache[$vars[0]][$vars[1]];
} else {
return $cache[$field];
}
} else {
return $cache;
}
}