diff --git a/app/home/BaseController.php b/app/base/BaseController.php similarity index 99% rename from app/home/BaseController.php rename to app/base/BaseController.php index 552d19d..a1e2514 100644 --- a/app/home/BaseController.php +++ b/app/base/BaseController.php @@ -7,7 +7,7 @@ declare (strict_types = 1); -namespace app\home; +namespace app\base; use think\App; use think\exception\HttpResponseException; @@ -53,9 +53,9 @@ abstract class BaseController */ public function __construct(App $app) { - $this->module = strtolower(app('http')->getName()); $this->app = $app; $this->request = $this->app->request; + $this->module = strtolower(app('http')->getName()); $this->controller = strtolower($this->request->controller()); $this->action = strtolower($this->request->action()); $this->uid = 0; diff --git a/app/common.php b/app/common.php index 3450bc6..9aae46f 100644 --- a/app/common.php +++ b/app/common.php @@ -102,6 +102,39 @@ function format_bytes($size, $delimiter = '') return round($size, 2) . $delimiter . $units[$i]; } +/** + * 截取字符串 + * @param $start 开始截取位置 + * @param $length 截取长度 + * @return + */ +function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) +{ + if (function_exists("mb_substr")) { + $slice = mb_substr($str, $start, $length, $charset); + } elseif (function_exists('iconv_substr')) { + $slice = iconv_substr($str, $start, $length, $charset); + if (false === $slice) { + $slice = ''; + } + } else { + $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; + $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; + $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; + $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; + preg_match_all($re[$charset], $str, $match); + $slice = join("", array_slice($match[0], $start, $length)); + } + if (utf8_strlen($str) < $length) $suffix = false; + return $suffix ? $slice . '...' : $slice; +} + +function utf8_strlen($string = null) +{ + preg_match_all("/./us", $string, $match); + return count($match[0]); +} + /** * PHP截取文字长度 * @return string @@ -319,6 +352,161 @@ function table_assign($code = 0, $msg = '请求成功', $data = [], $httpCode = throw new \think\exception\HttpResponseException($response); } +/** + * 人民币转大写 + * @param + */ +function cny($ns) +{ + static $cnums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"), + $cnyunits = array("圆", "角", "分"), + $grees = array("拾", "佰", "仟", "万", "拾", "佰", "仟", "亿"); + list($ns1, $ns2) = explode(".", $ns, 2); + $ns2 = array_filter(array($ns2[1], $ns2[0])); + $ret = array_merge($ns2, array(implode("", _cny_map_unit(str_split($ns1), $grees)), "")); + $ret = implode("", array_reverse(_cny_map_unit($ret, $cnyunits))); + return str_replace(array_keys($cnums), $cnums, $ret); +} + +function _cny_map_unit($list, $units) +{ + $ul = count($units); + $xs = array(); + foreach (array_reverse($list) as $x) { + $l = count($xs); + if ($x != "0" || !($l % 4)) { + $n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]); + } else { + $n = is_numeric($xs[0][0]) ? $x : ''; + } + array_unshift($xs, $n); + } + return $xs; +} + + +/** + * 金额展示规则,超过1万时以万为单位,低于1万时以千为单位,低于1千时以元为单位 + * @param string $money 金额 + * @return string + */ +function format_money($money) +{ + $data = '0元'; + if (($money / 10000) > 1) { + $data = is_int($money / 10000) ? ($money / 10000) . '万' : rand(($money / 10000), 2) . '万'; + } elseif (($money / 1000) > 1) { + $data = is_int($money / 1000) ? ($money / 1000) . '千' : rand(($money / 1000), 2) . '千'; + } else { + $data = $money . '元'; + } + return $data; +} + +/** + * 数组转换字符串(以逗号隔开) + * @param + * @return + */ +function arrayToString($array) +{ + if (!is_array($array)) { + $data_arr[] = $array; + } else { + $data_arr = $array; + } + $data_arr = array_filter($data_arr); //数组去空 + $data_arr = array_unique($data_arr); //数组去重 + $data_arr = array_merge($data_arr); + $string = $data_arr ? ',' . implode(',', $data_arr) . ',' : ''; + return $string ?: ''; +} + +/** + * 字符串转换数组(以逗号隔开) + * @param + * @return + */ +function stringToArray($string) +{ + if (is_array($string)) { + $data_arr = array_unique(array_filter($string)); + } else { + $data_arr = $string ? array_unique(array_filter(explode(',', $string))) : []; + } + $data_arr = $data_arr ? array_merge($data_arr) : []; + return $data_arr ?: []; +} + +/** + * 二维数组排序(选择) + * @param $select 要进行排序的select结果集 + * @param $field 排序的字段 + * @param $order 排序方式1降序2升序 + */ +function sort_select($select = array(), $field, $order = 1) +{ + $count = count($select); + if ($order == 1) { + for ($i = 0; $i < $count; $i++) { + $k = $i; + for ($j = $i; $j < $count; $j++) { + if ($select[$k][$field] < $select[$j][$field]) { + $k = $j; + } + } + $temp = $select[$i]; + $select[$i] = $select[$k]; + $select[$k] = $temp; + } + return $select; + } else { + for ($i = 0; $i < $count; $i++) { + $k = $i; + for ($j = $i; $j < $count; $j++) { + if ($select[$k][$field] > $select[$j][$field]) { + $k = $j; + } + } + $temp = $select[$i]; + $select[$i] = $select[$k]; + $select[$k] = $temp; + } + return $select; + } +} + +/** + * fullcalendar日历控件方法1 + */ +function parseDateTime($string, $timeZone=null) { + $date = new DateTime( + $string, + $timeZone ? $timeZone : new DateTimeZone('UTC') + ); + if ($timeZone) { + $date->setTimezone($timeZone); + } + return $date; +} + +/** + * fullcalendar日历控件方法2 + */ +function stripTime($datetime) { + return new DateTime($datetime->format('Y-m-d')); +} + +/** + * 根据时间戳获取星期几 + * @param $time 要转换的时间戳 + */ +function getTimeWeek($time, $i = 0) +{ + $weekarray = array("日", "一", "二", "三", "四", "五", "六"); + $oneD = 24 * 60 * 60; + return "星期" . $weekarray[date("w", $time + $oneD * $i)]; +} /** * 时间戳格式化 * @param int $time @@ -336,20 +524,312 @@ function time_format($time = NULL, $format = 'Y-m-d H:i:s') return $time != '' ? str_replace('x', $sec, date($format, intval($usec))) : ''; } -function parseDateTime($string, $timeZone=null) { - $date = new DateTime( - $string, - $timeZone ? $timeZone : new DateTimeZone('UTC') - ); - if ($timeZone) { - $date->setTimezone($timeZone); - } - return $date; +/** + * 将秒数转换为时间 (年、天、小时、分、秒) + * @param + */ +function getTimeBySec($time) +{ + if (is_numeric($time)) { + $value = array( + "years" => 0, "days" => 0, "hours" => 0, + "minutes" => 0, "seconds" => 0, + ); + if ($time >= 31556926) { + $value["years"] = floor($time / 31556926); + $time = ($time % 31556926); + $t .= $value["years"] . "年"; + } + if ($time >= 86400) { + $value["days"] = floor($time / 86400); + $time = ($time % 86400); + $t .= $value["days"] . "天"; + } + if ($time >= 3600) { + $value["hours"] = floor($time / 3600); + $time = ($time % 3600); + $t .= $value["hours"] . "小时"; + } + if ($time >= 60) { + $value["minutes"] = floor($time / 60); + $time = ($time % 60); + $t .= $value["minutes"] . "分钟"; + } + if ($time < 60) { + $value["seconds"] = floor($time); + $t .= $value["seconds"] . "秒"; + } + return $t; + } else { + return (bool)FALSE; + } } +/* + *根据年月计算有几天 + */ +function getmonthByYM($param) +{ + $month = $param['month'] ? $param['month'] : date('m', time()); + $year = $param['year'] ? $param['year'] : date('Y', time()); + if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) { + $days = '31'; + } elseif ($month == 2) { + if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) { + //判断是否是闰年 + $days = '29'; + } else { + $days = '28'; + } + } else { + $days = '30'; + } + return $days; +} -function stripTime($datetime) { - return new DateTime($datetime->format('Y-m-d')); +/** + * 根据时间戳计算当月天数 + * @param + */ +function getmonthdays($time) +{ + $month = date('m', $time); + $year = date('Y', $time); + if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) { + $days = '31'; + } elseif ($month == 2) { + if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) { + //判断是否是闰年 + $days = '29'; + } else { + $days = '28'; + } + } else { + $days = '30'; + } + return $days; +} + +/** + * 生成从开始时间到结束时间的日期数组 + * @param type,默认时间戳格式 + * @param type = 1 时,date格式 + * @param type = 2 时,获取每日开始、结束时间 + */ +function dateList($start, $end, $type = 0) +{ + if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return ''; + $i = 0; + //从开始日期到结束日期的每日时间戳数组 + $d = array(); + if ($type == 1) { + while ($start <= $end) { + $d[$i] = date('Y-m-d', $start); + $start = $start + 86400; + $i++; + } + } else { + while ($start <= $end) { + $d[$i] = $start; + $start = $start + 86400; + $i++; + } + } + if ($type == 2) { + $list = array(); + foreach ($d as $k => $v) { + $list[$k] = getDateRange($v); + } + return $list; + } else { + return $d; + } +} + +/** + * 获取指定日期开始时间与结束时间 + */ +function getDateRange($timestamp) +{ + $ret = array(); + $ret['sdate'] = strtotime(date('Y-m-d', $timestamp)); + $ret['edate'] = strtotime(date('Y-m-d', $timestamp)) + 86400; + return $ret; +} + +/** + * 生成从开始月份到结束月份的月份数组 + * @param int $start 开始时间戳 + * @param int $end 结束时间戳 + */ +function monthList($start, $end) +{ + if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return ''; + $start = date('Y-m', $start); + $end = date('Y-m', $end); + //转为时间戳 + $start = strtotime($start . '-01'); + $end = strtotime($end . '-01'); + $i = 0; + $d = array(); + while ($start <= $end) { + //这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数 + $d[$i] = $start; + $start += strtotime('+1 month', $start) - $start; + $i++; + } + return $d; +} + +/** + * 等于(时间段)数据处理 + * + * @param $data + * @return array + * @since 2021-06-11 + * @author fanqi + */ +function advancedDate($data) +{ + // 本年度 + if ($data['value'][0] == 'year') { + $arrTime = DataTime::year(); + $data['value'][0] = date('Y-m-d 00:00:00', $arrTime[0]); + $data['value'][1] = date('Y-m-d 23:59:59', $arrTime[1]); + } + + // 上一年度 + if ($data['value'][0] == 'lastYear') { + $data['value'][0] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-1 year')); + $data['value'][1] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 year')); + } + + // 下一年度 + if ($data['value'][0] == 'nextYear') { + $data['value'][0] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 year')); + $data['value'][1] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+1 year')); + } + + // 上半年 + if ($data['value'][0] == 'firstHalfYear') { + $data['value'][0] = date('Y-01-01 00:00:00'); + $data['value'][1] = date('Y-06-30 23:59:59'); + } + + // 下半年 + if ($data['value'][0] == 'nextHalfYear') { + $data['value'][0] = date('Y-07-01 00:00:00'); + $data['value'][1] = date('Y-12-31 23:59:59'); + } + + // 本季度 + if ($data['value'][0] == 'quarter') { + $season = ceil((date('n')) / 3); + $data['value'][0] = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'))); + $data['value'][1] = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'))); + } + + // 上一季度 + if ($data['value'][0] == 'lastQuarter') { + $season = ceil((date('n')) / 3) - 1; + $data['value'][0] = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'))); + $data['value'][1] = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'))); + } + + // 下一季度 + if ($data['value'][0] == 'nextQuarter') { + $season = ceil((date('n')) / 3); + $data['value'][0] = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 + 1, 1, date('Y'))); + $data['value'][1] = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3 + 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'))); + } + + // 本月 + if ($data['value'][0] == 'month') { + $data['value'][0] = date('Y-m-01 00:00:00'); + $data['value'][1] = date('Y-m-31 23:59:59'); + } + + // 上月 + if ($data['value'][0] == 'lastMonth') { + $data['value'][0] = date('Y-m-01 00:00:00', strtotime(date('Y-m-d') . '-1 month')); + $data['value'][1] = date('Y-m-31 23:59:59', strtotime(date('Y-m-d') . '-1 month')); + } + + // 下月 + if ($data['value'][0] == 'nextMonth') { + $data['value'][0] = date('Y-m-01 00:00:00', strtotime(date('Y-m-d') . '+1 month')); + $data['value'][1] = date('Y-m-31 23:59:59', strtotime(date('Y-m-d') . '+1 month')); + } + + // 本周 + if ($data['value'][0] == 'week') { + $data['value'][0] = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'))); + $data['value'][1] = date('Y-m-d 23:59:59', mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'))); + } + + // 上周 + if ($data['value'][0] == 'lastWeek') { + $date = date("Y-m-d"); + $w = date("w", strtotime($date)); + $d = $w ? $w - 1 : 6; + $start = date("Y-m-d", strtotime($date . " - " . $d . " days")); + $data['value'][0] = date('Y-m-d', strtotime($start . " - 7 days")); + $data['value'][1] = date('Y-m-d', strtotime($start . " - 1 days")); + } + + // 下周 + if ($data['value'][0] == 'nextWeek') { + $date = date("Y-m-d"); + $w = date("w", strtotime($date)); + $d = $w ? $w - 1 : 6; + $start = date("Y-m-d", strtotime($date . " - " . $d . " days")); + $data['value'][0] = date('Y-m-d', strtotime($start . " + 7 days")); + $data['value'][1] = date('Y-m-d', strtotime($start . " + 13 days")); + } + + // 今天 + if ($data['value'][0] == 'today') { + $data['value'][0] = date('Y-m-d 00:00:00'); + $data['value'][1] = date('Y-m-d 23:59:59'); + } + + // 昨天 + if ($data['value'][0] == 'yesterday') { + $data['value'][0] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-1 day')); + $data['value'][1] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day')); + } + + // 明天 + if ($data['value'][0] == 'tomorrow') { + $data['value'][0] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day')); + $data['value'][1] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+1 day')); + } + + // 过去7天 + if ($data['value'][0] == 'previous7day') { + $data['value'][0] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-7 day')); + $data['value'][1] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day')); + } + + // 过去30天 + if ($data['value'][0] == 'previous30day') { + $data['value'][0] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-30 day')); + $data['value'][1] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day')); + } + + // 未来7天 + if ($data['value'][0] == 'future7day') { + $data['value'][0] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day')); + $data['value'][1] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+7 day')); + } + + // 未来30天 + if ($data['value'][0] == 'future30day') { + $data['value'][0] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day')); + $data['value'][1] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+30 day')); + } + + return $data; } /** @@ -460,15 +940,218 @@ function is_email($user_email) } /** - * 验证输入的手机号码是否合法 - * @param $mobile_phone 手机号 - * @return bool + * 获取客户浏览器类型 */ -function is_mobile_phone($mobile_phone) +function getBrowser() { - $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/"; - if (preg_match($chars, $mobile_phone)) { - return true; + $Browser = $_SERVER['HTTP_USER_AGENT']; + if (preg_match('/MSIE/i', $Browser)) { + $Browser = 'MSIE'; + } elseif (preg_match('/Firefox/i', $Browser)) { + $Browser = 'Firefox'; + } elseif (preg_match('/Chrome/i', $Browser)) { + $Browser = 'Chrome'; + } elseif (preg_match('/Safari/i', $Browser)) { + $Browser = 'Safari'; + } elseif (preg_match('/Opera/i', $Browser)) { + $Browser = 'Opera'; + } else { + $Browser = 'Other'; } - return false; + return $Browser; +} + +/** + * 获取客户端系统 + */ +function getOS() +{ + $agent = $_SERVER['HTTP_USER_AGENT']; + if (preg_match('/win/i', $agent)) { + if (preg_match('/nt 6.1/i', $agent)) { + $OS = 'Windows 7'; + } else if (preg_match('/nt 6.2/i', $agent)) { + $OS = 'Windows 8'; + } else if (preg_match('/nt 10.0/i', $agent)) { + $OS = 'Windows 10'; + } else { + $OS = 'Windows'; + } + } elseif (preg_match('/mac/i', $agent)) { + $OS = 'MAC'; + } elseif (preg_match('/linux/i', $agent)) { + $OS = 'Linux'; + } elseif (preg_match('/unix/i', $agent)) { + $OS = 'Unix'; + } elseif (preg_match('/bsd/i', $agent)) { + $OS = 'BSD'; + } else { + $OS = 'Other'; + } + return $OS; +} + +/** + * 根据IP获取地址 + */ +function getAddress($ip) +{ + $res = file_get_contents("http://ip.360.cn/IPQuery/ipquery?ip=" . $ip); + $res = json_decode($res, 1); + if ($res && $res['errno'] == 0) { + return explode("\t", $res['data'])[0]; + } else { + return ''; + } +} + +/** + * 下载服务器文件 + * + * @param string $file 文件路径 + * @param string $name 下载名称 + * @param boolean $del 下载后删除 + * @return void + */ +function download($file, $name = '', $del = false) +{ + if (!file_exists($file)) { + return resultArray([ + 'error' => '文件不存在', + ]); + } + // 仅允许下载 public 目录下文件 + $res = strpos(realpath($file), realpath('./public')); + if ($res !== 0) { + return resultArray([ + 'error' => '文件路径错误', + ]); + } + + $fp = fopen($file, 'r'); + $size = filesize($file); + + //下载文件需要的头 + header("Content-type: application/octet-stream"); + header("Accept-Ranges: bytes"); + header('ResponseType: blob'); + header("Accept-Length: $size"); + $file_name = $name != '' ? $name : pathinfo($file, PATHINFO_BASENAME); + // urlencode 处理中文乱码 + header("Content-Disposition:attachment; filename=" . urlencode($file_name)); + + // 导出数据时 csv office Excel 需要添加bom头 + if (pathinfo($file, PATHINFO_EXTENSION) == 'csv') { + echo "\xEF\xBB\xBF"; // UTF-8 BOM + } + + $fileCount = 0; + $fileUnit = 1024; + while (!feof($fp) && $size - $fileCount > 0) { + $fileContent = fread($fp, $fileUnit); + echo $fileContent; + $fileCount += $fileUnit; + } + fclose($fp); + + // 删除 + if ($del) @unlink($file); + die(); +} + +/** + * 导出数据为excel表格 + * @param $data 一个二维数组,结构如同从数据库查出来的数组 + * @param $title excel的第一行标题,一个数组,如果为空则没有标题 + * @param $filename 下载的文件名 + * @param exportexcel($arr,array('id','账户','密码','昵称'),'文件名!'); + */ +function exportexcel($data = array(), $title = array(), $filename = 'report') +{ + header("Content-type:application/octet-stream"); + header("Accept-Ranges:bytes"); + header("Content-type:application/vnd.ms-excel"); + header("Content-Disposition:attachment;filename=" . $filename . ".xls"); + header("Pragma: no-cache"); + header("Expires: 0"); + //导出xls 开始 + if (!empty($title)) { + foreach ($title as $k => $v) { + $title[$k] = iconv("UTF-8", "GB2312", $v); + } + $title = implode("\t", $title); + echo "$title\n"; + } + if (!empty($data)) { + foreach ($data as $key => $val) { + foreach ($val as $ck => $cv) { + $data[$key][$ck] = iconv("UTF-8", "GB2312", $cv); + } + $data[$key] = implode("\t", $data[$key]); + } + echo implode("\n", $data); + } +} + + +//根据数据库查询出来数组获取某个字段拼接字符串 +function getFieldArray($array = array(), $field = '') +{ + if (is_array($array) && $field) { + $ary = array(); + foreach ($array as $value) { + $ary[] = $value[$field]; + } + $str = implode(',', $ary); + return $str; + } else { + return false; + } +} + +/** + * curl 模拟GET请求 + * @author lee + ***/ +function curl_get($url) +{ + //初始化 + $ch = curl_init(); + //设置抓取的url + curl_setopt($ch, CURLOPT_URL, $url); + //设置获取的信息以文件流的形式返回,而不是直接输出。 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // https请求 不验证hosts + //执行命令 + $output = curl_exec($ch); + curl_close($ch); //释放curl句柄 + return $output; +} + +/** + * 模拟post进行url请求 + * @param string $url + * @param string $param + */ +function curl_post($url = '', $post = array()) +{ + $curl = curl_init(); // 启动一个CURL会话 + curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在 + curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 + curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer + curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 + curl_setopt($curl, CURLOPT_POSTFIELDS, $post); // Post提交的数据包 + curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 + curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 + $res = curl_exec($curl); // 执行操作 + if (curl_errno($curl)) { + echo 'Errno' . curl_error($curl);//捕抓异常 + } + curl_close($curl); // 关闭CURL会话 + return $res; // 返回数据,json格式 } diff --git a/app/home/common.php b/app/home/common.php index b194eeb..86272ba 100644 --- a/app/home/common.php +++ b/app/home/common.php @@ -55,9 +55,14 @@ function get_system_config($name, $key = '') //获取指定管理员的信息 function get_admin($id) { - $admin = Db::name('Admin')->where(['id' => $id])->find(); - $admin['department'] = Db::name('Department')->where(['id' => $admin['did']])->value('title'); - $admin['position'] = Db::name('Position')->where(['id' => $admin['position_id']])->value('title'); + $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; } @@ -113,7 +118,7 @@ function get_admin_group_info($id) return $group; } -//菜单父子关系排序,用于后台菜单 +//菜单父子关系排序,用于菜单 function get_admin_menus() { $admin = get_login_admin(); @@ -134,6 +139,21 @@ function get_admin_menus() return $list; } + +/** + * 节点权限判断 + * @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() { @@ -141,7 +161,7 @@ function get_department() return $department; } -//获取某部门的子部门id +//获取某部门的子部门id.$is_self时候包含自己 function get_department_son($did = 0, $is_self = 1) { $department = get_department(); @@ -247,18 +267,52 @@ function get_file($id) } /** - * 节点权限判断 - * @return bool + * 发送站内信 + * @param $user_id 接收人user_id + * @param $data 操作内容 + * @param $sysMessage 1为系统消息 + * @param $template 消息模板 + * @return */ -function check_auth($rule, $uid) +function sendMessage($user_id, $template, $data=[]) { - $auth_list = Cache::get('RulesSrc' . $uid); - if (!in_array($rule, $auth_list)) { - return false; + $content = get_config('message.template')[$template]['template']; + foreach ($data as $key => $val) { + $content = str_replace('{' . $key . '}', $val, $content); + } + if(isSet($data['from_uid'])){ + $content = str_replace('{from_user}', get_admin($data['from_uid'])['name'], $content); + } + $content = str_replace('{date}', date('Y-m-d'), $content); + + if (!$user_id) return false; + if (!$content) return false; + if (!is_array($user_id)) { + $users[] = $user_id; } else { - return true; + $users = $user_id; } + $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' => $data['title'], + 'content' => $content, + '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; } + /** * 员工操作日志 * @param string $type 操作类型 login add edit view delete @@ -268,56 +322,10 @@ function check_auth($rule, $uid) function add_log($type, $param_id = '', $param = []) { $action = '未知操作'; - switch ($type) { - case 'login': - $action = '登录'; - break; - case 'upload': - $action = '上传'; - break; - case 'add': - $action = '新增'; - break; - case 'edit': - $action = '编辑'; - break; - case 'view': - $action = '查看'; - break; - case 'save': - $action = '保存'; - break; - case 'send': - $action = '发送'; - break; - case 'delete': - $action = '删除'; - break; - case 'check': - $action = '审核'; - break; - case 'leave': - $action = '离职'; - break; - case 'disable': - $action = '禁用'; - break; - case 'recovery': - $action = '恢复'; - break; - case 'apply': - $action = '申请'; - break; - case 'open': - $action = '开具'; - break; - case 'tovoid': - $action = '作废'; - break; - case 'reset': - $action = '重新设置'; - break; - } + $type_action = get_config('log.type_action'); + if($type_action[$type]){ + $action = $type_action[$type]; + } if ($type == 'login') { $login_admin = Db::name('Admin')->where(array('id' => $param_id))->find(); } else { @@ -331,9 +339,9 @@ function add_log($type, $param_id = '', $param = []) $data['action'] = $action; $data['param_id'] = $param_id; $data['param'] = json_encode($param); - $data['module'] = \think\facade\App::initialize()->http->getName(); + $data['module'] = strtolower(app('http')->getName()); $data['controller'] = strtolower(app('request')->controller()); - $data['function'] = app('request')->action(); + $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){ @@ -362,9 +370,7 @@ function add_log($type, $param_id = '', $param = []) function send_email($to, $subject = '', $content = '') { $mail = new PHPMailer\PHPMailer\PHPMailer(); - $email_config = Db::name('config') - ->where('name', 'email') - ->find(); + $email_config = Db::name('config')->where('name', 'email')->find(); $config = unserialize($email_config['content']); $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 diff --git a/app/home/controller/Admin.php b/app/home/controller/Admin.php index 58f0a61..b570d0a 100644 --- a/app/home/controller/Admin.php +++ b/app/home/controller/Admin.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\Admin as AdminList; use app\home\validate\AdminCheck; use avatars\MDAvatars; diff --git a/app/home/controller/Analysis.php b/app/home/controller/Analysis.php index 2b817a0..4e344cc 100644 --- a/app/home/controller/Analysis.php +++ b/app/home/controller/Analysis.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use think\facade\Db; use think\facade\View; diff --git a/app/home/controller/Api.php b/app/home/controller/Api.php index 9448327..5b77f2c 100644 --- a/app/home/controller/Api.php +++ b/app/home/controller/Api.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\AdminLog; use app\home\validate\AdminCheck; use think\exception\ValidateException; diff --git a/app/home/controller/Article.php b/app/home/controller/Article.php index d276eb9..305fcf5 100644 --- a/app/home/controller/Article.php +++ b/app/home/controller/Article.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\Article as ArticleList; use app\home\model\ArticleCate; use app\home\validate\ArticleCateCheck; diff --git a/app/home/controller/Check.php b/app/home/controller/Check.php index 99951a1..962166d 100644 --- a/app/home/controller/Check.php +++ b/app/home/controller/Check.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use think\facade\Db; use think\facade\View; diff --git a/app/home/controller/Conf.php b/app/home/controller/Conf.php index 9fd0226..7317e09 100644 --- a/app/home/controller/Conf.php +++ b/app/home/controller/Conf.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\validate\ConfCheck; use think\exception\ValidateException; use think\facade\Db; diff --git a/app/home/controller/Database.php b/app/home/controller/Database.php index 1cdf22a..74b908a 100644 --- a/app/home/controller/Database.php +++ b/app/home/controller/Database.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use backup\Backup; use think\facade\Db; use think\facade\View; diff --git a/app/home/controller/Department.php b/app/home/controller/Department.php index c68f625..4b6a020 100644 --- a/app/home/controller/Department.php +++ b/app/home/controller/Department.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\validate\DepartmentCheck; use think\exception\ValidateException; use think\facade\Db; diff --git a/app/home/controller/Expense.php b/app/home/controller/Expense.php index e2cd778..6f45ade 100644 --- a/app/home/controller/Expense.php +++ b/app/home/controller/Expense.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\Expense as ExpenseList; use app\home\model\ExpenseCate; use app\home\validate\ExpenseCateCheck; diff --git a/app/home/controller/Income.php b/app/home/controller/Income.php index d62d95c..cd60f7d 100644 --- a/app/home/controller/Income.php +++ b/app/home/controller/Income.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\Invoice as InvoiceList; use app\home\validate\InvoiceCheck; use think\exception\ValidateException; diff --git a/app/home/controller/Index.php b/app/home/controller/Index.php index eb192c8..214f246 100644 --- a/app/home/controller/Index.php +++ b/app/home/controller/Index.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use think\facade\Db; use think\facade\View; @@ -53,14 +53,14 @@ class Index extends BaseController } //未读消息统计 - $mail_map[] = ['to_uid','=',$admin_id]; - $mail_map[] = ['is_read','=',1]; - $mail_map[] = ['status','=',1]; - $mail_count = Db::name('Mail')->where($mail_map)->count(); - $statistics['mail_html'] = ' 您有'.$mail_count.'条未读消息'; - $statistics['num_mail'] = $mail_count; - if($mail_count==0){ - $statistics['mail_html'] = ''; + $msg_map[] = ['to_uid','=',$admin_id]; + $msg_map[] = ['read_time','=',0]; + $msg_map[] = ['status','=',1]; + $msg_count = Db::name('Message')->where($msg_map)->count(); + $statistics['msg_html'] = ' 您有'.$msg_count.'条未读消息'; + $statistics['msg_num'] = $msg_count; + if($msg_count==0){ + $statistics['msg_html'] = ''; } foreach ($statistics as $key => $value) { diff --git a/app/home/controller/Invoice.php b/app/home/controller/Invoice.php index 5f99407..2b08195 100644 --- a/app/home/controller/Invoice.php +++ b/app/home/controller/Invoice.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\InvoiceSubject; use app\home\model\Invoice as InvoiceList; use app\home\validate\InvoiceSubjectCheck; diff --git a/app/home/controller/Keywords.php b/app/home/controller/Keywords.php index a8dbac3..71683d9 100644 --- a/app/home/controller/Keywords.php +++ b/app/home/controller/Keywords.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\validate\KeywordsCheck; use think\exception\ValidateException; use think\facade\Db; diff --git a/app/home/controller/Menu.php b/app/home/controller/Menu.php index a8b1e08..f656cfd 100644 --- a/app/home/controller/Menu.php +++ b/app/home/controller/Menu.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\validate\MenuCheck; use think\exception\ValidateException; use think\facade\Db; diff --git a/app/home/controller/Mail.php b/app/home/controller/Message.php similarity index 70% rename from app/home/controller/Mail.php rename to app/home/controller/Message.php index 4dc41d2..9db22c4 100644 --- a/app/home/controller/Mail.php +++ b/app/home/controller/Message.php @@ -9,12 +9,12 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; -use app\home\model\Mail as MailList; +use app\base\BaseController; +use app\home\model\Message as MessageList; use think\facade\Db; use think\facade\View; -class Mail extends BaseController +class Message extends BaseController { //收件箱 public function inbox() @@ -24,13 +24,21 @@ class Mail extends BaseController $param['status'] = 1; $map = []; if (!empty($param['keywords'])) { - $map[] = ['name', 'like', '%' . $param['keywords'] . '%']; + $map[] = ['title', 'like', '%' . $param['keywords'] . '%']; } - if (!empty($param['is_read'])) { - $map[] = ['is_read', '=', $param['is_read']]; + if (!empty($param['read'])) { + if($param['read']==0){ + $map[] = ['read_time', '=', 0]; + }else{ + $map[] = ['read_time', '>', 0]; + } } - if (!empty($param['mail_type'])) { - $map[] = ['mail_type', '=', $param['mail_type']]; + if (!empty($param['template'])) { + if($param['template']==0){ + $map[] = ['template', '=', 0]; + }else{ + $map[] = ['template', '>', 0]; + } } $map[] = ['to_uid', '=', $this->uid]; $map[] = ['status', '=', $param['status']]; @@ -40,7 +48,7 @@ class Mail extends BaseController if ($start_time > 0 && $end_time > 0) { $map[] = ['send_time', 'between', "$start_time,$end_time"]; } - $list = $this->getList($map, $param); + $list = (new MessageList())->getList($map, $param, $this->uid); return table_assign(0, '', $list); } else { return view(); @@ -54,7 +62,7 @@ class Mail extends BaseController $param['status'] = 1; $map = []; if (!empty($param['keywords'])) { - $map[] = ['name', 'like', '%' . $param['keywords'] . '%']; + $map[] = ['title', 'like', '%' . $param['keywords'] . '%']; } $map[] = ['from_uid', '=', $this->uid]; $map[] = ['to_uid', '=', 0]; @@ -66,7 +74,7 @@ class Mail extends BaseController if ($start_time > 0 && $end_time > 0) { $map[] = ['send_time', 'between', "$start_time,$end_time"]; } - $list = $this->getList($map, $param); + $list = (new MessageList())->getList($map, $param, $this->uid); return table_assign(0, '', $list); } else { return view(); @@ -81,7 +89,7 @@ class Mail extends BaseController $param['status'] = 2; $map = []; if (!empty($param['keywords'])) { - $map[] = ['name', 'like', '%' . $param['keywords'] . '%']; + $map[] = ['title', 'like', '%' . $param['keywords'] . '%']; } $map[] = ['from_uid', '=', $this->uid]; $map[] = ['status', '=', 1]; @@ -92,7 +100,7 @@ class Mail extends BaseController if ($start_time > 0 && $end_time > 0) { $map[] = ['send_time', 'between', "$start_time,$end_time"]; } - $list = $this->getList($map, $param); + $list = (new MessageList())->getList($map, $param, $this->uid); return table_assign(0, '', $list); } else { return view(); @@ -107,7 +115,7 @@ class Mail extends BaseController $param['status'] = 0; $map = []; if (!empty($param['keywords'])) { - $map[] = ['name', 'like', '%' . $param['keywords'] . '%']; + $map[] = ['title', 'like', '%' . $param['keywords'] . '%']; } $map[] = ['status', '=', $param['status']]; //按时间检索 @@ -116,76 +124,39 @@ class Mail extends BaseController if ($start_time > 0 && $end_time > 0) { $map[] = ['send_time', 'between', "$start_time,$end_time"]; } - $list = $this->getList($map, $param); + $list = (new MessageList())->getList($map, $param, $this->uid); return table_assign(0, '', $list); } else { return view(); } } - //获取消息列表 - public function getList($map = [], $param = []) - { - $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit']; - //垃圾箱列表特殊处理 - if ($param['status'] == 0) { - $where = [['from_uid', '=', $this->uid], ['to_uid', '=', $this->uid]]; - $mail = MailList::where($map) - ->where(function ($query) use ($where) {$query->whereOr($where);}) - ->order('create_time desc') - ->paginate($rows, false, ['query' => $param]) - ->each(function ($item, $key) { - $item->send_time = empty($item->send_time) ? '-' : date('Y-m-d H:i:s', $item->send_time); - $item->from_name = Db::name('Admin')->where(['id' => $item->from_uid])->value('nickname'); - $item->to_name = Db::name('Admin')->where(['id' => $item->to_uid])->value('nickname'); - $item->type_title = MailList::$Type[$item->type]; - $item->mail_type_title = MailList::$MailType[$item->mail_type]; - $item->delete_source_title = MailList::$Source[$item->delete_source]; - $item->files = Db::name('MailFileInterfix')->where(['mid' => $item->id, 'status' => 1])->count(); - }); - return $mail; - } else { - $mail = MailList::where($map) - ->order('create_time desc') - ->paginate($rows, false, ['query' => $param]) - ->each(function ($item, $key) { - $item->send_time = empty($item->send_time) ? '-' : date('Y-m-d H:i:s', $item->send_time); - $item->from_name = Db::name('Admin')->where(['id' => $item->from_uid])->value('nickname'); - $item->to_name = Db::name('Admin')->where(['id' => $item->to_uid])->value('nickname'); - $item->type_title = MailList::$Type[$item->type]; - $item->mail_type_title = MailList::$MailType[$item->mail_type]; - $item->files = Db::name('MailFileInterfix')->where(['mid' => $item->id, 'status' => 1])->count(); - }); - return $mail; - } - } - //新增信息 public function add() { $id = empty(get_params('id')) ? 0 : get_params('id'); $fid = 0; if ($id > 0) { - $detail = Db::name('Mail')->where(['id' => $id, 'from_uid' => $this->uid])->find(); + $detail = Db::name('Message')->where(['id' => $id, 'from_uid' => $this->uid])->find(); if (empty($detail)) { $this->error('该信息不存在'); } $fid = $detail['fid']; $person_name = []; - if ($detail['type'] == 0) { //人员 + if ($detail['type'] == 1) { //人员 $users = Db::name('Admin')->where('status', 1)->where('id', 'in', $detail['type_user'])->select()->toArray(); $person_name = array_column($users, 'name'); - } elseif ($detail['type'] == 1) { //部门 + } elseif ($detail['type'] == 2) { //部门 $departments = Db::name('Department')->where('id', 'in', $detail['type_user'])->select()->toArray(); $person_name = array_column($departments, 'title'); - } elseif ($detail['type'] == 2) { //角色 + } elseif ($detail['type'] == 3) { //角色 $group_uid = Db::name('PositionGroup')->where('group_id', 'in', $detail['type_user'])->select()->toArray(); $pids = array_column($group_uid, 'pid'); $positions = Db::name('Position')->where('id', 'in', $pids)->select()->toArray(); $person_name = array_column($positions, 'title'); } $detail['person_name'] = implode(",", $person_name); - $file_array = Db::name('MailFileInterfix') + $file_array = Db::name('MessageFileInterfix') ->field('mf.id,mf.mid,mf.file_id,f.name,f.filesize,f.filepath') ->alias('mf') ->join('file f', 'mf.file_id = f.id', 'LEFT') @@ -197,8 +168,8 @@ class Mail extends BaseController //引用消息的附件 if($detail['fid']>0){ - $detail['from_content'] = Db::name('Mail')->where(['id' => $detail['fid']])->value('content'); - $from_file_array = Db::name('MailFileInterfix') + $detail['from_content'] = Db::name('Message')->where(['id' => $detail['fid']])->value('content'); + $from_file_array = Db::name('MessageFileInterfix') ->field('mf.id,mf.mid,mf.file_id,f.name,f.filesize,f.filepath') ->alias('mf') ->join('file f', 'mf.file_id = f.id', 'LEFT') @@ -207,7 +178,6 @@ class Mail extends BaseController ->select()->toArray(); $detail['from_file_array'] = $from_file_array; } - View::assign('file_array', $file_array); View::assign('detail', $detail); } @@ -221,7 +191,7 @@ class Mail extends BaseController { $id = empty(get_params('id')) ? 0 : get_params('id'); $type = empty(get_params('type')) ? 0 : get_params('type'); - $detail = Db::name('Mail')->where(['id' => $id, 'mail_type' => 2])->find(); + $detail = Db::name('Message')->where(['id' => $id, 'template' => 0])->find(); if (empty($detail)) { $this->error('该信息不存在'); } @@ -230,7 +200,7 @@ class Mail extends BaseController } $sender = get_admin($detail['from_uid']); $detail['person_name'] = $sender['name']; - $file_array = Db::name('MailFileInterfix') + $file_array = Db::name('MessageFileInterfix') ->field('mf.id,mf.mid,mf.file_id,f.name,f.filesize,f.filepath') ->alias('mf') ->join('file f', 'mf.file_id = f.id', 'LEFT') @@ -251,14 +221,14 @@ class Mail extends BaseController { $param = get_params(); $id = $param['id']; - $detail = Db::name('Mail')->where(['id' => $id])->find(); + $detail = Db::name('Message')->where(['id' => $id])->find(); if (empty($detail)) { $this->error('该信息不存在'); } if ($detail['to_uid'] != $this->uid && $detail['from_uid'] != $this->uid) { $this->error('该信息不存在'); } - Db::name('Mail')->where(['id' => $id])->update(['is_read' => 2]); + Db::name('Message')->where(['id' => $id])->update(['read_time' => time()]); if($detail['from_uid']==0){ $detail['person_name'] = '系统管理员'; } @@ -268,8 +238,8 @@ class Mail extends BaseController } //引用消息的附件 if($detail['fid']>0){ - $detail['from_content'] = Db::name('Mail')->where(['id' => $detail['fid']])->value('content'); - $from_file_array = Db::name('MailFileInterfix') + $detail['from_content'] = Db::name('Message')->where(['id' => $detail['fid']])->value('content'); + $from_file_array = Db::name('MessageFileInterfix') ->field('mf.id,mf.mid,mf.file_id,f.name,f.filesize,f.filepath') ->alias('mf') ->join('file f', 'mf.file_id = f.id', 'LEFT') @@ -280,7 +250,7 @@ class Mail extends BaseController } //当前消息的附件 - $file_array = Db::name('MailFileInterfix') + $file_array = Db::name('MessageFileInterfix') ->field('mf.id,mf.mid,mf.file_id,f.name,f.filesize,f.filepath') ->alias('mf') ->join('file f', 'mf.file_id = f.id', 'LEFT') @@ -294,14 +264,14 @@ class Mail extends BaseController //已读回执 $read_user_names = []; if($detail['from_uid'] == $this->uid){ - $mails= Db::name('Mail')->where(['pid' => $id])->select()->toArray(); - $read_mails= Db::name('Mail')->where(['pid' => $id,'is_read' => 2])->select()->toArray(); + $mails= Db::name('Message')->where(['pid' => $id])->select()->toArray(); + $read_mails= Db::name('Message')->where([['pid','=',$id],['read_time','>',2]])->select()->toArray(); $read_user_ids = array_column($read_mails, 'to_uid'); $read_users = Db::name('Admin')->where('status', 1)->where('id', 'in', $read_user_ids)->select()->toArray(); $read_user_names = array_column($read_users, 'name'); } else{ - $mails= Db::name('Mail')->where(['pid' => $detail['pid']])->select()->toArray(); + $mails= Db::name('Message')->where(['pid' => $detail['pid']])->select()->toArray(); } $user_ids = array_column($mails, 'to_uid'); $users = Db::name('Admin')->where('status', 1)->where('id', 'in', $user_ids)->select()->toArray(); @@ -320,19 +290,19 @@ class Mail extends BaseController $id = empty($param['id']) ? 0 : $param['id']; $fid = empty($param['fid']) ? 0 : $param['fid']; //接受人类型判断 - if ($param['type'] == 0) { + if ($param['type'] == 1) { if (!$param['uids']) { return to_assign(1, '人员不能为空'); } else { $type_user = $param['uids']; } - } elseif ($param['type'] == 1) { + } elseif ($param['type'] == 2) { if (!$param['dids']) { return to_assign(1, '部门不能为空'); } else { $type_user = $param['dids']; } - } elseif ($param['type'] == 2) { + } elseif ($param['type'] == 3) { if (!$param['pids']) { return to_assign(1, '岗位不能为空'); } else { @@ -343,33 +313,32 @@ class Mail extends BaseController } //基础信息数据 $admin_id = $this->uid; - $did = get_admin($admin_id)['did']; $basedata = []; $basedata['from_uid'] = $admin_id; - $basedata['admin_id'] = $admin_id; - $basedata['did'] = $did; $basedata['fid'] = $fid; - $basedata['mail_type'] = 2;//同事信息类型 $basedata['is_draft'] = 2;//默认是草稿信息 - $basedata['name'] = $param['name']; + $basedata['title'] = $param['title']; $basedata['type'] = $param['type']; $basedata['type_user'] = $type_user; $basedata['content'] = $param['content']; + $basedata['controller_name'] = $this->controller; + $basedata['module_name'] = $this->module; + $basedata['action_name'] = $this->action; if ($id > 0) { //编辑信息的情况 $basedata['update_time'] = time(); $basedata['id'] = $id; - $res = Db::name('Mail')->strict(false)->field(true)->update($basedata); + $res = Db::name('Message')->strict(false)->field(true)->update($basedata); } else { //新增信息的情况 $basedata['create_time'] = time(); - $res = Db::name('Mail')->strict(false)->field(true)->insertGetId($basedata); + $res = Db::name('Message')->strict(false)->field(true)->insertGetId($basedata); } if ($res !== false) { //信息附件处理 if ($id > 0) { $mid = $id; - Db::name('MailFileInterfix')->where('mid', $mid)->delete(); + Db::name('MessageFileInterfix')->where('mid', $mid)->delete(); } else { $mid = $res; } @@ -389,7 +358,7 @@ class Mail extends BaseController ); } if ($file_data) { - Db::name('MailFileInterfix')->strict(false)->field(true)->insertAll($file_data); + Db::name('MessageFileInterfix')->strict(false)->field(true)->insertAll($file_data); } } add_log('save',$mid); @@ -404,20 +373,20 @@ class Mail extends BaseController { $param = get_params(); //查询要发的消息 - $msg = Db::name('Mail')->where(['id' => $param['id']])->find(); + $msg = Db::name('Message')->where(['id' => $param['id']])->find(); $users = []; if ($msg) { - $admin_id = $msg['admin_id']; + $admin_id = $msg['from_uid']; //查询全部收件人 - if ($msg['type'] == 0) { //人员 + if ($msg['type'] == 1) { //人员 $users = Db::name('Admin')->where('status', 1)->where('id', 'in', $msg['type_user'])->select()->toArray(); - } elseif ($msg['type'] == 1) { //部门 + } elseif ($msg['type'] == 2) { //部门 $users = Db::name('Admin')->where('status', 1)->where('did', 'in', $msg['type_user'])->select()->toArray(); - } elseif ($msg['type'] == 2) { //角色 + } elseif ($msg['type'] == 3) { //角色 $group_uid = Db::name('PositionGroup')->where('group_id', 'in', $msg['type_user'])->select()->toArray(); $pids = array_column($group_uid, 'pid'); $users = Db::name('Admin')->where('status', 1)->where('position_id', 'in', $pids)->select()->toArray(); - } elseif ($msg['type'] == 3) { //全部 + } elseif ($msg['type'] == 4) { //全部 $users = Db::name('Admin')->where('status', 1)->select()->toArray(); } //组合要发的消息 @@ -427,27 +396,27 @@ class Mail extends BaseController continue; } $send_data[] = array( - 'pid' => $msg['id'],//来源发件关联id - 'fid' => $msg['fid'],//转发或回复消息关联id - 'name' => $msg['name'], - 'type' => $msg['type'], - 'type_user' => $msg['type_user'], - 'from_uid' => $admin_id,//发送人 - 'did' => $value['did'], - 'to_uid' => $value['id'], - 'mail_type' => 2,//同事信息类型 - 'content' => $msg['content'], - 'send_time' => time(), - 'admin_id' => $admin_id, - 'create_time' => time() + 'pid' => $param['id'],//来源发件箱关联id + 'to_uid' => $value['id'],//接收人 + 'fid' => $msg['fid'],//转发或回复消息关联id + 'title' => $msg['title'], + 'content' => $msg['content'], + 'type' => $msg['type'],//接收人类型 + 'type_user' => $msg['type_user'],//接收人数据 + 'from_uid' => $this->uid,//发送人 + 'controller_name' => $this->controller, + 'module_name' => $this->module, + 'action_name' => $this->action, + 'send_time' => time(), + 'create_time' => time() ); } - $res = Db::name('Mail')->strict(false)->field(true)->insertAll($send_data); + $res = Db::name('Message')->strict(false)->field(true)->insertAll($send_data); if ($res!==false) { //查询原来的附件,并插入 - $file_array = Db::name('MailFileInterfix')->where('mid', $msg['id'])->select()->toArray(); + $file_array = Db::name('MessageFileInterfix')->where('mid', $msg['id'])->select()->toArray(); if ($file_array) { - $mids = Db::name('Mail')->where('pid', $msg['id'])->select()->toArray(); + $mids = Db::name('Message')->where('pid', $msg['id'])->select()->toArray(); foreach ($mids as $k => $v) { $file_data = array(); foreach ($file_array as $key => $value) { @@ -462,12 +431,12 @@ class Mail extends BaseController ); } if ($file_data) { - Db::name('MailFileInterfix')->strict(false)->field(true)->insertAll($file_data); + Db::name('MessageFileInterfix')->strict(false)->field(true)->insertAll($file_data); } } } //草稿消息变成已发消息 - Db::name('Mail')->where(['id' => $msg['id']])->update(['is_draft' => '1', 'send_time' => time(), 'update_time' => time()]); + Db::name('Message')->where(['id' => $msg['id']])->update(['is_draft' => '1', 'send_time' => time(), 'update_time' => time()]); add_log('send',$msg['id']); return to_assign(0, '发送成功'); } else { @@ -490,9 +459,8 @@ class Mail extends BaseController foreach ($idArray as $key => $val) { if ($type==1) { //设置信息为已读 $list[] = [ - 'is_read' => 2, + 'read_time' => time(), 'id' => $val, - 'update_time' => time(), ]; } else if ($type==2) { //设置信息进入垃圾箱 @@ -520,7 +488,7 @@ class Mail extends BaseController } foreach ($list as $key => $v) { - if (Db::name('Mail')->update($v) !== false) { + if (Db::name('Message')->update($v) !== false) { if ($type = 1) { add_log('view', $v['id']); } else if ($type = 2) { diff --git a/app/home/controller/Note.php b/app/home/controller/Note.php index 5fedb37..1305897 100644 --- a/app/home/controller/Note.php +++ b/app/home/controller/Note.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\Note as NoteList; use app\home\model\NoteCate; use app\home\validate\NoteCateCheck; @@ -158,6 +158,8 @@ class Note extends BaseController $sid = NoteList::strict(false)->field(true)->insertGetId($param); if ($sid) { add_log('add', $sid, $param); + $users= Db::name('Admin')->field('id as from_uid')->where(['status' => 1])->column('id'); + sendMessage($users,1,['title'=>$param['title'],'action_id'=>$sid]); } return to_assign(); diff --git a/app/home/controller/Personal.php b/app/home/controller/Personal.php index 7faeb65..e45f442 100644 --- a/app/home/controller/Personal.php +++ b/app/home/controller/Personal.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\DepartmentChange as DepartmentChange; use app\home\model\PersonalQuit as PersonalQuit; use think\exception\ValidateException; diff --git a/app/home/controller/Plan.php b/app/home/controller/Plan.php index ff3aa59..a246285 100644 --- a/app/home/controller/Plan.php +++ b/app/home/controller/Plan.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\Plan as PlanList; use schedule\Schedule as ScheduleIndex; use think\facade\Db; diff --git a/app/home/controller/Position.php b/app/home/controller/Position.php index b65e611..52a284f 100644 --- a/app/home/controller/Position.php +++ b/app/home/controller/Position.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\validate\PositionCheck; use think\exception\ValidateException; use think\facade\Db; diff --git a/app/home/controller/Role.php b/app/home/controller/Role.php index ffe8687..f25e66d 100644 --- a/app/home/controller/Role.php +++ b/app/home/controller/Role.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\AdminGroup; use app\home\validate\GroupCheck; use think\exception\ValidateException; diff --git a/app/home/controller/Rule.php b/app/home/controller/Rule.php index c58785e..b629d37 100644 --- a/app/home/controller/Rule.php +++ b/app/home/controller/Rule.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\validate\RuleCheck; use think\exception\ValidateException; use think\facade\Db; diff --git a/app/home/controller/Schedule.php b/app/home/controller/Schedule.php index be3d25d..c8dad96 100644 --- a/app/home/controller/Schedule.php +++ b/app/home/controller/Schedule.php @@ -9,7 +9,7 @@ declare (strict_types = 1); namespace app\home\controller; -use app\home\BaseController; +use app\base\BaseController; use app\home\model\Schedule as ScheduleList; use schedule\Schedule as ScheduleIndex; use think\facade\Db; diff --git a/app/home/model/Mail.php b/app/home/model/Mail.php deleted file mode 100644 index 83b46cb..0000000 --- a/app/home/model/Mail.php +++ /dev/null @@ -1,55 +0,0 @@ - '无', - self::SOURCE_ONE => '已发消息', - self::SOURCE_TWO => '草稿消息', - self::SOURCE_THREE => '已收消息', - ]; - - public static $Type = [ - self::TYPE_ZERO => '同事', - self::TYPE_ONE => '部门', - self::TYPE_TWO => '岗位', - self::TYPE_THREE => '全部', - ]; - - public static $MailType = [ - self::MAILTYPE_ONE => '系统消息', - self::SOURCE_TWO => '同事消息', - ]; - -} diff --git a/app/home/model/Message.php b/app/home/model/Message.php new file mode 100644 index 0000000..5c535c6 --- /dev/null +++ b/app/home/model/Message.php @@ -0,0 +1,85 @@ + '无', + self::ONE => '已发消息', + self::TWO => '草稿消息', + self::THREE => '已收消息', + ]; + + public static $Type = [ + self::ZERO => '系统', + self::ONE => '同事', + self::TWO => '部门', + self::THREE => '岗位', + self::FOUR => '全部', + ]; + + //获取消息列表 + public function getList($map = [], $param = [],$uid) + { + $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit']; + //垃圾箱列表特殊处理 + if ($param['status'] == 0) { + $where = [['from_uid', '=', $uid], ['to_uid', '=', $uid]]; + $mail = $this->where($map) + ->where(function ($query) use ($where) {$query->whereOr($where);}) + ->order('create_time desc') + ->paginate($rows, false, ['query' => $param]) + ->each(function ($item, $key) { + if($item->template==0){ + $item->msg_type = '个人信息'; + $item->from_name = Db::name('Admin')->where(['id' => $item->from_uid])->value('name'); + } + else{ + $item->msg_type = '系统信息'; + $item->from_name = '系统'; + } + $item->send_time = empty($item->send_time) ? '-' : date('Y-m-d H:i:s', $item->send_time); + $item->to_name = Db::name('Admin')->where(['id' => $item->to_uid])->value('name'); + $item->type_title = self::$Type[(int)$item->type]; + $item->delete_source_title = self::$Source[(int)$item->delete_source]; + $item->files = Db::name('MessageFileInterfix')->where(['mid' => $item->id, 'status' => 1])->count(); + }); + return $mail; + } else { + $mail = self::where($map) + ->order('create_time desc') + ->paginate($rows, false, ['query' => $param]) + ->each(function ($item, $key) { + if($item->template==0){ + $item->msg_type = '个人信息'; + $item->from_name = Db::name('Admin')->where(['id' => $item->from_uid])->value('name'); + } + else{ + $item->msg_type = '系统信息'; + $item->from_name = '系统'; + } + $item->send_time = empty($item->send_time) ? '-' : date('Y-m-d H:i:s', $item->send_time); + $item->to_name = Db::name('Admin')->where(['id' => $item->to_uid])->value('name'); + $item->type_title = self::$Type[(int)$item->type]; + $item->files = Db::name('MessageFileInterfix')->where(['mid' => $item->id, 'status' => 1])->count(); + }); + return $mail; + } + } +} diff --git a/app/home/view/index/index.html b/app/home/view/index/index.html index ac2294f..bb2b6b2 100644 --- a/app/home/view/index/index.html +++ b/app/home/view/index/index.html @@ -127,8 +127,8 @@ type:'post', success:function(e){ if(e.code==0 && e.data!=''){ - if(e.data.num_mail>0){ - $('#msgNum').show().find('a').html(e.data.num_mail); + if(e.data.msg_num>0){ + $('#msgNum').show().find('a').html(e.data.msg_num); } else{ $('#msgNum').hide().find('a').html(0); @@ -136,7 +136,7 @@ if(isClose==0){ var html = '