where('name', $name)->find();
if ($conf['content']) {
$config = unserialize($conf['content']);
}
set_cache('system_config' . $name, $config);
}
if ($key == '') {
return $config;
} else {
if (isset($config[$key])) {
return $config[$key];
}
}
}
//读取文件配置
function get_config($key)
{
return Config::get($key);
}
//判断cms是否完成安装
function is_installed()
{
static $isInstalled;
if (empty($isInstalled)) {
$isInstalled = file_exists(CMS_ROOT . 'config/install.lock');
}
return $isInstalled;
}
//获取服务器信息
function get_system_info($key)
{
$system = [
'os' => PHP_OS,
'php' => PHP_VERSION,
'upload_max_filesize' => get_cfg_var("upload_max_filesize") ? get_cfg_var("upload_max_filesize") : "不允许上传附件",
'max_execution_time' => get_cfg_var("max_execution_time") . "秒 ",
];
if (empty($key)) {
return $system;
} else {
return $system[$key];
}
}
//获取菜单
function get_menus($pid = 0)
{
$admin = get_login_admin();
if (get_cache('menu' . $admin['id'])) {
$menu = get_cache('menu' . $admin['id']);
} else {
$adminGroup = Db::name('PositionGroup')->where(['pid' => $admin['position_id']])->column('group_id');
$adminMenu = Db::name('AdminGroup')->where('id', 'in', $adminGroup)->column('rules');
$adminMenus = [];
foreach ($adminMenu as $k => $v) {
$v = explode(',', $v);
$adminMenus = array_merge($adminMenus, $v);
}
$menu = Db::name('AdminRule')->field('id,pid,src,title,name,icon')->where(['menu' => 1, 'status' => 1])->where('id', 'in', $adminMenus)->order('sort asc')->select()->toArray();
Cache::tag('adminMenu')->set('menu' . $admin['id'], $menu);
}
if ($pid > 0) {
$menus = get_data_node($menu, $pid);
$list = list_to_tree($menus, 'id', 'pid', 'list', $pid);
} else {
$menus = $menu;
$list = list_to_tree($menus);
}
return $list;
}
//获取url参数
function get_params($key = "")
{
return Request::instance()->param($key);
}
//生成一个不会重复的字符串
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);
}
//获取指定管理员的信息
function get_admin($id)
{
$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' => $id])
->cache(true, 60)
->find();
$admin['last_login_time'] = empty($admin['last_login_time']) ? '-' : date('Y-m-d H:i', $admin['last_login_time']);
return $admin;
}
//获取当前登录用户的id
function get_login_admin_id()
{
$session_admin = get_config('app.session_admin');
if (\think\facade\Session::has($session_admin)) {
$gougu_admin = \think\facade\Session::get($session_admin);
return $gougu_admin['id'];
} else {
return 0;
}
}
//获取当前登录用户的信息
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);
$admin = get_admin($gougu_admin['id']);
if (!empty($key)) {
if (isset($admin[$key])) {
return $admin[$key];
} else {
return '';
}
} else {
return $admin;
}
} else {
return '';
}
}
/**
* 节点权限判断
* @return bool
*/
function check_auth($rule, $uid)
{
$auth_list = Cache::get('RulesSrc' . $uid);
if (!in_array($rule, $auth_list)) {
return false;
} else {
return true;
}
}
//读取部门列表
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_user_role($leader_id = 0, $uid = 0)
{
$did = get_admin($uid)['did'];
//获取子部门
$department = get_department();
$department_list = get_data_node($department, $did);
$department_array = array_column($department_list, 'id');
//包括自己部门在内
$department_array[] = $did;
//判断是否是部门负责人
$is_leader = Db::name('Department')->where([['id', 'in', $did], ['leader_id', '=', $leader_id]])->count();
return $is_leader;
}
//读取根据uid判断是否是某部门的领导,返回所在部门和所管理的子部门did
function get_leader_dids($uid = 0)
{
$did = get_admin($uid)['did'];
$department_array = [];
//判断是否是部门负责人
$is_leader = Db::name('Department')->where(['id' => $did, 'leader_id' => $uid])->count();
if ($is_leader > 0 || $uid == 1) {
//获取子部门
$department = get_department();
$department_list = get_data_node($department, $did);
$department_array = array_column($department_list, 'id');
//包括自己部门在内
$department_array[] = $did;
} else {
//包括自己部门在内
$department_array[] = $did;
}
return $department_array;
}
//读取职位
function get_position()
{
$position = Db::name('Position')->where(['status' => 1])->select()->toArray();
return $position;
}
//读取产品
function get_product()
{
$product = Db::name('Product')->where(['delete_time' => 0])->select()->toArray();
return $product;
}
//读取项目
function get_project($uid = 0)
{
$map = [];
$map[] = ['delete_time', '=', 0];
if ($uid > 0) {
$project_ids = Db::name('ProjectUser')->where(['uid' => $uid, 'delete_time' => 0])->column('project_id');
$map[] = ['id', 'in', $project_ids];
}
$project = Db::name('Project')->where($map)->select()->toArray();
return $project;
}
//读取任务
function get_task()
{
$tasks = Db::name('Task')->where(['delete_time' => 0])->select()->toArray();
return $tasks;
}
//读取用户名称
function get_admin_name()
{
$names = Db::name('Admin')->where('status', 1)->select()->toArray();
return $names;
}
/**
* 获取只属于自己的任务
* @return array
*/
function get_onlyme()
{
$tasks = Db::name('Task')->select()->toArray();
$names = [];
foreach ($tasks as $task) {
$admin = Db::name('Admin')->where('name', $task['11'])->find();
if ($admin) {
$names[] = $admin['name'];
}
}
return $names;
}
//读取公告类型
function get_note_cate()
{
$cate = Db::name('NoteCate')->order('id desc')->select()->toArray();
return $cate;
}
//读取工作类型
function get_work_cate()
{
$cate = Db::name('WorkCate')->where('status', 1)->select()->toArray();
return $cate;
}
//读取任务类型
function get_task_cate()
{
$cate = Db::name('TaskCate')->where('status', 1)->select()->toArray();
return $cate;
}
/**
* 根据附件表的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;
}
/**
* 员工操作日志
* @param string $type 操作类型 login add edit view delete
* @param int $param_id 操作类型
* @param array $param 提交的参数
*/
function add_log($type, $param_id = 0, $param = [], $old = [], $subject = '')
{
$action = '未知操作';
$type_action = get_config('log.type_action');
$data = [];
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['uid'] = $login_admin['id'];
$data['name'] = $login_admin['name'];
$data['type'] = $type;
$data['action'] = $action;
$data['param_id'] = $param_id;
$data['param'] = json_encode($param);
$data['module'] = strtolower(app('http')->getName());
$data['controller'] = strtolower(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 {
$data['title'] = '';
if ($subject != '') {
$data['subject'] = $subject;
} else {
$data['subject'] = '系统';
}
}
$content = $login_admin['name'] . '在' . 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);
if (!empty($old)) {
$log_data = [];
$key_array = ['id', 'create_time', 'update_time', 'delete_time', 'over_time', 'md_content'];
foreach ($param as $key => $value) {
if (!in_array($key, $key_array)) {
$log_data[] = array(
'module' => $data['module'],
'field' => $key,
$data['module'] . '_id' => $param_id,
'admin_id' => $data['uid'],
'old_content' => $old[$key],
'new_content' => $value,
'create_time' => time(),
);
}
}
Db::name('Log')->strict(false)->field(true)->insertAll($log_data);
}
}
/**
* 发送任务消息给钉钉个人
* @param $title 任务标题
* @param $text1 任务提示
* @param $text2 任务指派相关内容
* @param $link 项目链接地址
* @param $user_name 任务指派给谁
* @return
*/
function send_dd_gr($title, $text1, $text2, $link, $user_name = [])
{
// 发送消息给钉钉个人
// 定义数据类型
$header = array('Content-Type: application/json');
// 钉钉个人机器人地址
$webhook = 'https://oapi.dingtalk.com/robot/send?access_token=XXXXX';
// 发布消息内容模板
$msg = "{$text1}\n\n{$title}\n\n{$text2}\n\n{$link}";
$data = array(
'msgtype' => 'text',
'text' => array(
'content' => $msg
),
'at' => array(
'atMobiles' => array(),
'atUserIds' => array($user_name),
'isAtAll' => false
)
);
$options = array(
'http' => array(
'header' => $header,
'method' => 'POST',
'content' => json_encode($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($webhook, false, $context);
return '任务发布成功!
' . $title;
}
/**
* 发送任务消息给钉钉个人
* @param $title 任务标题
* @param $text1 任务提示
* @param $text2 任务指派相关内容
* @param $link 项目链接地址
* @param $user_name 任务指派给谁
* @return
*/
function send_dd_jqr($title, $text1, $text2, $link, $user_name = [])
{
// 发送消息给钉钉机器人
// 发送钉钉机器人消息
// 定义数据类型
$header = array('Content-Type: application/json');
// 警保项目群机器人地址
$webhook1 = 'https://oapi.dingtalk.com/robot/send?access_token=46ad1d7d5aa6e365a50a1a08608f1d89f16031a658bf729c32f38d369bcfc520';
// 功能测试群机器人地址
$webhook2 = 'https://oapi.dingtalk.com/robot/send?access_token=4448cd3d356856f7f91739b832f3ef466c2c1d8df6ce43a80e02661b6cfc3ea3';
// 选择要使用的webhook地址
$webhook = $webhook2;
// 发布消息内容模板
$msg = "{$text1}\n\n{$title}\n\n{$text2}\n\n{$link}";
$data = array(
'msgtype' => 'text',
'text' => array(
'content' => $msg
),
'at' => array(
'atMobiles' => array(),
'atUserIds' => array($user_name),
'isAtAll' => false
)
);
$options = array(
'http' => array(
'header' => $header,
'method' => 'POST',
'content' => json_encode($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($webhook, false, $context);
return '任务发布成功!
' . $title;
}
/**
* 发送站内信
* @param $user_id 接收人
* @param $template 消息模板
* @param $data 操作内容
* @return
*/
//发送站内信
function sendMessage($user_id, $template, $data = [])
{
$title = get_config('message.template')[$template]['title'];
$content = get_config('message.template')[$template]['content'];
$touid = get_config('message.template')[$template]['touid'];
$link = get_config('message.template')[$template]['link'];
foreach ($data as $key => $val) {
$title = str_replace('{' . $key . '}', $val, $title);
$content = str_replace('{' . $key . '}', $val, $content);
$touid = str_replace('{' . $key . '}', $val, $touid);
}
if (isset($data['from_uid'])) {
$title = str_replace('{from_user}', get_admin($data['from_uid'])['name'], $title);
$content = str_replace('{from_user}', get_admin($data['from_uid'])['name'], $content);
$touid = str_replace('{from_user}', get_admin($data['from_uid'])['name'], $touid);
}
$content = str_replace('{date}', date('Y-m-d'), $content);
if (!$user_id)
return false;
if (!$content)
return false;
if (!$touid)
return false;
if (!is_array($user_id)) {
$users = explode(",", strval($user_id));
} else {
$users = $user_id;
print_r('users:' . $users);
}
$users = array_unique(array_filter($users));
//组合要发的消息
$send_data = [];
foreach ($users as $key => $value) {
$send_data[] = array(
'to_uid' => $value,
//接收人
'action_id' => $data['action_id'],
'title' => $title,
'content' => $content,
'touid' => $data['touid'],
'template' => $template,
'module_name' => strtolower(app('http')->getName()),
'controller_name' => strtolower(app('request')->controller()),
'action_name' => strtolower(app('request')->action()),
'send_time' => time(),
'create_time' => time()
);
}
$res = Db::name('Message')->strict(false)->field(true)->insertAll($send_data);
return $res;
}
function getMessageLink($template, $action_id)
{
$content = '';
if (isset(get_config('message.template')[$template]['link'])) {
$link = get_config('message.template')[$template]['link'];
$content = str_replace('{action_id}', $action_id, $link);
}
return $content;
}
/**
* 邮件发送
* @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;
var_dump($to);
//调试输出格式
//$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 plan_count($arrData)
{
$documents = array();
foreach ($arrData as $index => $value) {
$planTime = date("Y-m-d", $value['end_time']);
if (empty($documents[$planTime])) {
$documents[$planTime] = 1;
} else {
$documents[$planTime] += 1;
}
}
return $documents;
}
//工时登记情况统计
function hour_count($arrData)
{
$documents = array();
foreach ($arrData as $index => $value) {
$hourTime = date("Y-m-d", $value['start_time']);
if (empty($documents[$hourTime])) {
$documents[$hourTime] = $value['labor_time'] + 0;
} else {
$documents[$hourTime] += $value['labor_time'];
}
$documents[$hourTime] = round($documents[$hourTime], 2);
}
return $documents;
}
//燃尽图统计
function cross_count($arrData)
{
$documents = array();
foreach ($arrData as $index => $value) {
$planTime = date("Y-m-d", $value['end_time']);
if (empty($documents[$planTime])) {
$documents[$planTime] = 1;
} else {
$documents[$planTime] += 1;
}
}
return $documents;
}
/**
* 截取文章摘要
* @return bool
*/
function get_desc_content($content, $count)
{
$content = preg_replace("@