LoginLog.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\controller;
  4. use app\BaseController;
  5. use think\facade\Request;
  6. use app\model\UserLoginLogModel;
  7. use app\model\UserModel;
  8. /**
  9. * 登录日志控制器
  10. */
  11. class LoginLog extends BaseController
  12. {
  13. /**
  14. * 获取登录日志列表
  15. */
  16. public function list()
  17. {
  18. $userInfo = $this->request->userInfo;
  19. // 获取查询参数
  20. $page = Request::get('page', 1, 'intval');
  21. $limit = Request::get('limit', 20, 'intval');
  22. $userId = Request::get('user_id', 0, 'intval');
  23. // 过滤条件
  24. $filters = [
  25. 'login_status' => Request::get('login_status', ''),
  26. 'login_ip' => Request::get('login_ip', '', 'trim'),
  27. 'start_time' => Request::get('start_time', '', 'trim'),
  28. 'end_time' => Request::get('end_time', '', 'trim'),
  29. ];
  30. try {
  31. $result = UserLoginLogModel::getLoginLogs($userInfo['merchant_id'], $userId, $page, $limit, $filters);
  32. return json_success($result, '获取成功');
  33. } catch (\Exception $e) {
  34. return json_error([], '获取登录日志失败:' . $e->getMessage());
  35. }
  36. }
  37. /**
  38. * 获取登录统计信息
  39. */
  40. public function statistics()
  41. {
  42. $userInfo = $this->request->userInfo;
  43. $userId = Request::get('user_id', 0, 'intval');
  44. $startDate = Request::get('start_date', '', 'trim');
  45. $endDate = Request::get('end_date', '', 'trim');
  46. // 如果没有指定日期范围,默认获取最近30天
  47. if (empty($startDate)) {
  48. $startDate = date('Y-m-d', strtotime('-30 days'));
  49. }
  50. if (empty($endDate)) {
  51. $endDate = date('Y-m-d');
  52. }
  53. try {
  54. $statistics = UserLoginLogModel::getLoginStatistics($userInfo['merchant_id'], $userId, $startDate, $endDate);
  55. return json_success([
  56. 'statistics' => $statistics,
  57. 'date_range' => [
  58. 'start_date' => $startDate,
  59. 'end_date' => $endDate
  60. ]
  61. ], '获取成功');
  62. } catch (\Exception $e) {
  63. return json_error([], '获取登录统计失败:' . $e->getMessage());
  64. }
  65. }
  66. /**
  67. * 获取用户最近登录记录
  68. */
  69. public function recentLogs()
  70. {
  71. $userInfo = $this->request->userInfo;
  72. $userId = Request::get('user_id', 0, 'intval');
  73. $limit = Request::get('limit', 10, 'intval');
  74. // 如果没有指定用户ID,则获取当前用户的登录记录
  75. if ($userId == 0) {
  76. $userId = $userInfo['user_id'];
  77. } else {
  78. // 验证用户是否属于当前商户
  79. $user = UserModel::where('user_id', $userId)
  80. ->where('merchant_id', $userInfo['merchant_id'])
  81. ->find();
  82. if (!$user) {
  83. return json_error([], '用户不存在');
  84. }
  85. }
  86. try {
  87. $logs = UserLoginLogModel::getRecentLogs($userId, $limit);
  88. // 格式化日志数据
  89. foreach ($logs as &$log) {
  90. $log['status_text'] = $log['login_status'] == UserLoginLogModel::STATUS_SUCCESS ? '成功' : '失败';
  91. $log['login_time_text'] = date('Y-m-d H:i:s', $log['login_time']);
  92. }
  93. return json_success([
  94. 'logs' => $logs,
  95. 'user_id' => $userId
  96. ], '获取成功');
  97. } catch (\Exception $e) {
  98. return json_error([], '获取最近登录记录失败:' . $e->getMessage());
  99. }
  100. }
  101. /**
  102. * 导出登录日志
  103. */
  104. public function export()
  105. {
  106. $userInfo = $this->request->userInfo;
  107. // 获取查询参数
  108. $userId = Request::get('user_id', 0, 'intval');
  109. // 过滤条件
  110. $filters = [
  111. 'login_status' => Request::get('login_status', ''),
  112. 'login_ip' => Request::get('login_ip', '', 'trim'),
  113. 'start_time' => Request::get('start_time', '', 'trim'),
  114. 'end_time' => Request::get('end_time', '', 'trim'),
  115. ];
  116. try {
  117. // 获取所有符合条件的数据(不分页)
  118. $result = UserLoginLogModel::getLoginLogs($userInfo['merchant_id'], $userId, 1, 100000, $filters);
  119. // 生成CSV数据
  120. $csvData = "ID,用户名,昵称,登录设备,登录IP,登录时间,登录状态\n";
  121. foreach ($result['list'] as $log) {
  122. // 格式化登录时间
  123. $loginTime = date('Y-m-d H:i:s', $log['login_time']);
  124. // 处理设备信息,移除换行符
  125. $device = str_replace(["\r", "\n"], ' ', $log['login_device']);
  126. $csvData .= sprintf(
  127. "%d,%s,%s,%s,%s,%s,%s\n",
  128. $log['id'],
  129. $log['user_name'],
  130. $log['nick_name'],
  131. $device,
  132. $log['login_ip'],
  133. $loginTime,
  134. $log['status_text']
  135. );
  136. }
  137. // 返回CSV数据
  138. return response($csvData)
  139. ->header(['Content-Type' => 'text/csv; charset=utf-8'])
  140. ->header(['Content-Disposition' => 'attachment; filename="login_logs_' . date('YmdHis') . '.csv"'])
  141. ->header(['Cache-Control' => 'no-cache, must-revalidate']);
  142. } catch (\Exception $e) {
  143. return json_error([], '导出登录日志失败:' . $e->getMessage());
  144. }
  145. }
  146. /**
  147. * 获取登录日志详情
  148. */
  149. public function detail()
  150. {
  151. $userInfo = $this->request->userInfo;
  152. $id = Request::get('id', 0, 'intval');
  153. if (!$id) {
  154. return json_error([], '日志ID不能为空');
  155. }
  156. try {
  157. $log = UserLoginLogModel::where('id', $id)
  158. ->where('merchant_id', $userInfo['merchant_id'])
  159. ->find();
  160. if (!$log) {
  161. return json_error([], '日志不存在');
  162. }
  163. // 获取用户信息
  164. $user = UserModel::where('user_id', $log->user_id)->find();
  165. if ($user) {
  166. $log->user_name = $user->user_name;
  167. $log->nick_name = $user->nick_name;
  168. }
  169. // 格式化数据
  170. $log->status_text = $log->login_status == UserLoginLogModel::STATUS_SUCCESS ? '成功' : '失败';
  171. $log->login_time_text = date('Y-m-d H:i:s', $log->login_time);
  172. return json_success($log, '获取成功');
  173. } catch (\Exception $e) {
  174. return json_error([], '获取日志详情失败:' . $e->getMessage());
  175. }
  176. }
  177. }