+----------------------------------------------------------------------------------------------- */ // 应用公共文件,内置主要的数据处理方法 use think\facade\Config; use think\facade\Request; use think\facade\Cache; use think\facade\Db; /***************************************************系统相关*****************************************************/ //设置缓存 function set_cache($key='', $value='', $date = 86400) { Cache::set($key, $value, $date); } //读取缓存 function get_cache($key) { return Cache::get($key); } //清空缓存 function clear_cache($key) { Cache::clear($key); } //读取文件配置 function get_config($key) { return Config::get($key); } //读取系统配置 function get_system_config($name='', $key = '') { $config = []; if (get_cache('system_config' . $name)) { $config = get_cache('system_config' . $name); } else { $conf = Db::name('config')->where('name', $name)->find(); if (isset($conf['content'])) { $config = unserialize($conf['content']); } set_cache('system_config' . $name, $config); } if ($key == '') { return $config; } else { if (isset($config[$key])) { return $config[$key]; } else{ return ''; } } } //设置系统配置 function set_system_config($name='', $key='', $value='') { $config = []; $conf = Db::name('config')->where('name', $name)->find(); if ($conf['content']) { $config = unserialize($conf['content']); } $config[$key] = $value; set_cache('system_config' . $name, $config); $content = serialize($config); Db::name('config')->where('name', $name)->update(['content'=>$content]); } //判断系统是否已安装 function is_installed() { static $isInstalled; if (empty($isInstalled)) { $isInstalled = file_exists(CMS_ROOT . 'config/install.lock'); } return $isInstalled; } //判断系统是否存在模板 function isTemplate($url='') { static $isTemplate; if (empty($isTemplate)) { $isTemplate = file_exists(CMS_ROOT . 'app/'.$url); } return $isTemplate; } //判断模块是否禁用 function isModule($name) { $map = []; $map[] = ['name', '=', $name]; $map[] = ['status', '=', 1]; $count = Db::name('AdminModule')->where($map)->count(); return $count; } //获取某模块的某数据配置 function valueAuth($name,$conf) { $map = []; $map[] = ['name', '=', $name]; $val = Db::name('DataAuth')->where($map)->value($conf); return $val; } //是否是某模块的数据权限,>1即有权限,$uid,要鉴别的用户,$name模块名称,$conf权限类型(字段), function isAuth($uid,$name,$conf) { if($uid == 1){ return 1; } $map = []; $map[] = ['name', '=', $name]; $map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',$conf)")]; $count = Db::name('DataAuth')->where($map)->count(); return $count; } //判断是否是部门负责人, function isLeader($uid = 0,$did='') { if($uid == 1){ return 1; } $map = []; $map[] = ['status','=',1]; if(!empty($did)){ $map[] = ['id','in',$did]; } $map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',leader_ids)")]; $count = Db::name('Department')->where($map)->count(); return $count; } //获取url参数,$key为空时返回所有参数数组 function get_params($key = "") { return Request::instance()->param($key); } /** * 菜单节点权限判断 * @rule String * @uid Int * @return bool */ function check_auth($rule, $uid) { $auth_list = Cache::get('RulesSrc' . $uid); if (!in_array($rule, $auth_list)) { return false; } else { return true; } } /** * 返回json数据,用于接口 * @param integer $code * @param string $msg * @param array $data * @param string $url * @param integer $httpCode * @param array $header * @param array $options * @return json */ function to_assign($code = 0, $msg = "操作成功", $data = [], $action = '', $url = '', $httpCode = 200, $header = [], $options = []) { $res = ['code' => $code]; $res['msg'] = $msg; $res['action'] = $action; $res['url'] = $url; if (is_object($data)) { $data = $data->toArray(); } $res['data'] = $data; $response = \think\Response::create($res, "json", $httpCode, $header, $options); throw new \think\exception\HttpResponseException($response); } /** * 适配layui table数据列表的返回数据方法,用于接口 * @param integer $code * @param string $msg * @param array $data * @param integer $httpCode * @param array $header * @param array $options * @return json */ function table_assign($code = 0, $msg = '请求成功', $data = [],$totalRow = [], $httpCode = 200, $header = [], $options = []) { $res['code'] = $code; $res['msg'] = $msg; $res['totalRow'] = $totalRow; if (is_object($data)) { $data = $data->toArray(); } if (!empty($data['total'])) { $res['count'] = $data['total']; } else { $res['count'] = 0; } $res['data'] = $data['data']; $response = \think\Response::create($res, "json", $httpCode, $header, $options); throw new \think\exception\HttpResponseException($response); } //写入操作日主 function add_log($type, $param_id = 0, $param = [] ,$subject='') { try { // 可能会抛出异常的代码 $title = '操作'; $session_admin = get_config('app.session_admin'); $uid = \think\facade\Session::get($session_admin); $type_action = get_config('log.type_action'); if($type_action[$type]){ $title = $type_action[$type]; } $data = [ 'uid' => $uid, 'type' => $type, 'action' => $title, 'param_id' => $param_id, 'param' => json_encode($param), 'module' => strtolower(app('http')->getName()), 'controller' => strtolower(app('request')->controller()), 'function' => strtolower(app('request')->action()), 'ip' => app('request')->ip(), 'create_time' => time(), 'subject' => 'OA系统' ]; if($subject!=''){ $data['subject'] =$subject; } else{ $rule = $data['module'] . '/' . $data['controller'] . '/' . $data['function']; $rule_menu = Db::name('AdminRule')->where(array('src' => $rule))->find(); if($rule_menu){ $data['subject'] = $rule_menu['name']; } } Db::name('AdminLog')->strict(false)->field(true)->insert($data); } catch (\Exception $e) { // 处理异常,记录日志或者其他逻辑 // 但不要抛出异常,以免中断主程序流程 } } //消息链接信息转换 function get_message_link($template_id,$action_id){ $content=''; $template = Db::name('Template')->where('id',$template_id)->find(); $link = $template['msg_link']; if(!empty($link)){ $content = str_replace('{action_id}', $action_id, $link); } return '查看详情'; } function get_message_mobile($template_id,$action_id){ $content=''; $template = Db::name('Template')->where('id',$template_id)->find(); $link = $template['msg_link']; if(!empty($link)){ $content = str_replace('{action_id}', $action_id, $link); } return '查看详情'; } /** * 邮件发送 * @param $to 接收人 * @param string $subject 邮件标题 * @param string $content 邮件内容(html模板渲染后的内容) * @throws Exception * @throws phpmailerException */ function send_email($to, $subject = '', $content = '') { $mail = new PHPMailer\PHPMailer\PHPMailer(); $email_config = Db::name('config')->where('name', 'email')->find(); $config = unserialize($email_config['content']); $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 $mail->isSMTP(); $mail->SMTPDebug = 0; //调试输出格式 //$mail->Debugoutput = 'html'; //smtp服务器 $mail->Host = $config['smtp']; //端口 - likely to be 25, 465 or 587 $mail->Port = $config['smtp_port']; if ($mail->Port == '465') { $mail->SMTPSecure = 'ssl'; // 使用安全协议 } //Whether to use SMTP authentication $mail->SMTPAuth = true; //发送邮箱 $mail->Username = $config['smtp_user']; //密码 $mail->Password = $config['smtp_pwd']; //Set who the message is to be sent from $mail->setFrom($config['email'], $config['from']); //回复地址 //$mail->addReplyTo('replyto@example.com', 'First Last'); //接收邮件方 if (is_array($to)) { foreach ($to as $v) { $mail->addAddress($v); } } else { $mail->addAddress($to); } $mail->isHTML(true); // send as HTML //标题 $mail->Subject = $subject; //HTML内容转换 $mail->msgHTML($content); $status = $mail->send(); if ($status) { return true; } else { // echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息 // die; return false; } } /***************************************************员工相关*****************************************************/ //获取指定用户的信息 function get_admin($uid) { $admin = Db::name('Admin') ->alias('a') ->field('a.*,d.title as department,p.title as position') ->leftJoin ('Department d ','d.id= a.did') ->leftJoin ('Position p ','p.id= a.position_id') ->where(['a.id' => $uid]) ->find(); return $admin; } //获取指定部门所有的员工信息(包含主部门、次要部门,如果son=1,则包含当前子部门) function get_department_employee($did=0,$son=0) { $admin_array = []; $department_array = []; $department_array[] = $did; if($son==1){ $department_array = get_department_son($did); $admin_array = Db::name('DepartmentAdmin')->whereIn('department_id',$department_array)->column('admin_id'); } else{ $admin_array = Db::name('DepartmentAdmin')->where('department_id',$did)->column('admin_id'); } $map1=[ ['id','in',$admin_array], ]; $map2=[ ['did','in',$department_array], ]; $where=[['id','>',1],['status','=',1]]; $whereOr =[$map1,$map2]; $admin = Db::name('Admin') ->where(function ($query) use($whereOr) { if (!empty($whereOr)){ $query->whereOr($whereOr); } }) ->where($where)->select()->toArray(); return $admin; } /***************************************************部门相关*****************************************************/ //读取部门列表 function get_department() { $department = Db::name('Department')->order('sort desc,id asc')->where(['status' => 1])->select()->toArray(); return $department; } //读取部门名称 function get_department_name($dids) { $departments = Db::name('Department')->where([['id','in',$dids],['status','=',1]])->column('title'); if(!empty($departments)){ return implode(',',$departments); }else{ return ''; } } //获取某部门的子部门id,$is_self是否包含自己 //输出部门数组,如:['1,2,3'] 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; } //读取某员工所在主部门的负责人(pid=1,上一级部门负责人) //输出负责人数组,如:1,2,3 function get_department_leader($uid=0,$pid=0) { $did = get_admin($uid)['did']; if($pid==1){ $did = Db::name('Department')->where('id',$did)->value('pid'); } $leader_ids = Db::name('Department')->where('id',$did)->value('leader_ids'); return $leader_ids; } //获取某员工所在部门的顶级部门(如果默认顶级部门当做是分公司的,即是获取某员工所属分公司) function get_department_top($did=0,$uid=0) { if($uid>0){ $did = get_admin($uid)['did']; } $top_id = $did; if($did>0){ $pid = Db::name('Department')->where('id',$did)->value('pid'); if($pid>0){ $top_id = get_department_top($pid,0); } } return $top_id; } //获取某负责人所负责的部门的数据集(ids) function get_leader_departments($uid = 0) { $dids = Db::name('Admin')->where('id',$uid)->value('son_dids'); if(empty($dids)){ return []; } else{ return explode(',',$dids); } } //获取某员工所能看的部门数据(dids) function get_role_departments($uid = 0) { $dids = Db::name('Admin')->where('id',$uid)->value('auth_dids'); if(empty($dids)){ return []; } else{ return explode(',',$dids); } } /***************************************************审批相关*****************************************************/ //获取全部审批状态 function get_check_status() { $check_status_array = ['待提交审批','审批中','审批通过','审批不通过','已撤回']; return $check_status_array; } //根据审批状态读取审批状态名称 function check_status_name($check_status=0) { $check_status_array = get_check_status(); return $check_status_array[$check_status]; } //根据流程类型读取某部门某模块的审核流程 function get_cate_department_flows($cate=1,$department=0) { $map1 = []; $map2 = []; $map1[] = ['status', '=', 1]; $map1[] = ['flow_cate', '=', $cate]; $map1[] = ['department_ids', '=', '']; $map2[] = ['status', '=', 1]; $map2[] = ['flow_cate', '=', $cate]; $map2[] = ['', 'exp', Db::raw("FIND_IN_SET('{$department}',department_ids)")]; $list = Db::name('Flow')->field('id,name,check_type')->whereOr([$map1,$map2])->order('id desc')->select()->toArray(); return $list; } //根据流程所属模块读取某部门某模块的审核流程 function get_type_department_flows($type=6,$department=0) { $map1 = []; $map2 = []; $map1[] = ['status', '=', 1]; $map1[] = ['type', '=', $type]; $map1[] = ['department_ids', '=', '']; $map2[] = ['status', '=', 1]; $map2[] = ['type', '=', $type]; $map2[] = ['', 'exp', Db::raw("FIND_IN_SET('{$department}',department_ids)")]; $list = Db::name('Flow')->field('id,name,check_type')->whereOr([$map1,$map2])->order('id desc')->select()->toArray(); return $list; } /** * 初始化审批流程数据 * @param $flow_id 审批流程id * @param $check_admin_ids 传入当前审批人ids,用于设置当前审批步骤的审批人 * @param $uid 传入当前登录用户id,用于查找其所在部门的负责人或者上一部门负责人 * @return */ function set_flow($flow_id,$check_admin_ids,$uid) { $flow_detail = Db::name('Flow')->where('id',$flow_id)->find(); $check_type = $flow_detail['check_type']; $flow = unserialize($flow_detail['flow_list']); if ($check_type == 1) { if($flow[0]['flow_type'] == 1){ //部门负责人 $leader = get_department_leader($uid); if($leader == 0){ return to_assign(1,'审批流程设置有问题:当前部门负责人还未设置,请联系HR或者管理员'); } else{ $check_admin_ids = $leader; } } else if($flow[0]['flow_type'] == 2){ //上级部门负责人 $leader = get_department_leader($uid,1); if($leader == 0){ return to_assign(1,'审批流程设置有问题:上级部门负责人还未设置,请联系HR或者管理员'); } else{ $check_admin_ids = $leader; } } else{ $check_admin_ids = $flow[0]['flow_uids']; } } else if ($check_type == 3) { $check_admin_ids = $flow[0]['flow_uids']; } $flow_data = array( 'check_type' => $check_type, 'flow' => $flow, 'check_admin_ids' => $check_admin_ids ); return $flow_data; } /** * 获取审批流程数据 * @param $uid 当前登录用户 * @param $flows 当前步骤内容 * @return */ function get_flow($uid,$flows) { $check_user = ''; $check_user_ids = []; if($flows['flow_type']==1){ $check_user = '部门负责人-'; $check_user_ids[]=get_department_leader($uid); } else if($flows['flow_type']==2){ $check_user = '上级部门负责人-'; $check_user_ids[]=get_department_leader($uid,1); } else{ $check_user_ids = explode(',',$flows['flow_uids']); } $check_user_array = Db::name('Admin')->where('id','in',$check_user_ids)->column('name'); $res = array( 'check_user' => $check_user.implode(',',$check_user_array), 'check_user_ids' => $check_user_ids ); return $res; } /** * 获取审批记录数据 * @param $check_table 关联内容表 * @param $action_id 关联内容记录id * @return */ function get_check_record($check_table,$action_id) { $check_record = Db::name('FlowRecord') ->field('f.*,a.name') ->alias('f')->join('Admin a', 'a.id = f.check_uid', 'left') ->where([['f.check_table','=',$check_table],['f.action_id','=',$action_id],['f.delete_time','=',0]]) ->select()->toArray(); foreach ($check_record as $kk => &$vv) { $vv['check_time_str'] = date('Y-m-d H:i', $vv['check_time']); $vv['status_str'] = '提交申请'; if($vv['check_status'] == 1){ $vv['status_str'] = '审核通过'; } else if($vv['check_status'] == 2){ $vv['status_str'] = '审核拒绝'; } if($vv['check_status'] == 3){ $vv['status_str'] = '撤销申请'; } if($vv['check_status'] == 4){ $vv['status_str'] = '反确认'; } } return $check_record; } /***************************************************常规数据获取*****************************************************/ //读取基础数据 function get_base_data($table) { $data = Db::name($table)->where(['status' => 1])->select()->toArray(); return $data; } //读取模块基础数据 function get_base_type_data($table,$type) { $data = Db::name($table)->where(['status' => 1,'types' => $type])->select()->toArray(); return $data; } //读取所属地区 function get_region_name($id){ $region = Db::name('city')->where(['id'=>$id])->find(); if(empty($region)){ return ''; } else{ return $region['name']; } } //读取分类子分类ids,返回id数组 function get_cate_son($table='',$id=0,$is_self = 1) { $cate = Db::name($table)->order('id desc')->select()->toArray(); $cate_list = get_data_node($cate, $id); $ids_array = array_column($cate_list, 'id'); if ($is_self == 1) { //包括自己在内 $ids_array[] = $id; } return $ids_array; } /** * 根据附件表的id返回url地址 * @param [type] $id [description] */ function get_file($id) { if ($id) { $geturl = Db::name("file")->where(['id' => $id])->find(); if ($geturl['status'] == 1) { //审核通过 //获取签名的URL $url = $geturl['filepath']; return $url; } elseif ($geturl['status'] == 0) { //待审核 return '/static/home/images/none_pic.jpg'; } else { //不通过 return '/static/home/images/none_pic.jpg'; } } return false; } /***************************************************工具函数相关*****************************************************/ //生成一个不会重复的字符串 function make_token() { $str = md5(uniqid(md5(microtime(true)), true)); $str = sha1($str); //加密 return $str; } //随机字符串,默认长度10 function set_salt($num = 10) { $str = 'qwertyuiopasdfghjklzxcvbnm1234567890'; $salt = substr(str_shuffle($str), 10, $num); return $salt; } //密码加密 function set_password($pwd, $salt) { return md5(md5($pwd . $salt) . $salt); } /** * 生成时间编号 * $prefix前缀 */ function get_codeno($prefix=1){ $no = $prefix . date('YmdHis') . rand(1,9); return $no; } /** * 去除空格 * @param string $str 字符串 * @return string 字符串 */ function trim_space($str=''){ $str = mb_ereg_replace('^( | )+', '', $str); $str = mb_ereg_replace('( | )+$', '', $str); return mb_ereg_replace(' ', "\n ", $str); } /** * 隐藏电话号码中间4位和邮箱 */ function hidetel($phone) { //隐藏邮箱 if (strpos($phone, '@')) { $email_array = explode("@", $phone); $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($phone, 0, 3); //邮箱前缀 $count = 0; $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $phone, -1, $count); $rs = $prevfix . $str; return $rs; } else { //隐藏联系方式中间4位 $Istelephone = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i', $phone); //固定电话 if ($Istelephone) { return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i', '$1****$2', $phone); } else { return preg_replace('/(1[0-9]{1}[0-9])[0-9]{4}([0-9]{4})/i', '$1****$2', $phone); } } } /** * 间隔时间段格式化 * @param int $time 时间戳 * @param string $format 格式 【d:显示到天 i显示到分钟 s显示到秒】 * @return string */ function time_trans($time, $format = 'd') { $now = time(); if (!is_numeric($time)) { $time = strtotime($time); } $diff = $now - $time; if ($diff < 60) { return '1分钟前'; } else if ($diff < 3600) { return floor($diff / 60) . '分钟前'; } else if ($diff < 86400) { return floor($diff / 3600) . '小时前'; } $yes_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('-1 days'))); //昨天开始时间 $yes_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-1 days'))); //昨天结束时间 $two_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-2 days'))); //2天前结束时间 $three_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-3 days'))); //3天前结束时间 $four_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-4 days'))); //4天前结束时间 $five_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-5 days'))); //5天前结束时间 $six_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-6 days'))); //6天前结束时间 $seven_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-7 days'))); //7天前结束时间 if ($time > $yes_start_time && $time < $yes_end_time) { return '昨天'; } if ($time > $yes_start_time && $time < $two_end_time) { return '1天前'; } if ($time > $yes_start_time && $time < $three_end_time) { return '2天前'; } if ($time > $yes_start_time && $time < $four_end_time) { return '3天前'; } if ($time > $yes_start_time && $time < $five_end_time) { return '4天前'; } if ($time > $yes_start_time && $time < $six_end_time) { return '5天前'; } if ($time > $yes_start_time && $time < $seven_end_time) { return '6天前'; } switch ($format) { case 'd': $show_time = date('Y-m-d', $time); break; case 'i': $show_time = date('Y-m-d H:i', $time); break; case 's': $show_time = date('Y-m-d H:i:s', $time); break; } return $show_time; } /** * 时间戳格式化 * @param int $time * @param string $format 默认'Y-m-d H:i:s' * @return string 完整的时间显示 */ function to_date($time = NULL, $format = 'Y-m-d H:i:s') { if(empty($time)){ return ''; } else{ if (is_numeric($time)) { return date($format, intval($time)); } else{ return $time; } } } /** * 计算按相差天数 */ function count_days($a=0, $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; } } /** * @Method: 文件大小格式化 * @param[type] $file_size [文件大小] */ function to_size($file_size){ $file_size = $file_size-1; if ($file_size >= 1099511627776){ $show_filesize = number_format(($file_size / 1099511627776),2) . " TB"; } elseif ($file_size >= 1073741824) { $show_filesize = number_format(($file_size / 1073741824),2) . " GB"; } elseif ($file_size >= 1048576) { $show_filesize = number_format(($file_size / 1048576),2) . " MB"; } elseif ($file_size >= 1024) { $show_filesize = number_format(($file_size / 1024),2) . " KB"; } elseif ($file_size > 0) { $show_filesize = $file_size . " b"; } elseif ($file_size == 0 || $file_size == -1) { $show_filesize = "0 b"; } return $show_filesize; } //格式化附件展示 function file_card($file,$view=''){ if(empty($file['file_id'])){ $file['file_id'] = $file['id']; } $image=['jpg','jpeg','png','gif']; $office=['doc','docx','xls','xlsx','ppt','pptx']; $type_icon = 'icon-xiangmuguanli'; $type = 0;//0下载+重命名+删除,1下载+查看+重命名+删除,2下载+查看+编辑+重命名+删除 $ext = 'zip'; $view_btn = ''; if($file['fileext'] == 'pdf'){ $type_icon = 'icon-kejian'; $ext = 'pdf'; $type = 1; } if(in_array($file['fileext'], $image)){ $type_icon = 'icon-sucaiguanli'; $ext = 'image'; $type = 1; } if(in_array($file['fileext'], $office)){ $type_icon = 'icon-shenbao'; $ext = 'office'; $type = 2; } if(empty($view)){ $view_btn = ''; } else{ $view_btn = ''; } $file_del=''; if(!empty($file['delete_time'])){ $file_del = 'file-hasdelete'; } $item = '