123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- <?php
- /**
- * @copyright Copyright (c) 2021 勾股工作室
- * @license https://opensource.org/licenses/Apache-2.0
- * @link https://www.gougucms.com
- */
- // 应用公共文件,内置主要的数据处理方法
- use think\facade\Config;
- use think\facade\Request;
- use think\facade\Cache;
- use think\facade\Db;
- //获取后台模块当前登录用户的信息
- 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;
- }
- /**
- * PHP格式化字节大小
- * @param number $size 字节数
- * @param string $delimiter 数字和单位分隔符
- * @return string 格式化后的带单位的大小
- */
- function format_bytes($size, $delimiter = '')
- {
- $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
- for ($i = 0; $size >= 1024 && $i < 5; $i++) {
- $size /= 1024;
- }
- return round($size, 2) . $delimiter . $units[$i];
- }
- function create_tree_list($pid, $arr, $group, &$tree = [])
- {
- foreach ($arr as $key => $vo) {
- if ($key == 0) {
- $vo['spread'] = true;
- }
- if (!empty($group) and in_array($vo['id'], $group)) {
- $vo['checked'] = true;
- } else {
- $vo['checked'] = false;
- }
- if ($vo['pid'] == $pid) {
- $child = create_tree_list($vo['id'], $arr, $group);
- if ($child) {
- $vo['children'] = $child;
- }
- $tree[] = $vo;
- }
- }
- return $tree;
- }
- //递归排序,用于分类选择
- function set_recursion($result, $pid = 0, $level=-1)
- {
- /*记录排序后的类别数组*/
- static $list = array();
- static $space = ['','├─','§§├─','§§§§├─','§§§§§§├─'];
- $level++;
- foreach ($result as $k => $v) {
- if ($v['pid'] == $pid) {
- if ($pid != 0) {
- $v['title'] = $space[$level] . $v['title'];
- }
- /*将该类别的数据放入list中*/
- $list[] = $v;
- set_recursion($result, $v['id'],$level);
- }
- }
- 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);
- }
- //获取指定管理员的信息
- function get_admin($id)
- {
- $admin = Db::name('Admin')->where(['id' => $id])->find();
- $admin['group_id'] = Db::name('AdminGroupAccess')->where(['uid' => $id])->column('group_id');
- return $admin;
- }
- //读取权限节点列表
- function get_admin_rule()
- {
- $rule = Db::name('AdminRule')->where(['status'=>1])->order('sort asc,id asc')->select()->toArray();
- return $rule;
- }
- //读取模块列表
- function get_admin_module()
- {
- $group = Db::name('AdminModule')->order('id asc')->select()->toArray();
- return $group;
- }
- //读取权限分组列表
- function get_admin_group()
- {
- $group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray();
- return $group;
- }
- //读取指定权限分组详情
- function get_admin_group_info($id)
- {
- $rule = Db::name('AdminGroup')->where(['id' => $id])->value('rules');
- $rules = explode(',', $rule);
- return $rules;
- }
- //读取部门列表
- 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;
- }
- //读取导航列表,用于后台
- function get_nav($nav_id)
- {
- $nav = Db::name('NavInfo')->where('nav_id', $nav_id)->order('sort asc')->select();
- return $nav;
- }
- //读取关键字列表
- function get_keywords()
- {
- $keywords = Db::name('Keywords')->where(['status' => 1])->order('create_time asc')->select();
- return $keywords;
- }
- //读取文章分类列表
- function get_article_cate()
- {
- $cate = Db::name('ArticleCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
- return $cate;
- }
- //读取图集分类列表
- function get_gallery_cate()
- {
- $cate = Db::name('GalleryCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
- return $cate;
- }
- //读取商品分类列表
- function get_goods_cate()
- {
- $cate = Db::name('GoodsCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
- return $cate;
- }
- //访问按小时归档统计
- 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;
- }
- //访问按日期归档统计
- 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;
- }
- /**
- * 管理员操作日志
- * @param string $type 操作类型 login add edit view delete
- * @param int $param_id 操作类型
- * @param array $param 提交的参数
- * @param subject $param 操作主题
- */
- function add_log($type, $param_id = '', $param = [],$subject='')
- {
- $action = '未知操作';
- $type_action = get_config('log.admin_action');
- if($type_action[$type]){
- $action = $type_action[$type];
- }
- if ($type == 'login') {
- $login_admin = Db::name('Admin')->where(array('id' => $param_id))->find();
- } else {
- $session_admin = get_config('app.session_admin');
- $login_admin = \think\facade\Session::get($session_admin);
- }
- $data = [];
- $data['title'] = '';
- $data['uid'] = $login_admin['id'];
- $data['nickname'] = $login_admin['nickname'];
- $data['type'] = $type;
- $data['action'] = $action;
- $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());
- $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{
- if(empty($subject)){
- $data['subject'] = '系统';
- }else{
- $data['subject'] = $subject;
- }
- }
- $content = $login_admin['nickname'] . '在' . date('Y-m-d H:i:s') . $data['action'] . '了' . $data['subject'];
- $data['content'] = $content;
- $data['ip'] = app('request')->ip();
- $data['create_time'] = time();
- Db::name('AdminLog')->strict(false)->field(true)->insert($data);
- }
- /**
- * 项目操作日志
- *
- */
- function add_project_log($action, $project_id,$content=''){
- $data = array();
- $session_admin = get_config('app.session_admin');
- $login_admin = \think\facade\Session::get($session_admin);
- $data['uid'] = $login_admin['id'];
- $data['nickname'] = $login_admin['nickname'];
- $data['unit_name'] = $login_admin['unit_name'];
- $data['project_id'] = $project_id;
- $project = Db::name('CostProject')->where('id', $project_id)->find();
- $data['project_name'] = $project['project_name'];
- $data['project_status'] = $project['project_status'];
- $data['action'] = $action;
- $data['ip'] = app('request')->ip();
- $data['create_time'] = time();
- // halt($data);
- if(isset($content)){
- $data["content"] = $content;
- }
- Db::name('ProjectLog')->strict(false)->field(true)->insert($data);
- }
- //递归返回树形菜单数据
- function get_tree($data, $pId ,$open=0,$deep=0)
- {
- $tree = [];
- foreach($data as $k => $v)
- {
- $v['checkArr']=array('type'=>0, 'isChecked'=>0);
- $v['spread']=true;
- $v['parentId']=$v['pid'];
- if($deep>=$open){
- $v['spread']=false;
- }
- $v['name']=$v['title'];
- if($v['pid'] == $pId){
- //父亲找到儿子
- $deep++;
- $v['children'] = get_tree($data, $v['id'],$open,$deep);
- $tree[] = $v;
- //unset($data[$k]);
- }
- }
- return array_values($tree);
- }
- //获取顶级部门id
- function get_unit($unit_id){
- $unit_pid = Db::name('Department')->where("id",$unit_id)->field(["pid","id"])->find();
- for ($i = $unit_id;$i>0;) {
- $unit_pid = Db::name('Department')->where("id", $i)->field(["pid", "id"])->find();
- $i = $unit_pid["pid"];
- if ($i == 0) {
- return $unit_pid["id"];
- }
- }
- }
- /**
- * 附件样式
- */
- function fileCard($item) {
- $li = '';
- $host = $_SERVER['HTTP_HOST'];
- if(count($item) > 0) {
- for($a = 0; $a < count($item); $a++) {
- $image = ['jpg', 'jpeg', 'png', 'gif'];
- $doc = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'pdf', 'zip', 'rar', '7z'];
- $down ='<a href="' . $item[$a]['filepath'].'" target="_blank" class="layui-btn layui-btn-xs layui-btn-normal" download="'.$item[$a]['name'].'">预览</a>';
- $down1 = '<a href="'. $item[$a]['filepath'] . "?attname=" . $item[$a]['filepath'] .'" target="_blank" class="layui-btn layui-btn-xs layui-btn-normal file_download" download="'.$item[$a]['name'].'">下载</a>';
- // 判断元素是否在数组中
- $path = '/static/home/images/icon/file.png';
- if (in_array($item[$a]['fileext'], $image)) {
- $path = $item[$a]['filepath'];
- $down = '<span data-href="'.$item[$a]['filepath'].'" class="layui-btn layui-btn-xs layui-btn-normal file-view-img">预览</span>';
- } else if (in_array($item[$a]['fileext'], $doc)) {
- $path = '/static/home/images/icon/'.$item[$a]['fileext'].'.png';
- }
- if($item[$a]['fileext'] == 'pdf') {
- $down = '<span data-href="'.$item[$a]['filepath'].'" class="layui-btn layui-btn-xs layui-btn-normal file-view-pdf">预览</span>';
- }
- $li .= '<li id="'.$item[$a]['id'].'" data-id="'.$item[$a]['id'].'" data-title="'.$item[$a]['name'].'" data-ext="'.$item[$a]['fileext'].'"><a href="'.$item[$a]['filepath'].'" target="_blank"download="'.$item[$a]['name'].'"><img src="'.$path.'" alt="'.$item[$a]['name'].'" style="object-fit: contain;height:5vw;" class="file-item"></a><p title="'.$item[$a]['name'].'">'.$item[$a]['name'].'</p><div class="layui-btn-group">'.'</div></li>';
- }
- return $li;
- }
- }
- /**
- * 计算按天数
- */
- function countDays($a, $b = 0)
- {
- if ($b == 0) {
- $b = date("Y-m-d");
- }
- $date_1 = $a;
- $date_2 = $b;
- $d1 = strtotime($date_1);
- $d2 = strtotime($date_2);
- $days = round(($d2 - $d1) / 3600 / 24);
- if ($days > 0) {
- return $days;
- } else {
- return 0;
- }
- }
- //读取分类列表
- function contract_cate()
- {
- $cate = Db::name('ContractCate')->where(['status' => 1])->order('id desc')->select()->toArray();
- return $cate;
- }
|