1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 商业智能-日志分析
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\bi\controller;
  8. use app\admin\controller\ApiCommon;
  9. use think\Db;
  10. use think\Hook;
  11. use think\Request;
  12. class Log extends ApiCommon
  13. {
  14. /**
  15. * 用于判断权限
  16. * @permission 无限制
  17. * @allow 登录用户可访问
  18. * @other 其他根据系统设置
  19. **/
  20. public function _initialize()
  21. {
  22. $action = [
  23. 'permission'=>[''],
  24. 'allow'=>['statistics','excelexport']
  25. ];
  26. Hook::listen('check_auth',$action);
  27. $request = Request::instance();
  28. $a = strtolower($request->action());
  29. if (!in_array($a, $action['permission'])) {
  30. parent::_initialize();
  31. }
  32. if (!checkPerByAction('bi', 'oa', 'read')) {
  33. header('Content-Type:application/json; charset=utf-8');
  34. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  35. }
  36. }
  37. /**
  38. * 日志统计列表
  39. * @return
  40. */
  41. public function statistics()
  42. {
  43. $param = $this->param;
  44. if ($param['type']) {
  45. $timeArr = getTimeByType($param['type']);
  46. $param['start_time'] = $timeArr[0];
  47. $param['end_time'] = $timeArr[1];
  48. } else {
  49. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  50. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  51. }
  52. $loglModel = new \app\bi\model\Log();
  53. $list = $loglModel->getStatistics($param) ? : [];
  54. return resultArray(['data'=>$list]);
  55. }
  56. /**
  57. * 统计导出
  58. * @author Michael_xu
  59. * @param
  60. * @return
  61. */
  62. public function excelExport()
  63. {
  64. $param = $this->param;
  65. $excelModel = new \app\admin\model\Excel();
  66. // 导出的字段列表
  67. $field_list = [
  68. '0' => ['field' => 'realname','name' => '员工'],
  69. '1' => ['field' => 'count','name' => '填写数'],
  70. '2' => ['field' => 'unReadCont','name' => '接收人未读数'],
  71. '3' => ['field' => 'unCommentCount','name' => '未评论数'],
  72. '4' => ['field' => 'commentCount','name' => '已评论数'],
  73. ];
  74. // 文件名
  75. $file_name = '5kcrm_log_'.date('Ymd');
  76. $excelModel->dataExportCsv($file_name, $field_list, function($list) use ($param){
  77. $loglModel = new \app\bi\model\Log();
  78. if ($param['type']) {
  79. $timeArr = getTimeByType($param['type']);
  80. $param['start_time'] = $timeArr[0];
  81. $param['end_time'] = $timeArr[1];
  82. }
  83. $list = $loglModel->getStatistics($param);
  84. return $list ? : [];
  85. });
  86. }
  87. }