Commit 518558b0 by 肖康

Merge branch 'feature/20251222/notice' of…

Merge branch 'feature/20251222/notice' of http://git.ichunt.net/liyang/CMS-Message into feature/20251222/notice

# Conflicts:
#	public/js/Message/MessageList.js
parents a69af574 00c32add
......@@ -9,6 +9,7 @@
namespace App\Http\Controllers;
use App\Logic\SystemNoticeLogic;
use App\Services\ActionLogService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
......@@ -54,6 +55,19 @@ class NoticeController extends Controller
}
public function getLogs(Request $request)
{
$obj_type = $request->input('obj_type');
$obj_id = $request->input('obj_id');
$act_type = $request->input('act_type');
$act_types = ($act_type) ? [$act_type] : [];
$data = [
"list" => ActionLogService::getLogsByObjId($obj_type, $obj_id, $act_types)
];
return $this->codeExport(0, '保存成功',$data);
}
......
......@@ -87,6 +87,7 @@ Route::get ('/notice/getNotice','NoticeController@getNotice');
Route::get('/sync/businessNoticeList','NoticeController@businessNoticeList');
Route::post('/sync/saveBusinessNotice','NoticeController@saveBusinessNotice');
Route::get('/sync/getLogs','NoticeController@getLogs');
......
......@@ -10,6 +10,7 @@ namespace App\Logic;
//系统通知逻辑层
use App\Console\Commands\CreateNotice;
use App\Services\ActionLogService;
use App\Model\BusinessNoticeModel;
use App\Model\SystemBulletinModel;
use App\Model\SystemPreachModel;
......@@ -433,10 +434,14 @@ class SystemNoticeLogic
$data['create_name'] = $user->name;
$data['create_id'] = $user->userId;
$data['create_time'] = $time;
$model->insertGetId($data);
$id = $model->insertGetId($data);
ActionLogService::addLog(100001,$id,'新增了公告');
}else{
$data['update_time'] = $time;
$model->where('business_notice_id',$id)->update($data);
ActionLogService::addLog(100001,$id,'修改了公告');
}
return [];
}
......@@ -454,6 +459,14 @@ class SystemNoticeLogic
{
$idArr = explode(',',$requestData['business_notice_id']);
(new BusinessNoticeModel())->whereIn('business_notice_id',$idArr)->update(['show_status'=>$requestData['show_status']]);
foreach ($idArr as $id){
if (empty($requestData['show_status'])){
ActionLogService::addLog(100001,$id,'隐藏了公告');
}else{
ActionLogService::addLog(100001,$id,'显示了公告');
}
}
return [];
}
......@@ -466,6 +479,14 @@ class SystemNoticeLogic
}
}
(new BusinessNoticeModel())->whereIn('business_notice_id',$idArr)->update(['top_status'=>$requestData['top_status']]);
foreach ($idArr as $id){
if (empty($requestData['top_status'])){
ActionLogService::addLog(100001,$id,'取消置顶了公告');
}else{
ActionLogService::addLog(100001,$id,'置顶了公告');
}
}
return [];
}
......
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class ActionLogModel extends Model
{
protected $connection = 'messagemodel';
protected $table = 'action_log';
public $timestamps = false;
public static function addLog($data)
{
return self::insertGetId($data);
}
//批量插入日志
public static function addLogs($actionLogs)
{
return self::insert($actionLogs);
}
public static function getLogs($obj_type, $obj_id, $limit = 10)
{
return self::where(['obj_type' => $obj_type, 'obj_id' => $obj_id])
->orderBy('id', 'DESC')->limit($limit)->get()->toArray();
}
public static function getLogsByObjId($obj_type, $obj_id)
{
$where = [
['obj_type', '=', $obj_type],
['obj_id', '=', $obj_id],
];
$res = self::where($where)->orderBy('id', 'DESC')->get();
return ($res) ? $res->toArray() : [];
}
public static function getLogsByObjIds($obj_type, $obj_ids)
{
$where = [
['obj_type', '=', $obj_type],
];
$res = self::where($where)->whereIn('obj_id', $obj_ids)->orderBy('id', 'DESC')->get();
return ($res) ? $res->toArray() : [];
}
public static function getLogsByObjIdAndTypes($obj_type, $obj_id, $act_types)
{
$where = [
['obj_type', '=', $obj_type],
['obj_id', '=', $obj_id],
];
$res = self::where($where)->whereIn('act_type', $act_types)->orderBy('id', 'DESC')->get();
return ($res) ? $res->toArray() : [];
}
public static function getLastLogByActTypeAndObjId($act_type,$obj_id)
{
$res = self::where('act_type', $act_type)->where('obj_id', $obj_id)->orderBy('id', 'DESC')->first();
return ($res) ? $res->toArray() : [];
}
}
<?php
/**
* Created by PhpStorm.
* User: duwenjun
* Date: 2021/8/10
* Time: 5:59 PM
*/
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class CmsUserInfoModel extends Model
{
protected $connection = 'mysql';
protected $table = 'user_info';
const GENDER_TYPE_MAN = 1; // 男性
const GENDER_TYPE_WOMAN = 2; // 女性
const STATUS_ENABLE = 0; // 状态-正常
public $timestamps = false;
//获取下属用户ids
public static function getInferiorUserIds($userId)
{
$departmentId = self::where('userId', $userId)->value('department_id');
$users = CmsUserDepartmentModel::getUserByDepartmentId($departmentId);
return array_column($users, 'userId');
}
public static function getSalesBuyerList($where, $department_ids, $page = 1, $limit = 10)
{
if (empty($where)) {
return self::whereIn('department_id', $department_ids)->orderBy('userId',
'desc')->paginate($limit, ['*'], 'p', $page)->toArray();
} else {
return self::whereIn('department_id', $department_ids)->where($where)->orderBy('userId',
'desc')->paginate($limit, ['*'], 'p', $page)->toArray();
}
}
public static function getSalesByPrefixName($name, $department_id)
{
CmsDepartmentModel::getSubDepartmentId($department_id, $departmentIds);
return self::selectRaw("userId as id,name")->whereIn('department_id', $departmentIds)->where("name", "like",
"%" . trim($name) . "%")->orderBy('userId',
'desc')->get()->toArray();
}
public static function getUserListByPrefixName($name)
{
return self::selectRaw("userId as id,name")->where("name", "like",
"%" . trim($name) . "%")->orderBy('userId',
'desc')->get()->toArray();
}
public static function getUserInfoById($user_id)
{
$res = self::where('userId', $user_id)->where("status",0)->first();
return ($res) ? $res->toArray() : [];
}
public static function getUserInfoByName($name)
{
$res = self::where('name', $name)->first();
return ($res) ? $res->toArray() : [];
}
public static function updateByWhere($data, $where)
{
if (!isset($data['mtime'])) {
$data['mtime'] = date("Y-m-d H:i:s");
}
return self::where($where)->update($data);
}
public static function getUsers($user_ids)
{
$res = self::whereIn('userId', $user_ids)->get();
return ($res) ? $res->toArray() : [];
}
public static function getUserInfo($user_id)
{
$res = self::select('department_name', 'name',"department_id",'org_id')->where('userId', $user_id)->first();
return ($res) ? $res->toArray() : [];
}
public static function getUserName($userId)
{
if (empty($userId)) {
return '';
}
$res = self::select('name')->where('userId', $userId)->get()->toArray();
if (!isset($res[0])) {
return -1;
}
return $res[0]['name'];
}
public static function getUsersByPositionId($position_id)
{
$res = self::where('position_id', $position_id)->where('status', self::STATUS_ENABLE)->get();
return ($res) ? $res->toArray() : [];
}
public static function getUserIdsByDepartmentIds($department_ids = [])
{
$res = self::whereIn('department_id', $department_ids)->pluck('userId');
return ($res) ? $res->toArray() : [];
}
public static function getUsersByDepartmentIdAndPositionNames($department_id, $position_names)
{
$res = self::where('department_id', $department_id)->where('status', self::STATUS_ENABLE)->whereIn('position_name', $position_names)->get();
return ($res) ? $res->toArray() : [];
}
public static function getCreatorUserInfo($create_name)
{
$creator_user_info = [];
$userInfo = self::getUserInfoByName($create_name);
if ($userInfo) {
$creator_user_info = [
"uid" => $userInfo['userId'],
"name" => $userInfo['name']
];
}
return $creator_user_info;
}
public static function getDepartmentList($user_ids)
{
$res = self::selectRaw('department_name as department , userId as user_id')->whereIn('userId',
$user_ids)->get();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentAllUser($department_id) {
$res = self::selectRaw('name as user_name , userId as user_id')->where("status",0)->where('department_id', $department_id)->get();
return ($res) ? $res->toArray() : [];
}
}
<?php
namespace App\Services;
use App\Model\ActionLogModel;
use App\Model\CmsUserInfoModel;
class ActionLogService{
const ABNORMAL = 100; //异常管理
const ABNORMAL_CREATE = 100001; //操作
public static function getLogsDataByObjId($obj_type, $obj_id)
{
$action_logs = ActionLogModel::getLogsByObjId($obj_type, $obj_id);
foreach ($action_logs as &$log) {
$log['create_time'] = NameConvert::getDateTime($log['create_time']);
$log['create_name'] = CmsUserInfoModel::getUserName($log['act_uid']);
$logData = json_decode($log['log_data'], true);
$log['log_data'] = isset($logData['message']) ? $logData['message'] : $logData;
}
return $action_logs;
}
public static function addLog($action_type, $obj_id, $log_data)
{
$action_log_info = self::getInsertLogInfo($action_type, $obj_id, $log_data);
return ActionLogModel::addLog($action_log_info);
}
// 批量添加日志,只有obj_ids不一样,其他都一样
public static function addMultiLogsWithObjIds($action_type, $obj_ids, $log_data)
{
$action_logs = [];
if ($obj_ids && is_array($obj_ids)) {
foreach ($obj_ids as $obj_id) {
$action_logs[] = self::getInsertLogInfo($action_type, $obj_id, $log_data);
}
}
if ($action_logs) {
return ActionLogModel::addLogs($action_logs);
}
return true;
}
// 批量添加日志
/*
*
* $log_list = [['action_type' => $action_type, 'obj_id' => $obj_id, 'log_data' => ['message' => 'hello']]]
*
*
*/
public static function addMultiLogs($log_list)
{
$action_logs = [];
if ($log_list && is_array($log_list)) {
foreach ($log_list as $log) {
$action_logs[] = self::getInsertLogInfo($log['action_type'], $log['obj_id'], $log['log_data']);
}
}
if ($action_logs) {
return ActionLogModel::addLogs($action_logs);
}
return true;
}
//为什么要独立出来,因为同步是没有session,所以没有用户,而且怕还会有其它不一样,所以要独立出来一个方法
public static function addSyncLog($action_type, $obj_id, $log_data)
{
$action_log_info = [
"act_uid" => 1000,
"act_type" => $action_type,
"obj_type" => self::getObjTypeFromActionType($action_type),
"obj_id" => $obj_id,
"log_data" => json_encode([
'message' => $log_data,
]),
"create_uid" => 1000,
"create_time" => time()
];
return ActionLogModel::addLog($action_log_info);
}
/** 批量插入日志
* $log_data 的数据格式应该是这样 :
* [
* 'obj_id' => 1,
* 'log_data' => 'xxx修改了xxx'
* ]
**/
public static function addLogs($action_type, $log_data)
{
if (!$log_data) {
return true;
}
$action_logs = [];
foreach ($log_data as $data) {
if (!$data) {
continue;
}
if (!isset($data['obj_id']) || !isset($data['log_data'])) {
throw new InvalidRequestException('日志的obj_id或者log_data不能为空');
}
$action_logs[] = [
"act_uid" => request()->user->userId,
"act_type" => $action_type,
"obj_type" => self::getObjTypeFromActionType($action_type),
"obj_id" => $data['obj_id'],
"log_data" => self::dataEncode($data['log_data']),
"create_uid" => request()->user->userId,
"create_time" => time()
];
}
return ActionLogModel::addLogs($action_logs);
}
public static function getCmsUserNameMap($user_ids)
{
$user_name_map = [];
$user_list = CmsUserInfoModel::getUsers($user_ids);
if ($user_list) {
foreach ($user_list as $user) {
$user_name_map[$user['userId']] = $user['name'];
}
}
return $user_name_map;
}
public static function dataDecode($data)
{
$message_data = [];
$log_data = json_decode($data, 1);
if ($log_data && isset($log_data['message'])) {
$message_data = $log_data;
}
return $message_data;
}
// 获取日志
public static function getLogsByObjId($obj_type, $obj_id, $act_types = [])
{
// 如果指定行为日志,那么就只获取特定的
if ($act_types){
$action_logs = ActionLogModel::getLogsByObjIdAndTypes($obj_type, $obj_id, $act_types);
} else {
$action_logs = ActionLogModel::getLogsByObjId($obj_type, $obj_id);
}
if ($action_logs) {
$create_uids = array_column($action_logs, 'act_uid');
$cms_user_name_map = self::dataDecode($create_uids);
foreach ($action_logs as &$log) {
$log['log_data'] = self::dataDecode($log['log_data']);
$log['create_time'] = empty($log['create_time'])?'':date('Y-m-d H:i:s',$log['create_time']);
}
}
return $action_logs;
}
public static function getLogs($obj_type, $obj_id)
{
$action_logs = ActionLogModel::getLogs($obj_type, $obj_id);
$create_uids = array_column($action_logs, 'act_uid');
$cms_user_name_map = CmsUserService::getCmsUserNameMap($create_uids);
if ($action_logs) {
foreach ($action_logs as &$log) {
$logData = json_decode($log['log_data'], true);
$log['log_data'] = isset($logData['message']) ? $logData['message'] : $logData;
$log['create_time'] = date('Y-m-d H:i', $log['create_time']);
$log['create_name'] = (isset($cms_user_name_map[$log['act_uid']])) ? $cms_user_name_map[$log['act_uid']] : '';
}
unset($log);
}
return $action_logs;
}
// 获取格式化插入信息
private static function getInsertLogInfo($action_type, $obj_id, $log_data)
{
$action_log_info = [
"act_uid" => (request()->user) ? request()->user->userId : 0,
"act_type" => $action_type,
"obj_type" => self::getObjTypeFromActionType($action_type),
"obj_id" => $obj_id,
"log_data" => self::dataEncode($log_data),
"create_uid" => (request()->user) ? request()->user->userId : 0,
"create_time" => time(),
"create_name" => (request()->user) ? request()->user->name : '',
];
return $action_log_info;
}
//判断日志是否存在,比如生产日志那块,如果队列进行连续多次重试,会生成重复的日志,所以要对一定时间内的日志进行过滤
public static function checkLogIsRepeated($action_type, $obj_id ,$durationTime = 15)
{
return ActionLogModel::where([
'act_type' => $action_type,
'obj_id' => $obj_id,
])->where('create_time','>' ,time() - $durationTime)->exists();
}
private static function getObjTypeFromActionType($action_type)
{
return substr($action_type, 0, 3);
}
private static function dataEncode($data)
{
return json_encode($data);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment