nk-lihaink-cn/app/admin/common.php

321 lines
8.8 KiB
PHP
Raw Normal View History

2021-01-30 20:59:12 +08:00
<?php
2021-07-28 10:04:47 +08:00
/**
* @copyright Copyright (c) 2021 勾股工作室
2021-11-24 17:17:29 +08:00
* @license https://opensource.org/licenses/Apache-2.0
2021-07-28 10:04:47 +08:00
* @link https://www.gougucms.com
*/
// 应用公共文件,内置主要的数据处理方法
use think\facade\Config;
use think\facade\Request;
use think\facade\Cache;
use think\facade\Db;
2021-01-30 20:59:12 +08:00
//获取后台模块当前登录用户的信息
function get_login_admin($key = "")
{
$session_admin = get_config('app.session_admin');
if (\think\facade\Session::has($session_admin)) {
$gougu_admin = \think\facade\Session::get($session_admin);
if (!empty($key)) {
if (isset($gougu_admin[$key])) {
return $gougu_admin[$key];
} else {
return '';
}
} else {
return $gougu_admin;
}
} else {
return '';
}
}
/**
* 截取摘要
* @return bool
*/
function getDescriptionFromContent($content, $count)
{
$content = preg_replace("@<script(.*?)</script>@is", "", $content);
$content = preg_replace("@<iframe(.*?)</iframe>@is", "", $content);
$content = preg_replace("@<style(.*?)</style>@is", "", $content);
$content = preg_replace("@<(.*?)>@is", "", $content);
$content = str_replace(PHP_EOL, '', $content);
$space = array(" ", " ", " ", " ", " ");
$go_away = array("", "", "", "", "");
$content = str_replace($space, $go_away, $content);
$res = mb_substr($content, 0, $count, 'UTF-8');
if (mb_strlen($content, 'UTF-8') > $count) {
$res = $res . "...";
}
return $res;
}
2021-02-22 23:43:11 +08:00
/**
* PHP格式化字节大小
* @param number $size 字节数
* @param string $delimiter 数字和单位分隔符
* @return string 格式化后的带单位的大小
*/
function format_bytes($size, $delimiter = '')
{
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
2021-07-28 10:04:47 +08:00
for ($i = 0; $size >= 1024 && $i < 5; $i++) {
$size /= 1024;
}
2021-02-22 23:43:11 +08:00
return round($size, 2) . $delimiter . $units[$i];
}
2021-07-28 10:04:47 +08:00
function create_tree_list($pid, $arr, $group, &$tree = [])
{
foreach ($arr as $key => $vo) {
if ($key == 0) {
2021-01-30 20:59:12 +08:00
$vo['spread'] = true;
}
if (!empty($group) and in_array($vo['id'], $group)) {
$vo['checked'] = true;
2021-07-28 10:04:47 +08:00
} else {
2021-01-30 20:59:12 +08:00
$vo['checked'] = false;
}
2021-07-28 10:04:47 +08:00
if ($vo['pid'] == $pid) {
$child = create_tree_list($vo['id'], $arr, $group);
if ($child) {
2021-01-30 20:59:12 +08:00
$vo['children'] = $child;
}
$tree[] = $vo;
}
}
return $tree;
}
//递归排序,用于分类选择
function set_recursion($result, $pid = 0, $level=-1)
2021-01-30 20:59:12 +08:00
{
/*记录排序后的类别数组*/
static $list = array();
static $space = ['','├─','§§├─','§§§§├─','§§§§§§├─'];
$level++;
2021-01-30 20:59:12 +08:00
foreach ($result as $k => $v) {
if ($v['pid'] == $pid) {
if ($pid != 0) {
$v['title'] = $space[$level] . $v['title'];
2021-01-30 20:59:12 +08:00
}
/*将该类别的数据放入list中*/
$list[] = $v;
set_recursion($result, $v['id'],$level);
2021-01-30 20:59:12 +08:00
}
}
return $list;
}
/**
* 根据id递归返回子数据
* @param $data 数据
* @param $pid 父节点id
*/
function get_data_node($data=[],$pid=0){
$dep = [];
foreach($data as $k => $v){
if($v['pid'] == $pid){
$node=get_data_node($data, $v['id']);
array_push($dep,$v);
if(!empty($node)){
$dep=array_merge($dep,$node);
}
}
}
return array_values($dep);
}
2021-01-30 20:59:12 +08:00
//获取指定管理员的信息
function get_admin($id)
{
$admin = Db::name('Admin')->where(['id' => $id])->find();
$admin['group_id'] = Db::name('AdminGroupAccess')->where(['uid' => $id])->column('group_id');
2021-01-30 20:59:12 +08:00
return $admin;
}
//读取权限节点列表
function get_admin_rule()
{
$rule = Db::name('AdminRule')->where(['status'=>1])->order('sort asc,id asc')->select()->toArray();
2021-01-30 20:59:12 +08:00
return $rule;
}
//读取模块列表
function get_admin_module()
2021-01-30 20:59:12 +08:00
{
$group = Db::name('AdminModule')->order('id asc')->select()->toArray();
2021-01-30 20:59:12 +08:00
return $group;
}
//读取权限分组列表
function get_admin_group()
2021-01-30 20:59:12 +08:00
{
$group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray();
2021-01-30 20:59:12 +08:00
return $group;
}
//读取指定权限分组详情
function get_admin_group_info($id)
2021-01-30 20:59:12 +08:00
{
$rule = Db::name('AdminGroup')->where(['id' => $id])->value('rules');
$rules = explode(',', $rule);
return $rules;
2021-01-30 20:59:12 +08:00
}
//读取部门列表
function get_department()
{
$department = Db::name('Department')->where(['status' => 1])->select()->toArray();
return $department;
}
//获取某部门的子部门id.$is_self时候包含自己
function get_department_son($did = 0, $is_self = 1)
{
$department = get_department();
$department_list = get_data_node($department, $did);
$department_array = array_column($department_list, 'id');
if ($is_self == 1) {
//包括自己在内
$department_array[] = $did;
}
return $department_array;
}
//读取员工所在部门的负责人
function get_department_leader($uid=0,$pid=0)
{
$did = get_admin($uid)['did'];
if($pid==0){
$leader = Db::name('Department')->where(['id' => $did])->value('leader_id');
}
else{
$pdid = Db::name('Department')->where(['id' => $did])->value('pid');
if($pdid == 0){
$leader = 0;
}
else{
$leader = Db::name('Department')->where(['id' => $pdid])->value('leader_id');
}
}
return $leader;
}
//读取职位
function get_position()
{
$position = Db::name('Position')->where(['status' => 1])->select()->toArray();
return $position;
}
2021-01-30 20:59:12 +08:00
//读取导航列表,用于后台
function get_nav($nav_id)
{
$nav = Db::name('NavInfo')->where('nav_id', $nav_id)->order('sort asc')->select();
2021-01-30 20:59:12 +08:00
return $nav;
}
//读取关键字列表
function get_keywords()
{
$keywords = Db::name('Keywords')->where(['status' => 1])->order('create_time asc')->select();
2021-01-30 20:59:12 +08:00
return $keywords;
}
2021-04-12 21:28:00 +08:00
2021-01-30 20:59:12 +08:00
//读取文章分类列表
function get_article_cate()
{
2022-08-31 18:04:16 +08:00
$cate = Db::name('ArticleCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
return $cate;
}
//读取图集分类列表
function get_gallery_cate()
{
2022-08-31 18:04:16 +08:00
$cate = Db::name('GalleryCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
return $cate;
}
//读取商品分类列表
function get_goods_cate()
{
2022-08-31 18:04:16 +08:00
$cate = Db::name('GoodsCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
2021-01-30 20:59:12 +08:00
return $cate;
}
2021-09-04 22:28:11 +08:00
//访问按小时归档统计
function hour_document($arrData)
{
$documents = array();
$hour = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
foreach ($hour as $val) {
$documents[$val] = 0;
}
foreach ($arrData as $index => $value) {
$archivesTime = intval(date("H", $value['create_time']));
$documents[$archivesTime] += 1;
}
return $documents;
}
2021-10-29 08:59:52 +08:00
//访问按日期归档统计
function date_document($arrData)
{
$documents = array();
foreach ($arrData as $index => $value) {
$archivesTime = date("Y-m-d", $value['create_time']);
if (empty($documents[$archivesTime])) {
$documents[$archivesTime] = 1;
} else {
$documents[$archivesTime] += 1;
}
}
return $documents;
}
2021-01-30 20:59:12 +08:00
/**
* 管理员操作日志
* @param string $type 操作类型 login add edit view delete
* @param int $param_id 操作类型
* @param array $param 提交的参数
*/
function add_log($type, $param_id = '', $param = [])
{
$action = '未知操作';
$type_action = get_config('log.admin_action');
if($type_action[$type]){
$action = $type_action[$type];
}
2021-01-30 20:59:12 +08:00
if ($type == 'login') {
$login_admin = Db::name('Admin')->where(array('id' => $param_id))->find();
2021-01-30 20:59:12 +08:00
} else {
$session_admin = get_config('app.session_admin');
$login_admin = \think\facade\Session::get($session_admin);
}
$data = [];
$data['uid'] = $login_admin['id'];
$data['nickname'] = $login_admin['nickname'];
$data['type'] = $type;
$data['action'] = $action;
2021-01-30 20:59:12 +08:00
$data['param_id'] = $param_id;
$data['param'] = json_encode($param);
$data['module'] = strtolower(app('http')->getName());
$data['controller'] = uncamelize(app('request')->controller());
$data['function'] = strtolower(app('request')->action());
2021-07-28 10:04:47 +08:00
$parameter = $data['module'] . '/' . $data['controller'] . '/' . $data['function'];
$rule_menu = Db::name('AdminRule')->where(array('src' => $parameter))->find();
if($rule_menu){
$data['title'] = $rule_menu['title'];
$data['subject'] = $rule_menu['name'];
}
else{
$data['title'] = '';
$data['subject'] ='系统';
}
$content = $login_admin['nickname'] . '在' . date('Y-m-d H:i:s') . $data['action'] . '了' . $data['subject'];
$data['content'] = $content;
2021-01-30 20:59:12 +08:00
$data['ip'] = app('request')->ip();
$data['create_time'] = time();
Db::name('AdminLog')->strict(false)->field(true)->insert($data);
2021-01-30 20:59:12 +08:00
}