123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- <?php
- // 设置编码
- session_start();
- header('Content-Type: text/html; charset=utf-8');
- header('Cache-Control: no-cache');
- date_default_timezone_set('Asia/Shanghai');
- // 数据库连接
- $conn = new mysqli("127.0.0.1", "crm", "Qweasdzxc", "crm_new");
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $conn->set_charset("utf8mb4");
- // 检查登录
- function checkLogin() {
- if (empty($_SESSION['employee_id'])) {
- echo "<script>top.location.href='index.php'</script>";
- exit;
- }
- }
- function checkAdmin() {
- if ((empty($_SESSION['em_permission_role_id'])||($_SESSION['em_permission_role_id']!=1))) {
- die("No permission , Please contact the administrator");
- exit;
- }
- }
- function checkPermissionDie(...$permission_role_ids) {
- // 检查会话中是否设置权限ID
- if (empty($_SESSION['em_permission_role_id'])) {
- die("No permission , Please contact the administrator");
- }
- // 如果是超级管理员(ID=1),直接返回true
- if ($_SESSION['em_permission_role_id'] == 1) {
- // return true;
- }
- // 检查当前角色ID是否在允许的角色ID中
- if(!in_array($_SESSION['em_permission_role_id'], $permission_role_ids))
- {
- die("No permission , Please contact the administrator");
- }
- }
- function checkPermission(...$permission_role_ids) {
- // 检查会话中是否设置权限ID
- if (empty($_SESSION['em_permission_role_id'])) {
- return false;
- }
- // 如果是超级管理员(ID=1),直接返回true
- if ($_SESSION['em_permission_role_id'] == 1) {
- return true;
- }
- // 检查当前角色ID是否在允许的角色ID中
- return in_array($_SESSION['em_permission_role_id'], $permission_role_ids);
- }
- // 检查管理员或组长或组员
- function checkAdminOrEmployee () {
- if ((empty($_SESSION['em_permission_role_id'])||($_SESSION['em_permission_role_id']!=1)&&($_SESSION['em_permission_role_id']!=2)&&($_SESSION['em_permission_role_id']!=3))) {
- return false;
- }
- else
- {
- return true;
- }
- }
- // 检查是否管理员
- function checkIfAdmin() {
- if ((empty($_SESSION['em_permission_role_id'])||($_SESSION['em_permission_role_id']!=1))) {
- return false;
- }
- else
- {
- return true;
- }
- }
- // 获取IP
- function getIp() {
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
- if (strlen($ip) > 15) {
- $ip = "UnKnow";
- }
- return htmlspecialcharsFix($ip);
- }
- // 记录操作日志
- function logAction($action) {
- global $conn;
-
- // 从SESSION获取当前用户名
- $loginName = $_SESSION['employee_name'] ?? 'Unknown User';
-
- // 获取当前IP
- $loginIp = getIp();
-
- // 当前时间
- $loginTime = date('Y-m-d H:i:s');
-
- // 记录到日志表
- $stmt = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES (
- '" . mysqli_real_escape_string($conn, $loginName) . "',
- '" . mysqli_real_escape_string($conn, $loginIp) . "',
- '$loginTime',
- '" . mysqli_real_escape_string($conn, $action) . "')";
-
- $conn->query($stmt);
- }
- // 移除HTML
- function removeHTML($t0) {
- if (empty($t0)) {
- return "";
- }
- $t0 = preg_replace("/<script.+?\/script>/is", "", $t0);
- $t0 = preg_replace("/<iframe.+?\/iframe>/is", "", $t0);
- $t0 = str_replace(["<", ">", " "], ["<", ">", ""], $t0);
- $t0 = preg_replace("/<.+?>/", "", $t0);
- return str_replace(["\r\n", "\t", "\r", "\n"], "", $t0);
- }
- // Text转HTML
- function txt2HTML($t0) {
- if (empty($t0) || is_array($t0)) {
- return "";
- }
- return str_replace(
- ["&", "\"", "<", ">", " "],
- ["&", """, "<", ">", " "],
- $t0
- );
- }
- // HTML转Text
- function html2Txt($t0) {
- if (empty($t0) || is_array($t0)) {
- return "";
- }
- return str_replace(
- [""", "<", ">", " ", "&"],
- ["\"", "<", ">", " ", "&"],
- $t0
- );
- }
- // HTML编码
- function htmlEncode($t0) {
- if (empty($t0) || is_array($t0)) {
- return "";
- }
- $replacements = [
- chr(38) => "&", chr(9) => "	", chr(11) => "",
- chr(10) => " ", chr(13) => " ", chr(32) => " ",
- chr(34) => """, chr(37) => "%", chr(39) => "'",
- chr(40) => "(", chr(41) => ")", chr(60) => "<",
- chr(62) => ">", chr(91) => "[", chr(93) => "]",
- chr(94) => "^", chr(95) => "_", chr(123) => "{",
- chr(124) => "|", chr(125) => "}"
- ];
- return strtr($t0, $replacements);
- }
- // HTML解码
- function htmlUnCode($t0) {
- if (empty($t0) || is_array($t0)) {
- return "";
- }
- $replacements = [
- "	" => chr(9), "" => chr(11), " " => chr(10),
- " " => chr(13), " " => chr(32), """ => chr(34),
- "%" => chr(37), "'" => chr(39), "(" => chr(40),
- ")" => chr(41), "<" => chr(60), ">" => chr(62),
- "[" => chr(91), "]" => chr(93), "^" => chr(94),
- "_" => chr(95), "{" => chr(123), "|" => chr(124),
- "}" => chr(125), "&" => chr(38)
- ];
- return strtr($t0, $replacements);
- }
- // 文本编码
- function textEncode($t0) {
- if (empty($t0) || is_array($t0)) {
- return "";
- }
- $t0 = trim($t0);
- $remove = [chr(8), chr(9), chr(11), chr(12), chr(10), chr(13)];
- $t0 = str_replace($remove, "", $t0);
- $replacements = [
- chr(38) => "&", chr(47) => "/", chr(32) => " ",
- chr(34) => """, chr(37) => "%", chr(39) => "'",
- chr(40) => "(", chr(41) => ")", "(" => "(",
- ")" => ")", chr(60) => "<", chr(62) => ">",
- chr(91) => "[", chr(93) => "]", chr(94) => "^",
- chr(95) => "_", chr(123) => "{", chr(124) => "|",
- chr(125) => "}"
- ];
- return strtr($t0, $replacements);
- }
- // 数字格式化
- function numFormat($t0) {
- if (empty($t0) || is_array($t0)) {
- return "";
- }
- $t0 = trim($t0);
- $remove = ["-", "+", " ", "&", " ", chr(34), "*", "%", "'", "(", ")", "<", ">",
- "[", "]", "^", "_", "{", "\\", "/", "|", "}", "(", ")"];
- return str_replace($remove, "", $t0);
- }
- // 文本解码
- function textUncode($t0) {
- if (empty($t0) || is_array($t0)) {
- return "";
- }
- $replacements = [
- " " => chr(32), """ => chr(34), "%" => chr(37),
- "'" => chr(39), "(" => chr(40), ")" => chr(41),
- "<" => chr(60), ">" => chr(62), "[" => chr(91),
- "]" => chr(93), "^" => chr(94), "_" => chr(95),
- "{" => chr(123), "|" => chr(124), "}" => chr(125),
- "/" => chr(47), "&" => chr(38)
- ];
- return strtr($t0, $replacements);
- }
- // HTML解码1
- function htmlUnCode1($t0) {
- if (empty($t0) || is_array($t0)) {
- return "";
- }
- $replacements = [
- "	" => chr(9), "" => chr(11), " " => "<br />",
- " " => "<br />", " " => "<br />", " " => " ",
- "&" => chr(38)
- ];
- return strtr($t0, $replacements);
- }
- // 格式化时间
- function formatTime($ttime, $tparam) {
- if (!strtotime($ttime)) {
- return "";
- }
- $date = new DateTime($ttime);
- $tsrt = $tparam;
- $replacements = [
- "yyyy" => $date->format("Y"),
- "yy" => $date->format("y"),
- "mm" => $date->format("m"),
- "dd" => $date->format("d"),
- "hh" => $date->format("H"),
- "ff" => $date->format("i"),
- "ss" => $date->format("s"),
- "m" => $date->format("n"),
- "d" => $date->format("j"),
- "h" => $date->format("G"),
- "f" => $date->format("i"),
- "s" => $date->format("s")
- ];
- return strtr($tsrt, $replacements);
- }
- // 英文月份
- function enMonth($m) {
- $months = [
- "1" => "Jan", "2" => "Feb", "3" => "Mar", "4" => "Apr",
- "5" => "May", "6" => "Jun", "7" => "Jul", "8" => "Aug",
- "9" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec"
- ];
- return $months[$m] ?? "Dec";
- }
- // 字符串截取
- function strLeft($str, $strLen) {
- if (empty($str)) {
- return "";
- }
- $length = 0;
- $result = "";
- for ($i = 0; $i < mb_strlen($str); $i++) {
- $char = mb_substr($str, $i, 1);
- $length += (mb_ord($char) > 255) ? 2 : 1;
- if ($length > $strLen) {
- return $result . "..";
- }
- $result .= $char;
- }
- return $result;
- }
- // 验证邮箱
- function isValidEmail($email) {
- $names = explode("@", $email);
- if (count($names) !== 2) {
- return false;
- }
- foreach ($names as $name) {
- if (empty($name)) {
- return false;
- }
- if (preg_match("/[^a-z0-9_.-]/", strtolower($name))) {
- return false;
- }
- if (str_starts_with($name, ".") || str_ends_with($name, ".")) {
- return false;
- }
- }
- $domainParts = explode(".", $names[1]);
- if (count($domainParts) < 2) {
- return false;
- }
- $tldLength = strlen(end($domainParts));
- if ($tldLength !== 2 && $tldLength !== 3) {
- return false;
- }
- if (str_contains($email, "..")) {
- return false;
- }
- return true;
- }
- // 站点链接替换
- function sitelink_replace($t0, $t1, $t2, $t3) {
- if (empty($t0)) {
- return "";
- }
- $t4 = $t0;
- $pattern = "/(\<a[^<>]+\>.+?\<\/a\>)|(\<img[^<>]+\>)|(\<h[1-6]+[\s]*\>.+?\<\/h[1-6]+\>)/i";
- preg_match_all($pattern, $t4, $matches);
- $myarray = [];
- if (count($matches[0]) > 0) {
- foreach ($matches[0] as $i => $match) {
- $myarray[$i] = $match;
- $t4 = str_replace($match, "[$i]", $t4, $t3);
- }
- }
- if (empty($myarray)) {
- return str_replace($t1, $t2, $t0, $t3);
- }
- $t4 = str_replace($t1, $t2, $t4, $t3);
- foreach ($myarray as $i => $value) {
- $t4 = str_replace("[$i]", $value, $t4, $t3);
- }
- return $t4;
- }
- if(!function_exists('htmlspecialcharsFix')) {
- //处理特殊字符
- function htmlspecialcharsFix($input_str)
- {
- return textUncode($input_str);
- //return $input_str;
- }
- }
- if(!function_exists('htmlspecialcharsAjaxFix')) {
- //处理特殊字符
- function htmlspecialcharsAjaxFix($input_str)
- {
- return textUncode($input_str);
- }
- }
- if(!function_exists('textDecode')) {
- function textDecode($str) {
- return textUncode($str);
- }
- }
- if(!function_exists('htmlDecode')) {
- function htmlDecode($str) {
- return htmlspecialchars_decode($str, ENT_QUOTES);
- }
- }
- if(!function_exists('formatCurrency')) {
- function formatCurrency($value) {
- return '¥' . number_format($value ?? 0, 2);
- }
- }
- // // 发送个人消息
- // $message_id = sendMessage(
- // '个人消息标题',
- // '消息内容',
- // 2, // 客户相关
- // 0, // 个人消息
- // 123, // 员工ID
- // 1 // 重要
- // );
- // // 发送部分群发消息
- // $message_id = sendMessage(
- // '部门通知',
- // '通知内容',
- // 1, // 系统消息
- // 1, // 部分群发
- // [101, 102, 103], // 员工ID数组
- // 0 // 普通优先级
- // );
- // // 发送全体公告
- // $message_id = sendMessage(
- // '系统升级通知',
- // '系统将于今晚10点维护',
- // 1, // 系统消息
- // 2, // 全体公告
- // [], // 无需指定接收者
- // 2 // 紧急
- // );
- // // 获取未读消息数
- // $unread_count = getUnreadMessageCount();
- /**
- * 发送消息函数
- *
- * @param string $title 消息标题
- * @param string $content 消息内容
- * @param int $message_type 消息类型: 1=系统消息, 2=客户相关, 3=订单相关, 4=任务提醒, 5=其他
- * @param int $target_type 接收目标类型: 0=个人, 1=部分群发, 2=全体公告
- * @param array|int $recipients 当target_type=0或1时,接收者ID数组或单个接收者ID
- * @param int $priority 优先级: 0=普通, 1=重要, 2=紧急
- * @param int|null $related_customer_id 相关客户ID (可选)
- * @param int|null $related_order_id 相关订单ID (可选)
- * @return int|false 成功返回消息ID,失败返回false
- */
- function sendMessage($title, $content, $message_type = 1, $target_type = 0, $recipients = [], $priority = 0, $related_customer_id = null, $related_order_id = null) {
- global $conn;
-
- // 验证和清理输入
- $title = mysqli_real_escape_string($conn, trim($title));
- $content = mysqli_real_escape_string($conn, trim($content));
- $message_type = intval($message_type);
- $target_type = intval($target_type);
- $priority = intval($priority);
- $related_customer_id = $related_customer_id ? intval($related_customer_id) : "NULL";
- $related_order_id = $related_order_id ? intval($related_order_id) : "NULL";
-
- // 验证必填字段
- if (empty($title) || empty($content)) {
- return false;
- }
-
- // 插入消息主表
- $sql = "INSERT INTO messages (title, content, message_type, target_type, priority, related_customer_id, related_order_id)
- VALUES ('$title', '$content', $message_type, $target_type, $priority, $related_customer_id, $related_order_id)";
-
- if (!$conn->query($sql)) {
- return false;
- }
-
- // 获取新插入消息的ID
- $message_id = $conn->insert_id;
-
- // 处理接收人
- if ($target_type == 0 || $target_type == 1) {
- // 个人消息或部分群发
- if (!is_array($recipients)) {
- $recipients = [$recipients]; // 转换为数组
- }
-
- if (empty($recipients)) {
- return false; // 接收人为空,返回失败
- }
-
- // 插入接收人记录
- $values = [];
- foreach ($recipients as $employee_id) {
- $employee_id = intval($employee_id);
- if ($employee_id > 0) {
- $values[] = "($message_id, $employee_id, 0, NULL, 0, NOW())";
- }
- }
-
- if (!empty($values)) {
- $recipientSql = "INSERT INTO message_recipients (message_id, employee_id, is_read, read_time, is_deleted, created_at)
- VALUES " . implode(",", $values);
- $conn->query($recipientSql);
- }
- } else if ($target_type == 2) {
- // 全体公告,不需要添加接收人记录
- // 在message_list.php中通过target_type=2来判断全员可见
- }
-
- // 记录操作日志
- logAction("发送消息: $title");
-
- return $message_id;
- }
- /**
- * 获取未读消息数量
- *
- * @param int $employee_id 员工ID,默认为当前登录员工
- * @return int 未读消息数量
- */
- function getUnreadMessageCount($employee_id = null) {
- global $conn;
-
- // 如果没有指定员工ID,使用当前登录员工ID
- if ($employee_id === null) {
- if (empty($_SESSION['employee_id'])) {
- return 0;
- }
- $employee_id = $_SESSION['employee_id'];
- }
-
- $employee_id = intval($employee_id);
-
- // 查询未读消息数量
- $sql = "SELECT COUNT(*) AS count
- FROM messages m
- LEFT JOIN message_recipients mr ON m.id = mr.message_id AND mr.employee_id = $employee_id
- WHERE (m.target_type = 2 OR (mr.employee_id = $employee_id))
- AND (mr.is_deleted = 0 OR mr.is_deleted IS NULL)
- AND (mr.is_read = 0 OR mr.is_read IS NULL)";
-
- $result = mysqli_query($conn, $sql);
- $row = mysqli_fetch_assoc($result);
-
- return intval($row['count']);
- }
|