LogLogic.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. 'oa_examine' => '办公审批',
  32. 'work_task' => '任务',
  33. 'work' => '项目',
  34. 'label' => '标签',
  35. 'calendar' => '日历'
  36. ];
  37. public $systemModules = [
  38. 'company' => '企业首页',
  39. 'application' => '应用管理',
  40. 'structures' => '部门管理',
  41. 'employee' => '员工管理',
  42. 'role' => '角色管理',
  43. 'approval' => '审批流程管理',
  44. 'workbench' => '工作台',
  45. 'project' => '项目管理',
  46. 'customer' => '客户管理'
  47. ];
  48. /**
  49. * 日志记录中的行为所对应的中文名称
  50. *
  51. * @var string[]
  52. */
  53. private $action = [
  54. 'index' => '查看数据',
  55. 'save' => '添加数据',
  56. 'update' => '编辑数据',
  57. 'delete' => '删除数据'
  58. ];
  59. private $loginType = [
  60. '成功', '密码错误', '账号禁用'
  61. ];
  62. /**
  63. * 登录日志
  64. *
  65. * @param $param
  66. * @return array
  67. * @throws \think\exception\DbException
  68. */
  69. public function getLoginRecord($param)
  70. {
  71. $loginRecordModel = new LoginRecord();
  72. $limit = !empty($param['limit']) ? $param['limit'] : 15;
  73. $data = $loginRecordModel->where(function ($query) use ($param) {
  74. if (!empty($param['startTime'])) $query->where('create_time', '>=', strtotime($param['startTime']));
  75. if (!empty($param['endTime'])) $query->where('create_time', '<=', strtotime($param['endTime']));
  76. if (!empty($param['userIds'])) $query->whereIn('create_user_id', $param['userIds']);
  77. })->order('id', 'desc')->paginate($limit)->each(function ($value) {
  78. $value['username'] = $value->create_user_info['realname'];
  79. $value['type'] = $this->loginType[$value['type']];
  80. })->toArray();
  81. return ['list' => $data['data'], 'count' => $data['total']];
  82. }
  83. /**
  84. * 获取系统操作日志列表
  85. *
  86. * @param $param 查询条件、分页参数
  87. * @return bool|\PDOStatement|string|\think\Collection
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @throws \think\exception\DbException
  91. */
  92. public function getSystemLogs($param)
  93. {
  94. $data = SystemLog::with(['toAdminUser'])
  95. ->where(function ($query) use ($param) {
  96. if (!empty($param['startTime'])) $query->where('create_time', '>=', strtotime($param['startTime']));
  97. if (!empty($param['endTime'])) $query->where('create_time', '<=', strtotime($param['endTime']));
  98. if (!empty($param['modules'])) $query->whereIn('modules', $param['modules']);
  99. if (!empty($param['userIds'])) $query->whereIn('user_id', $param['userIds']);
  100. })->limit(($param['page'] - 1) * $param['limit'])->order('log_id', 'desc')->select();
  101. return $this->setSystemData($data);
  102. }
  103. /**
  104. * 获取系统操作日志总数
  105. *
  106. * @param $param 查询条件、分页参数
  107. * @return int|string|null
  108. */
  109. public function getSystemLogCount($param)
  110. {
  111. return SystemLog::where(function ($query) use ($param) {
  112. if (!empty($param['startTime'])) $query->where('create_time', '>=', strtotime($param['startTime']));
  113. if (!empty($param['endTime'])) $query->where('create_time', '<=', strtotime($param['endTime']));
  114. if (!empty($param['modules'])) $query->whereIn('controller_name', $param['modules']);
  115. if (!empty($param['userIds'])) $query->whereIn('user_id', $param['userIds']);
  116. })->count();
  117. }
  118. /**
  119. * 获取数据操作日志列表
  120. *
  121. * @param $param 查询条件、分页参数
  122. * @return bool|\PDOStatement|string|\think\Collection
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @throws \think\exception\DbException
  126. */
  127. public function getRecordLogs($param)
  128. {
  129. $data = OperationLog::with(['toAdminUser'])
  130. ->where(function ($query) use ($param) {
  131. if (!empty($param['startTime'])) $query->where('create_time', '>=', strtotime($param['startTime']));
  132. if (!empty($param['endTime'])) $query->where('create_time', '<=', strtotime($param['endTime']));
  133. if (!empty($param['modules'])) $query->whereIn('module', $param['modules']);
  134. if (!empty($param['userIds'])) $query->whereIn('user_id', $param['userIds']);
  135. })
  136. ->limit(($param['page'] - 1) * $param['limit'])->order('log_id', 'desc')->select();
  137. return $this->setRecordData($data);
  138. }
  139. /**
  140. * 获取数据操作日志总数
  141. *
  142. * @param $param 查询条件、分页参数
  143. * @return int|string|null
  144. */
  145. public function getRecordLogCount($param)
  146. {
  147. return OperationLog::where(function ($query) use ($param) {
  148. if (!empty($param['startTime'])) $query->where('create_time', '>=', strtotime($param['startTime']));
  149. if (!empty($param['endTime'])) $query->where('create_time', '<=', strtotime($param['endTime']));
  150. if (!empty($param['modules'])) $query->whereIn('module', $param['module']);
  151. if (!empty($param['userIds'])) $query->whereIn('user_id', $param['userIds']);
  152. })->count();
  153. }
  154. /**
  155. * 组装数据操作日志数据
  156. *
  157. * @param $data
  158. * @return mixed
  159. */
  160. private function setRecordData($data)
  161. {
  162. $result = [];
  163. foreach ($data AS $key => $value) {
  164. $result[] = [
  165. 'log_id' => $value['log_id'],
  166. 'source_name' => $value['source_name'],
  167. 'create_time' => date('Y-m-d H:i:s', $value['create_time']),
  168. 'ip' => $value['client_ip'],
  169. 'module' => $this->recordModules[$value['module']],
  170. 'content' => $value['content']
  171. ];
  172. }
  173. return $result;
  174. }
  175. /**
  176. * 组装数据操作日志数据
  177. *
  178. * @param $data
  179. * @return mixed
  180. */
  181. private function setSystemData($data)
  182. {
  183. $result = [];
  184. foreach ($data AS $key => $value) {
  185. $result[] = [
  186. 'log_id' => $value['log_id'],
  187. 'source_name' => $value['source_name'],
  188. 'create_time' => date('Y-m-d H:i:s', $value['create_time']),
  189. 'ip' => $value['client_ip'],
  190. 'module' => $this->systemModules[$value['module']],
  191. 'content' => $value['content']
  192. ];
  193. }
  194. return $result;
  195. }
  196. }