Adminlog.php 7.4 KB

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