Log.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-31 20:43
  10. // +----------------------------------------------------------------------
  11. namespace app\system\controller;
  12. use app\BaseController;
  13. use app\model\system\AdminLog;
  14. use app\model\api\UserLogin;
  15. use app\model\api\User as UserModel;
  16. use library\services\UtilService;
  17. use library\utils\IpLocation;
  18. class Log extends BaseController
  19. {
  20. public function list()
  21. {
  22. $pageSize = 50;
  23. $post = UtilService::getMore([
  24. ['page', 1],
  25. ['data', ''],
  26. ['action', ''],
  27. ['username', ''],
  28. ['ip', ''],
  29. ]);
  30. list($pageCount, $data) = (new AdminLog())
  31. ->getList($post['page'], [function ($query) use ($post) {
  32. if ($post['data'] == "lately7") {
  33. $sTime = date('Y-m-d', strtotime('-7 day'));
  34. $eTime = date('Y-m-d');
  35. $query->whereBetweenTime('time', $sTime, $eTime);
  36. } else if ($post['data'] == "lately30") {
  37. $sTime = date('Y-m-d', strtotime('-30 day'));
  38. $eTime = date('Y-m-d');
  39. $query->whereBetweenTime('time', $sTime, $eTime);
  40. } else if ($post['data'] == "today") {
  41. $query->whereTime('time', 'today');
  42. } else if ($post['data'] == "yesterday") {
  43. $query->whereTime('time', 'yesterday');
  44. } else if ($post['data'] == "month") {
  45. $query->whereMonth('time');
  46. } else if ($post['data'] == "year") {
  47. $query->whereYear('time');
  48. } else {
  49. $split = explode('-', $post['data']);
  50. if (count($split) == 2) {
  51. $sTime = $split[0];
  52. $eTime = $split[1];
  53. $query->whereBetweenTime('time', $sTime, $eTime);
  54. }
  55. }
  56. if (!empty($post['username'])) {
  57. $query->whereLike('admin_username', "%{$post['username']}%");
  58. }
  59. if (!empty($post['action'])) {
  60. $query->whereLike('action', "%{$post['action']}%");
  61. }
  62. if (!empty($post['ip'])) {
  63. $query->whereLike('ip', "%{$post['ip']}%");
  64. }
  65. },
  66. ], $pageSize, '*', 'id desc');
  67. $result = UtilService::getParam([
  68. 'id',
  69. 'ip',
  70. 'admin_username',
  71. 'action',
  72. 'controller',
  73. 'title',
  74. 'post',
  75. 'get',
  76. ['time', 'time', 'date("Y-m-d H:i:s",$1)'],
  77. ['ipara', 'ip', function ($item) {
  78. return "";
  79. // return (new IpLocation())->getlocation($item)['country'];
  80. }],
  81. ], $data);
  82. return app('json')->success([
  83. 'list' => $result,
  84. 'pageCount' => $pageCount,
  85. 'pageSize' => $pageSize,
  86. 'page' => $post['page'],
  87. ]);
  88. }
  89. /**
  90. * 清理一个月之前的数据
  91. */
  92. public function delMonth()
  93. {
  94. $time = strtotime('- 1 month');
  95. AdminLog::where('time', '<', $time)->delete();
  96. return app('json')->success('清理成功');
  97. }
  98. public function listLoginLog()
  99. {
  100. $pageSize = 50;
  101. $post = UtilService::getMore([
  102. ['page', 1],
  103. ['data', ''],
  104. ['uid', ''],
  105. ['mobile', ''],
  106. ]);
  107. $where = [];
  108. if(!empty($post["uid"])){
  109. $where["l.uid"] = $post["uid"];
  110. }
  111. if(!empty($post["mobile"])){
  112. $udata = (new UserModel)->where("mobile",$post["mobile"])->find();
  113. if(!empty($udata)){
  114. $where["l.uid"] = $udata["uid"];
  115. }
  116. }
  117. $data = (new UserLogin)
  118. ->alias("l")
  119. ->field("l.*,u.mobile,u.nickname")
  120. ->leftJoin("user u","u.uid = l.uid")
  121. ->where($where)
  122. ->when(!empty($post["data"]), function ($query) use ($post) {
  123. if ($post['data'] == "lately7") {
  124. $sTime = date('Y-m-d', strtotime('-7 day'));
  125. $eTime = date('Y-m-d');
  126. $query->whereBetweenTime('l.time', $sTime, $eTime);
  127. } else if ($post['data'] == "lately30") {
  128. $sTime = date('Y-m-d', strtotime('-30 day'));
  129. $eTime = date('Y-m-d');
  130. $query->whereBetweenTime('l.time', $sTime, $eTime);
  131. } else if ($post['data'] == "today") {
  132. $query->whereTime('l.time', 'today');
  133. } else if ($post['data'] == "yesterday") {
  134. $query->whereTime('l.time', 'yesterday');
  135. } else if ($post['data'] == "month") {
  136. $query->whereMonth('l.time');
  137. } else if ($post['data'] == "year") {
  138. $query->whereYear('l.time');
  139. } else {
  140. $split = explode('-', $post['data']);
  141. if (count($split) == 2) {
  142. $sTime = $split[0];
  143. $eTime = $split[1];
  144. $query->whereBetweenTime('l.time', $sTime, $eTime);
  145. }
  146. }
  147. })
  148. ->page((int)$post["page"], $pageSize)
  149. ->order("l.id desc")
  150. ->select()
  151. ->toArray();
  152. $pageCount = (new UserLogin)
  153. ->alias("l")
  154. ->where($where)
  155. ->when(!empty($post["data"]), function ($query) use ($post) {
  156. if ($post['data'] == "lately7") {
  157. $sTime = date('Y-m-d', strtotime('-7 day'));
  158. $eTime = date('Y-m-d');
  159. $query->whereBetweenTime('l.time', $sTime, $eTime);
  160. } else if ($post['data'] == "lately30") {
  161. $sTime = date('Y-m-d', strtotime('-30 day'));
  162. $eTime = date('Y-m-d');
  163. $query->whereBetweenTime('l.time', $sTime, $eTime);
  164. } else if ($post['data'] == "today") {
  165. $query->whereTime('l.time', 'today');
  166. } else if ($post['data'] == "yesterday") {
  167. $query->whereTime('l.time', 'yesterday');
  168. } else if ($post['data'] == "month") {
  169. $query->whereMonth('l.time');
  170. } else if ($post['data'] == "year") {
  171. $query->whereYear('l.time');
  172. } else {
  173. $split = explode('-', $post['data']);
  174. if (count($split) == 2) {
  175. $sTime = $split[0];
  176. $eTime = $split[1];
  177. $query->whereBetweenTime('l.time', $sTime, $eTime);
  178. }
  179. }
  180. })
  181. ->order("l.id desc")
  182. ->count();
  183. foreach($data as $k=>$v){
  184. $data[$k]["time"] = date("Y-m-d H:i:s",$v["time"]);
  185. $data[$k]["title"] = "{$v['nickname']}进行了登录";
  186. }
  187. return app('json')->success([
  188. 'list' => $data,
  189. 'pageCount' => $pageCount,
  190. 'pageSize' => $pageSize,
  191. 'page' => $post['page'],
  192. ]);
  193. }
  194. /**
  195. * 清理一个月之前的登录数据
  196. */
  197. public function delMonthLoginLog()
  198. {
  199. $time = strtotime('- 1 month');
  200. (new UserLogin)->where('time', '<', $time)->delete();
  201. return app('json')->success('清理成功');
  202. }
  203. }