Index.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin\block\logtj;
  10. use fecadmin\FecadminbaseBlock;
  11. use fecadmin\models\AdminUser;
  12. use fecadmin\models\AdminLog;
  13. use fec\helpers\CUrl;
  14. use fec\helpers\CDate;
  15. use fec\helpers\CRequest;
  16. /**
  17. * @author Terry Zhao <2358269014@qq.com>
  18. * @since 1.0
  19. */
  20. class Index extends FecadminbaseBlock{
  21. public $_obj ;
  22. public $_paramKey = 'id';
  23. public $_defaultDirection = 'asc';
  24. # 初始化参数
  25. public function initParam(){
  26. # 定义编辑和删除的URL
  27. $this->_editUrl = ''; #CUrl::getUrl("fecadmin/log/indexedit");
  28. $this->_deleteUrl = ''; #CUrl::getUrl("fecadmin/account/indexdelete");
  29. $this->_obj = new AdminLog;
  30. $this->_paramKey = 'id';
  31. /*
  32. # 自定义参数如下:
  33. #排序默认为主键倒序
  34. $this->_orderField = 'created_at';
  35. $this->_sortDirection = 'asc';
  36. # 主键默认为id
  37. $this->_paramKey = 'id';
  38. #第一次打开默认为第一页,一页显示50个
  39. $this->_pageNum = 1;
  40. $this->_numPerPage;
  41. */
  42. parent::initParam();
  43. $this->_param['created_at_lt'] || $this->_param['created_at_lt'] = date('Y-m-d',strtotime(CDate::getCurrentDate().' +1 day '));
  44. $this->_param['created_at_gte'] || $this->_param['created_at_gte'] = date('Y-m-d',strtotime($this->_param['created_at_gte'].' -1 month '));
  45. }
  46. public function getLastData(){
  47. # 返回数据的函数
  48. # 隐藏部分
  49. $pagerForm = $this->getPagerForm();
  50. # 搜索部分
  51. $searchBar = $this->getSearchBar();
  52. # 编辑 删除 按钮部分
  53. $editBar = $this->getEditBar();
  54. # 表头部分
  55. $thead = $this->getTableThead();
  56. # 表内容部分
  57. $tbody = $this->getTableTbody();
  58. # 分页部分
  59. $toolBar = $this->getToolBar($this->_param['numCount'],$this->_param['pageNum'],$this->_param['numPerPage']);
  60. return [
  61. 'pagerForm' => $pagerForm,
  62. 'searchBar' => $searchBar,
  63. 'editBar' => $editBar,
  64. 'thead' => $thead,
  65. 'tbody' => $tbody,
  66. 'toolBar' => $toolBar,
  67. ];
  68. }
  69. # 定义搜索部分字段格式
  70. public function getSearchArr(){
  71. $data = [
  72. [ # 字符串类型
  73. 'type'=>'inputtext',
  74. 'title'=>'账号',
  75. 'name'=>'account' ,
  76. 'columns_type' =>'string'
  77. ],
  78. [ # 字符串类型
  79. 'type'=>'inputtext',
  80. 'title'=>'操作人',
  81. 'name'=>'person' ,
  82. 'columns_type' =>'string'
  83. ],
  84. [ # selecit的Int 类型
  85. 'type'=>'select',
  86. 'title'=>'类型',
  87. 'name'=>'tj_type',
  88. 'columns_type' =>'int', # int使用标准匹配, string使用模糊查询
  89. 'value'=> [ # select 类型的值
  90. '账号登录'=>'账号登录',
  91. ''=>'全部操作',
  92. ],
  93. ],
  94. [ # 时间区间类型搜索
  95. 'type'=>'inputdatefilter',
  96. 'name'=> 'created_at',
  97. 'columns_type' =>'datetime',
  98. 'value'=>[
  99. 'gte'=>'LOG时间开始',
  100. 'lt' =>'LOG时间结束',
  101. ]
  102. ],
  103. ];
  104. return $data;
  105. }
  106. # 定义表格显示部分的配置
  107. public function getTableFieldArr(){
  108. $table_th_bar = [
  109. [
  110. 'orderField' => 'id',
  111. 'label' => 'ID',
  112. 'width' => '70',
  113. 'align' => 'center',
  114. ],
  115. [
  116. 'orderField' => 'account',
  117. 'label' => '账号',
  118. 'width' => '70',
  119. 'align' => 'center',
  120. ],
  121. [
  122. 'orderField' => 'person',
  123. 'label' => '操作人',
  124. 'width' => '70',
  125. 'align' => 'left',
  126. ],
  127. [
  128. 'orderField' => 'menu',
  129. 'label' => '操作菜单',
  130. 'width' => '70',
  131. 'align' => 'left',
  132. ],
  133. [
  134. 'orderField' => 'click_count',
  135. 'label' => '次数',
  136. 'width' => '220',
  137. 'align' => 'left',
  138. ],
  139. ];
  140. return $table_th_bar ;
  141. }
  142. # 得到表格的内容部分
  143. public function getTableTbody(){
  144. $obj = $this->_obj;
  145. //$searchArr = $this->getSearchArr();
  146. //$query = $obj::find();
  147. //if(is_array($searchArr) && !empty($searchArr)){
  148. // $this->initDateWhere($query,$searchArr);
  149. //}
  150. //$this->_param['numCount'] = $query->count();
  151. //$query->limit = $this->_param['numPerPage'];
  152. # 偏离值
  153. //$query->offset = ($this->_param['pageNum'] -1)*$this->_param['numPerPage'] ;
  154. //$query->orderBy([$this->_param['orderField']=> (($this->_param['orderDirection'] == 'desc') ? SORT_DESC : SORT_ASC)]);
  155. //$data = $query->all();
  156. $offset = ($this->_param['pageNum'] -1)*$this->_param['numPerPage'] ;
  157. $limit = $this->_param['numPerPage'];
  158. $limit = " limit $offset , $limit ";
  159. $group = " account ";
  160. $account = CRequest::param('account');
  161. $person = CRequest::param('person');
  162. $tj_type = CRequest::param('tj_type');
  163. $created_at_lt = $this->_param['created_at_lt'];
  164. $created_at_gte = $this->_param['created_at_gte'];
  165. $where = [];
  166. if($account)
  167. $where []= " account = '$account' ";
  168. if($person)
  169. $where []= " person = '$person' ";
  170. if($tj_type == '账号登录'){
  171. $where []= " menu = '账号登录' ";
  172. $group .= " ,menu ";
  173. }
  174. if($created_at_lt)
  175. $where []= " created_at < '$created_at_lt' ";
  176. if($created_at_gte)
  177. $where []= " created_at >= '$created_at_gte' ";
  178. if(!empty($where)){
  179. $where = ' where '.implode(' and ',$where);
  180. }else{
  181. $where = '';
  182. }
  183. $table = $obj::tableName();
  184. $db = \Yii::$app->db;
  185. # 得到 总数。
  186. $sql = "select count(*) as count from (select account,person,menu ,count(*) as click_count
  187. from $table $where group by $group ) as t ";
  188. $data_count = $db->createCommand($sql,[])->queryOne();
  189. $this->_param['numCount'] = $data_count['count'];
  190. # 得到数据
  191. $sql = "select account,person,menu ,count(*) as click_count
  192. from $table $where group by $group order by click_count DESC $limit ";
  193. $data = $db->createCommand($sql,[])->queryAll();
  194. return $this->getTableTbodyHtml($data);
  195. }
  196. # table 内容部分
  197. public function getTableTbodyHtml($data){
  198. $fileds = $this->getTableFieldArr();
  199. $str .= '';
  200. $csrfString = \fec\helpers\CRequest::getCsrfString();
  201. foreach($data as $one){
  202. $str .= '<tr target="sid_user" rel="'.$one[$this->_paramKey].'">';
  203. $str .= '<td><input name="'.$this->_paramKey.'s" value="'.$one[$this->_paramKey].'" type="checkbox"></td>';
  204. foreach($fileds as $field){
  205. $orderField = $field['orderField'];
  206. $display = $field['display'];
  207. $val = $one[$orderField];
  208. $originVal = $one[$orderField];
  209. if($val){
  210. if(isset($field['display']) && !empty($field['display'])){
  211. $display = $field['display'];
  212. $val = $display[$val] ? $display[$val] : $val;
  213. }
  214. if(isset($field['convert']) && !empty($field['convert'])){
  215. $convert = $field['convert'];
  216. foreach($convert as $origin =>$to){
  217. if(strstr($origin,'date')){
  218. if($to == 'date'){
  219. $val = date('Y-m-d',strtotime($val));
  220. }else if($to == 'datetime'){
  221. $val = date('Y-m-d H:i:s',strtotime($val));
  222. }else if($to == 'int'){
  223. $val = strtotime($val);
  224. }
  225. }else if($origin == 'int'){
  226. if($to == 'date'){
  227. $val = date('Y-m-d',$val);
  228. }else if($to == 'datetime'){
  229. $val = date('Y-m-d H:i:s',$val);
  230. }else if($to == 'int'){
  231. $val = $val;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. $str .= '<td><span title='.$originVal.'>'.$val.'</span></td>';
  238. }
  239. $str .= '</tr>';
  240. }
  241. return $str ;
  242. }
  243. # table 表 标题 1
  244. public function getTableTheadHtml($table_th_bar){
  245. $table_th_bar = $this->getTableTheadArrInit($table_th_bar);
  246. $this->_param['orderField'] = $this->_param['orderField'] ? $this->_param['orderField'] : $this->_paramKey;
  247. $this->_param['orderDirection'] = $this->_param['orderDirection'] ? $this->_param['orderDirection'] : $this->_defaultDirection;
  248. foreach($table_th_bar as $k => $field){
  249. if($field['orderField'] == $this->_param['orderField']){
  250. $table_th_bar[$k]['class'] = $this->_param['orderDirection'];
  251. }
  252. }
  253. $str = '<thead><tr>';
  254. $str .= '<th width="22"><input type="checkbox" group="'.$this->_paramKey.'s" class="checkboxCtrl"></th>';
  255. foreach($table_th_bar as $b){
  256. $width = $b['width'];
  257. $label = $b['label'];
  258. $orderField = $b['orderField'];
  259. $class = isset($b['class']) ? $b['class'] : '';
  260. $align = isset($b['align']) ? 'align="'.$b['align'].'"' : '';
  261. $str .= '<th width="'.$width.'" '.$align.' orderField="'.$orderField.'" class="'.$class.'">'.$label.'</th>';
  262. }
  263. $str .= '</tr></thead>';
  264. return $str;
  265. }
  266. }