Adminlog.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Db;
  5. use think\facade\Lang;
  6. /**
  7. *
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * 控制器
  12. */
  13. class Adminlog extends AdminControl
  14. {
  15. const EXPORT_SIZE = 5000;
  16. public function initialize()
  17. {
  18. parent::initialize(); // TODO: Change the autogenerated stub
  19. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/adminlog.lang.php');
  20. }
  21. /**
  22. * 日志列表
  23. *
  24. */
  25. public function loglist()
  26. {
  27. $condition = array();
  28. $time1 = '';
  29. $time2 = '';
  30. if (!empty(input('param.admin_name'))) {
  31. $condition[] = array('admin_name', '=', input('param.admin_name'));
  32. }
  33. if (!empty(input('param.time_from'))) {
  34. $time1 = strtotime(input('param.time_from'));
  35. }
  36. if (!empty(input('param.time_to'))) {
  37. $time2 = strtotime(input('param.time_to'));
  38. if ($time2 !== false)
  39. $time2 = $time2 + 86400;
  40. }
  41. if ($time1 && $time2) {
  42. $condition[] = array('adminlog_time', 'between', array($time1, $time2));
  43. } elseif ($time1) {
  44. $condition[] = array('adminlog_time', '>=', $time1);
  45. } elseif ($time2) {
  46. $condition[] = array('adminlog_time', '<=', $time2);
  47. }
  48. $adminlog_model = model('adminlog');
  49. $order = 'adminlog_id desc';
  50. $adminlog_list = $adminlog_model->getAdminlogList($condition, 10, $order);
  51. View::assign('adminlog_list', $adminlog_list);
  52. View::assign('show_page', $adminlog_model->page_info->render());
  53. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  54. $this->setAdminCurItem('loglist');
  55. return View::fetch();
  56. }
  57. /**
  58. * 删除日志
  59. *
  60. */
  61. public function list_del()
  62. {
  63. $adminlog_id = input('param.adminlog_id');
  64. $adminlog_id_array = ds_delete_param($adminlog_id);
  65. if ($adminlog_id_array == FALSE) {
  66. ds_json_encode('10001', lang('param_error'));
  67. }
  68. $condition = array();
  69. $condition[] = array('adminlog_id', 'in', $adminlog_id_array);
  70. $adminlog_model = model('adminlog');
  71. if (!$adminlog_model->delAdminlog($condition)) {
  72. $this->log(lang('ds_del') . lang('admin_log'), 0);
  73. ds_json_encode('10001', lang('ds_common_del_fail'));
  74. } else {
  75. $this->log(lang('ds_del') . lang('admin_log'), 1);
  76. ds_json_encode('10000', lang('ds_common_del_succ'));
  77. }
  78. }
  79. /**
  80. * 导出第一步
  81. */
  82. public function export_step1()
  83. {
  84. $time1 = '';
  85. $time2 = '';
  86. $condition = array();
  87. if (!empty(input('param.admin_name'))) {
  88. $condition[] = array('admin_name', '=', input('param.admin_name'));
  89. }
  90. if (!empty(input('param.time_from'))) {
  91. $time1 = strtotime(input('param.time_from'));
  92. }
  93. if (!empty(input('param.time_to'))) {
  94. $time2 = strtotime(input('param.time_to'));
  95. if ($time2 !== false)
  96. $time2 = $time2 + 86400;
  97. }
  98. if ($time1 && $time2) {
  99. $condition[] = array('createtime', 'between', array($time1, $time2));
  100. } elseif ($time1) {
  101. $condition[] = array('createtime', '>=', $time1);
  102. } elseif ($time2) {
  103. $condition[] = array('createtime', '<=', $time2);
  104. }
  105. if (!is_numeric(input('param.page'))) {
  106. $adminlog_model = model('adminlog');
  107. $count = $adminlog_model->getAdminlogCount($condition);
  108. $export_list = array();
  109. if ($count > self::EXPORT_SIZE) { //显示下载链接
  110. $page = ceil($count / self::EXPORT_SIZE);
  111. for ($i = 1; $i <= $page; $i++) {
  112. $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
  113. $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
  114. $export_list[$i] = $limit1 . ' ~ ' . $limit2;
  115. }
  116. View::assign('export_list', $export_list);
  117. return View::fetch('/public/excel');
  118. } else { //如果数量小,直接下载
  119. $data = Db::name('adminlog')->where($condition)->order('adminlog_id desc')->limit(self::EXPORT_SIZE)->select()->toArray();
  120. $this->createExcel($data);
  121. }
  122. } else { //下载
  123. $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
  124. $limit2 = self::EXPORT_SIZE;
  125. $data = Db::name('adminlog')->where($condition)->order('adminlog_id desc')->limit($limit1, $limit2)->select()->toArray();
  126. $this->createExcel($data);
  127. }
  128. }
  129. /**
  130. * 生成excel
  131. *
  132. * @param array $data
  133. */
  134. private function createExcel($data = array())
  135. {
  136. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/export.lang.php');
  137. $excel_obj = new \excel\Excel();
  138. $excel_data = array();
  139. //设置样式
  140. $excel_obj->setStyle(array(
  141. 'id' => 's_title', 'Font' => array('FontName' => lang('ds_song_typeface'), 'Size' => '12', 'Bold' => '1')
  142. ));
  143. //header
  144. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('admin_log_man'));
  145. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('admin_log_do'));
  146. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('admin_log_dotime'));
  147. $excel_data[0][] = array('styleid' => 's_title', 'data' => 'IP');
  148. foreach ((array)$data as $k => $v) {
  149. $tmp = array();
  150. $tmp[] = array('data' => $v['admin_name']);
  151. $tmp[] = array('data' => $v['adminlog_content']);
  152. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['adminlog_time']));
  153. $tmp[] = array('data' => $v['adminlog_ip']);
  154. $excel_data[] = $tmp;
  155. }
  156. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  157. $excel_obj->addArray($excel_data);
  158. $excel_obj->addWorksheet($excel_obj->charset(lang('admin_log'), CHARSET));
  159. $excel_obj->generateXML($excel_obj->charset(lang('admin_log'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
  160. }
  161. protected function getAdminItemList()
  162. {
  163. $menu_array = array(
  164. array(
  165. 'name' => 'loglist',
  166. 'text' => lang('admin_log'),
  167. 'url' => (string)url('Adminlog/loglist')
  168. )
  169. );
  170. return $menu_array;
  171. }
  172. }