html:
<button class="btn btn-default" onclick="logToExcel(‘{:url(‘userLogToExcel‘,[‘day‘ => ‘3‘])}‘)">3天日志导出</button>
js:
function logToExcel(url){
$(‘.masking‘).show();
$.ajax({
url:url,
type:"post",
processData : false,
contentType : false ,
success:function (data) {
if(data.result){
var form=$("<form>");//定义一个form表单
form.attr("style","display:none");
form.attr("target","");
form.attr("method","post");
form.attr("action",data.data);//请求地址
$("body").append(form);//将表单放置在web中
//传递参数
var input1 = $("<input>");
input1.attr("type", "hidden");
input1.attr("name", "fileName");
input1.attr("value", data.fileName);
form.append(input1);
var input2 = $("<input>");
input2.attr("type", "hidden");
input2.attr("name", "fileDir");
input2.attr("value", data.fileDir);
form.append(input2);
form.submit().remove();//表单提交
$(‘.masking‘).hide();
}else{
alert(data.msg);
}
}
})
}
php:
/**
* 用户操作日志导出Excel表
* @internal param string $token 令牌
* @internal param int $day 导出日志时间
* @return \think\response\Json
* @reviewer
*/
public function userLogToExcel(){
set_time_limit(0);
$time = date(‘Y-m-d‘,time());
$fileName = ‘userlog-‘ . $time;
$day = Request::instance()->param(‘day‘);
//$day = input(‘day‘);
if ($day == ‘7‘){
$start_time = strtotime(‘-7 day‘);
}else{
$start_time = strtotime(‘-3 day‘);
}
$startTime = date(‘Y-m-d‘,$start_time);
$endTime = date(‘Y-m-d H:i:s‘,time());
$userID = ‘‘;
$firstRow = ‘‘;
$listRows = ‘‘;
$logUser = new UserLog();
$list = $logUser->getLogList($userID,$startTime,$endTime,$firstRow, $listRows);
$listID = array();
foreach ($list as $k => $v){
$listID[$k] = $v[‘user_id‘];
}
$listId = array_unique($listID);
$userDetail = new UserInfo();
$userData = $userDetail->getUserInfo($listId);
$userInfo = [];
foreach ($userData as $key => $value){
$userInfo[$value[‘user_id‘]] = $value;
}
$taskData = [];
foreach ($list as $k => $v){
$taskData[$k] = [
$k+1,
$userInfo[$v[‘user_id‘]][‘user_name‘],
$userInfo[$v[‘user_id‘]][‘real_name‘],
$v[‘log_info‘],
$v[‘log_ip‘],
$v[‘log_area‘],
$v[‘log_url‘],
$v[‘log_time‘],
];
}
$taskNewData = [‘序号‘, ‘用户名‘, ‘真实名‘, ‘操作信息‘, ‘ip地址‘, ‘登陆地‘, ‘操作url‘,‘操作时间‘];
array_unshift($taskData, $taskNewData);
$filePath = ROOT_PATH . ‘public‘ . DS . ‘static‘ . DS . ‘file‘ . DS . ‘tmp‘ . DS;
try{
Excel::generateExcel($fileName, $taskData, false, $filePath);
}catch (\Exception $e){
return json([‘result‘ => false, ‘msg‘ => ‘日志导出失败‘]);
}
return json([‘result‘ => true, ‘fileName‘ => $fileName.‘.xlsx‘, ‘fileDir‘ => $filePath, ‘data‘ => ‘/admin/log_user/sendFile‘]);
}
/**
* 将服务器文件发送到浏览器
* @internal param $fileDir 文件路径
* @internal param $fileName 文件名称
* @return Excel
*/
public function sendFile()
{
$fileDir = Request::instance()->param(‘fileDir‘);
$fileName = Request::instance()->param(‘fileName‘);
$fileName = iconv(‘gbk‘,‘utf-8‘, $fileName);
//检查文件是否存在
if (!file_exists($fileDir . $fileName)) {
echo "<meta http-equiv=‘refresh‘ content=‘3; url={$_SERVER[‘HTTP_REFERER‘]}‘>";
echo "文件不存在";
exit();
} else {
//打开文件
$file = fopen($fileDir . $fileName, "r");
//输入文件标签
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: " . filesize($fileDir . $fileName));
Header("Content-Disposition: attachment; filename=" . $fileName);
//输出文件内容
//读取文件内容并直接输出到浏览器
echo fread($file, filesize($fileDir . $fileName));
fclose($file);
exit();
}
}