LogLogic.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * 日志逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-11-30
  7. */
  8. namespace app\admin\logic;
  9. use app\admin\model\LoginRecord;
  10. use app\admin\model\OperationLog;
  11. use app\admin\model\SystemLog;
  12. class LogLogic
  13. {
  14. /**
  15. * 数据操作日志中的模块对应的中文名称
  16. *
  17. * @var string[]
  18. */
  19. public $recordModules = [
  20. 'crm_leads' => '线索',
  21. 'crm_customer' => '客户',
  22. 'crm_pool' => '客户公海',
  23. 'crm_contacts' => '联系人',
  24. 'crm_product' => '产品',
  25. 'crm_business' => '商机',
  26. 'crm_contract' => '合同',
  27. 'crm_receivables' => '回款',
  28. 'crm_visit' => '回访',
  29. 'crm_invoice' => '回款',
  30. 'oa_log' => '办公日志',
  31. 'work_task' => '任务',
  32. 'work' => '项目',
  33. 'oa_event' => '日程',
  34. 'crm_activity' => '跟进记录',
  35. ];
  36. public $systemModules = [
  37. 'company' => '企业首页',
  38. 'application' => '应用管理',
  39. 'structures' => '部门管理',
  40. 'employee' => '员工管理',
  41. 'role' => '角色管理',
  42. 'approval' => '审批流程管理',
  43. 'workbench' => '工作台',
  44. 'project' => '项目管理',
  45. 'customer' => '客户管理',
  46. 'work_task' => '其他设置'
  47. ];
  48. public $systemAction=[
  49. 'admin_oalog_rule'=>'日志',
  50. 'admin_group'=>'角色',
  51. 'crm_leads' => '线索',
  52. 'crm_customer' => '客户',
  53. 'crm_pool' => '客户公海',
  54. 'crm_contacts' => '联系人',
  55. 'crm_product' => '产品',
  56. 'crm_business' => '商机',
  57. 'crm_contract' => '合同',
  58. 'crm_receivables' => '回款',
  59. 'crm_visit' => '回访',
  60. 'crm_invoice' => '回款',
  61. 'oa_log' => '办公日志',
  62. 'oa_examine' => '办公审批',
  63. 'crm_config' => '客户管理系统设置',
  64. 'crm_number_sequence' => '业绩目标',
  65. 'admin_structure' => '部门',
  66. 'admin_config' => '应用管理',
  67. 'work_task' => '其他设置'
  68. ];
  69. /**
  70. * 日志记录中的行为所对应的中文名称
  71. *
  72. * @var string[]
  73. */
  74. private $action = [
  75. 'save' => '添加数据',
  76. 'update' => '编辑数据',
  77. 'delete' => '删除数据',
  78. 'excel' => '导入数据',
  79. 'excelexport' => '导出数据',
  80. 'lock' => '锁定',
  81. 'islock' => '解锁',
  82. 'status' => '更改成交状态',
  83. 'receive' => '领取',
  84. 'transfer' => '转移',
  85. 'teamSave' => '添加团队成员',
  86. 'distribute' => '分配',
  87. 'up' => '上架',
  88. 'down' => '下架',
  89. 'recover' => '归档恢复',
  90. 'archiveData' => '归档',
  91. 'copy' => '复制',
  92. ];
  93. private $loginType = [
  94. '成功', '密码错误', '账号禁用'
  95. ];
  96. /**
  97. * 登录日志
  98. *
  99. * @param $param
  100. * @return array
  101. * @throws \think\exception\DbException
  102. */
  103. public function getLoginRecord($param)
  104. {
  105. $res= db('admin_login_record')
  106. ->alias('login')
  107. ->join('__ADMIN_USER__ user','user.id=login.create_user_id','LEFT')
  108. ->where(function ($query) use ($param) {
  109. if (!empty($param['startTime'])) $query->where('login.create_time', '>=', strtotime($param['startTime']));
  110. if (!empty($param['endTime'])) $query->where('login.create_time', '<=', strtotime($param['endTime']));
  111. if (!empty($param['userIds'])) $query->whereIn('login.create_user_id', $param['userIds']);
  112. })
  113. ->field('login.*,user.realname as username')
  114. ->page($param['page'],$param['limit'])
  115. ->order('login.id', 'desc')
  116. ->select();
  117. foreach ($res as $k =>$v){
  118. $res[$k]['create_time']=!empty($v['create_time'])?date('Y-m-d H:i:s',$v['create_time']):null;
  119. $res[$k]['type']=$this->loginType[$v['type']];
  120. }
  121. $total= db('admin_login_record')
  122. ->alias('login')
  123. ->join('__ADMIN_USER__ user','user.id=login.create_user_id','LEFT')
  124. ->where(function ($query) use ($param) {
  125. if (!empty($param['startTime'])) $query->where('login.create_time', '>=', strtotime($param['startTime']));
  126. if (!empty($param['endTime'])) $query->where('login.create_time', '<=', strtotime($param['endTime']));
  127. if (!empty($param['userIds'])) $query->whereIn('login.create_user_id', $param['userIds']);
  128. })->count();
  129. $data=[];
  130. $data['list']=$res;
  131. $data['dataCount']=$total;
  132. return $data;
  133. }
  134. /**
  135. * 获取系统操作日志列表
  136. *
  137. * @param $param 查询条件、分页参数
  138. * @return bool|\PDOStatement|string|\think\Collection
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. * @throws \think\exception\DbException
  142. */
  143. public function getSystemLogs($param)
  144. {
  145. $data = db('admin_system_log')
  146. ->alias('system')
  147. ->join('__ADMIN_USER__ user','user.id=system.user_id','LEFT')
  148. ->where(function ($query) use ($param) {
  149. if (!empty($param['startTime'])) $query->where('system.create_time', '>=', strtotime($param['startTime']));
  150. if (!empty($param['endTime'])) $query->where('system.create_time', '<=', strtotime($param['endTime']));
  151. if (!empty($param['modules'])) $query->whereIn('system.module_name', $param['modules']);
  152. if (!empty($param['userIds'])) $query->whereIn('system.user_id', $param['userIds']);
  153. })
  154. ->page($param['page'], $param['limit'])
  155. ->field('system.log_id,system.target_name,system.create_time,system.client_ip,system.module_name,system.content,system.target_name,system.action_name,system.controller_name,user.realname')
  156. ->order('system.log_id', 'desc')->select();
  157. return $this->setSystemData($data);
  158. }
  159. /**
  160. * 获取系统操作日志总数
  161. *
  162. * @param $param 查询条件、分页参数
  163. * @return int|string|null
  164. */
  165. public function getSystemLogCount($param)
  166. {
  167. return SystemLog::where(function ($query) use ($param) {
  168. if (!empty($param['startTime'])) $query->where('create_time', '>=', strtotime($param['startTime']));
  169. if (!empty($param['endTime'])) $query->where('create_time', '<=', strtotime($param['endTime']));
  170. if (!empty($param['modules'])) $query->whereIn('controller_name', $param['modules']);
  171. if (!empty($param['userIds'])) $query->whereIn('user_id', $param['userIds']);
  172. })->count();
  173. }
  174. /**
  175. * 获取数据操作日志列表
  176. *
  177. * @param $param 查询条件、分页参数
  178. * @return bool|\PDOStatement|string|\think\Collection
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. * @throws \think\exception\DbException
  182. */
  183. public function getRecordLogs($param)
  184. {
  185. $data = db('admin_operation_log')
  186. ->alias('operation')
  187. ->join('__ADMIN_USER__ user','user.id=operation.user_id','LEFT')
  188. ->where(function ($query) use ($param) {
  189. if (!empty($param['startTime'])) $query->where('operation.create_time', '>=', strtotime($param['startTime']));
  190. if (!empty($param['endTime'])) $query->where('operation.create_time', '<=', strtotime($param['endTime']));
  191. if (!empty($param['modules'])) $query->whereIn('operation.module', arrayToString($param['modules']));
  192. if (!empty($param['userIds'])) $query->whereIn('operation.user_id', $param['userIds']);
  193. })
  194. ->field('operation.*,user.realname')
  195. ->page($param['page'], $param['limit'])
  196. ->order('operation.log_id', 'desc')->select();
  197. return $this->setRecordData($data);
  198. }
  199. /**
  200. * 获取数据操作日志总数
  201. *
  202. * @param $param 查询条件、分页参数
  203. * @return int|string|null
  204. */
  205. public function getRecordLogCount($param)
  206. {
  207. return db('admin_operation_log')
  208. ->alias('operation')
  209. ->join('__ADMIN_USER__ user','user.id=operation.user_id','LEFT')
  210. ->where(function ($query) use ($param) {
  211. if (!empty($param['startTime'])) $query->where('operation.create_time', '>=', strtotime($param['startTime']));
  212. if (!empty($param['endTime'])) $query->where('operation.create_time', '<=', strtotime($param['endTime']));
  213. if (!empty($param['modules'])) $query->whereIn('operation.module', arrayToString($param['modules']));
  214. if (!empty($param['userIds'])) $query->whereIn('operation.user_id', $param['userIds']);
  215. })->count();
  216. }
  217. /**
  218. * 组装数据操作日志数据
  219. *
  220. * @param $data
  221. * @return mixed
  222. */
  223. private function setRecordData($data)
  224. {
  225. $result = [];
  226. foreach ($data AS $key => $value) {
  227. $result[] = [
  228. 'log_id' => $value['log_id'],
  229. 'source_name' => $value['target_name'],
  230. 'create_time' => date('Y-m-d H:i:s', $value['create_time']),
  231. 'ip' => $value['client_ip'],
  232. 'module' => $this->recordModules[$value['module']],
  233. 'action' => in_array('crm',explode('_',$value['module']))==1?
  234. '客户管理':(in_array('oa',explode('_',$value['module']))==1?'办公管理':'项目管理'),
  235. 'content' => $value['content'],
  236. 'user_name' => $value['realname']?:'系统',
  237. 'action_name' => $this->action[$value['action_name']],
  238. ];
  239. }
  240. return $result;
  241. }
  242. /**
  243. * 组装系统操作数据操作日志数据
  244. *
  245. * @param $data
  246. * @return mixed
  247. */
  248. private function setSystemData($data)
  249. {
  250. $result = [];
  251. foreach ($data AS $key => $value) {
  252. $result[] = [
  253. 'log_id' => $value['log_id'],
  254. 'source_name' => $value['target_name'],
  255. 'create_time' => date('Y-m-d H:i:s', $value['create_time']),
  256. 'ip' => $value['client_ip'],
  257. 'action' => $this->systemModules[$value['module_name']],
  258. 'content' => $value['content'],
  259. 'user_name' => $value['realname']?:'系统',
  260. 'action_name' => $this->action[$value['action_name']],
  261. 'module' => '后台管理',
  262. 'action_down' => $this->recordModules[$value['controller_name']]?:'',
  263. ];
  264. }
  265. return $result;
  266. }
  267. /**
  268. * 导出方法
  269. * @param $param
  270. *
  271. * @author alvin guogaobo
  272. * @version 1.0 版本号
  273. * @since 2021/4/8 0008 16:37
  274. */
  275. public function downExcel($param){
  276. $excelModel = new \app\admin\model\Excel();
  277. $file_name='sysLogs';
  278. $field_list=[];
  279. $type='';
  280. $action=$param['action'];
  281. unset($param['action']);
  282. unset($param['page']);
  283. unset($param['limit']);
  284. $param['startTime']= !empty($param['startTime'])?$param['startTime'].' 00:00:00':'';
  285. $param['endTime']= !empty($param['endTime'])?$param['endTime'].' 23:59:59':'';
  286. switch ($action){
  287. case 'getSystemLogs':
  288. $type='系统日志';
  289. $field_list = [
  290. '0' => ['name' => '用户', 'field' => 'user_name'],
  291. '1' => ['name' => '时间', 'field' => 'create_time'],
  292. '2' => ['name' => 'ip地址', 'field' => 'ip'],
  293. '3' => ['name' => '模块', 'field' => 'module'],
  294. '4' => ['name' => '子模块', 'field' => 'action'],
  295. '5' => ['name' => '行为', 'field' => 'action_name'],
  296. '6' => ['name' => '对象', 'field' => 'source_name'],
  297. '7' => ['name' => '操作详情', 'field' => 'content'],
  298. ];
  299. $modules = $this->systemModules;
  300. if(!empty($param['subModelLabels'])){
  301. $param['modules']=$param['subModelLabels'];
  302. }
  303. $list = db('admin_system_log')
  304. ->alias('system')
  305. ->join('__ADMIN_USER__ user','user.id=system.user_id','LEFT')
  306. ->where(function ($query) use ($param) {
  307. if (!empty($param['startTime'])) $query->where('system.create_time', '>=', strtotime($param['startTime']));
  308. if (!empty($param['endTime'])) $query->where('system.create_time', '<=', strtotime($param['endTime']));
  309. if (!empty($param['modules'])) $query->whereIn('system.module_name', $param['modules']);
  310. if (!empty($param['userIds'])) $query->whereIn('system.user_id', $param['userIds']);
  311. })
  312. ->field('system.log_id,system.target_name,system.create_time,system.client_ip,system.module_name,system.content,system.target_name,system.action_name,system.controller_name,user.realname')
  313. ->order('system.log_id', 'desc')->select();
  314. $data=$this->setSystemData($list);
  315. break;
  316. case 'getRecordLogs':
  317. $type='系统日志';
  318. $field_list = [
  319. '0' => ['name' => '用户', 'field' => 'user_name'],
  320. '1' => ['name' => '时间', 'field' => 'create_time'],
  321. '2' => ['name' => 'ip地址', 'field' => 'ip'],
  322. '3' => ['name' => '模块', 'field' => 'action'],
  323. '4' => ['name' => '子模块', 'field' => 'module'],
  324. '5' => ['name' => '行为', 'field' => 'action_name'],
  325. '6' => ['name' => '对象', 'field' => 'source_name'],
  326. '7' => ['name' => '操作详情', 'field' => 'content'],
  327. ];
  328. if(!empty($param['subModelLabels'])){
  329. $param['modules']=$param['subModelLabels'];
  330. }else{
  331. switch ($param['model']){
  332. case 'crm': //客户管理
  333. $param['modules']=array(
  334. 'crm_leads' ,
  335. 'crm_customer',
  336. 'crm_pool' ,
  337. 'crm_contacts' ,
  338. 'crm_product' ,
  339. 'crm_business' ,
  340. 'crm_contract' ,
  341. 'crm_receivables' ,
  342. 'crm_visit' ,
  343. 'crm_invoice' ,
  344. 'crm_activity'
  345. );
  346. break;
  347. case 'oa' : //办公管理
  348. $param['modules']=array(
  349. 'oa_log' ,
  350. 'oa_event',
  351. );
  352. break;
  353. case 'work' ://项目管理
  354. $param['modules']=array(
  355. 'work_task' ,
  356. 'work',
  357. );
  358. break;
  359. default :
  360. $param['modules']=array(
  361. 'crm_leads' ,
  362. 'crm_customer',
  363. 'crm_pool' ,
  364. 'crm_contacts' ,
  365. 'crm_product' ,
  366. 'crm_business' ,
  367. 'crm_contract' ,
  368. 'crm_receivables' ,
  369. 'crm_visit' ,
  370. 'crm_invoice' ,
  371. 'crm_activity',
  372. 'oa_log' ,
  373. 'oa_event',
  374. 'work_task' ,
  375. 'work',
  376. );
  377. break;
  378. }
  379. }
  380. $list = db('admin_operation_log')
  381. ->alias('operation')
  382. ->join('__ADMIN_USER__ user','user.id=operation.user_id','LEFT')
  383. ->where(function ($query) use ($param) {
  384. if (!empty($param['startTime'])) $query->where('operation.create_time', '>=', strtotime($param['startTime']));
  385. if (!empty($param['endTime'])) $query->where('operation.create_time', '<=', strtotime($param['endTime']));
  386. if (!empty($param['modules'])) $query->whereIn('operation.module', arrayToString($param['modules']));
  387. if (!empty($param['userIds'])) $query->whereIn('operation.user_id', $param['userIds']);
  388. })
  389. ->field('operation.*,user.realname')
  390. ->order('operation.log_id', 'desc')->select();
  391. $data=$this->setRecordData($list);
  392. break;
  393. case 'getLoginRecord':
  394. $type='登陆日志';
  395. $field_list = [
  396. '0' => ['name' => '用户', 'field' => 'username'],
  397. '1' => ['name' => '时间', 'field' => 'create_time'],
  398. '2' => ['name' => 'ip地址', 'field' => 'ip'],
  399. '3' => ['name' => '登陆地点', 'field' => 'address'],
  400. '4' => ['name' => '设备类型', 'field' => 'remark'],
  401. '5' => ['name' => '终端内核', 'field' => 'browser'],
  402. '6' => ['name' => '平台', 'field' => 'os'],
  403. '7' => ['name' => '成功', 'field' => 'type'],
  404. ];
  405. $res = db('admin_login_record')
  406. ->alias('login')
  407. ->join('__ADMIN_USER__ user','user.id=login.create_user_id','LEFT')
  408. ->where(function ($query) use ($param) {
  409. if (!empty($param['startTime'])) $query->where('login.create_time', '>=', strtotime($param['startTime']));
  410. if (!empty($param['endTime'])) $query->where('login.create_time', '<=', strtotime($param['endTime']));
  411. if (!empty($param['userIds'])) $query->whereIn('login.create_user_id', $param['userIds']);
  412. })
  413. ->field('login.*,user.realname as username')
  414. ->order('login.id', 'desc')
  415. ->select();
  416. foreach ($res as $k =>$v){
  417. $res[$k]['create_time']=!empty($v['create_time'])?date('Y-m-d H:i:s',$v['create_time']):null;
  418. $res[$k]['type']=$this->loginType[$v['type']];
  419. }
  420. $data=$res;
  421. break;
  422. }
  423. return $excelModel->biExportExcel($file_name, $field_list, $type, $data);
  424. }
  425. }