Adminlog.php 7.2 KB

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