83 lines
3.2 KiB
PHP
83 lines
3.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 测试
|
|
*/
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use Repository\ExcelRepository;
|
|
use Repository\ZipRepository;
|
|
use think\Controller;
|
|
class Test extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
echo "hello world!";
|
|
}
|
|
/**
|
|
* 下载平台的客服聊天记录
|
|
* @return array|false|string
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function downServiceLog()
|
|
{
|
|
$gdb = db('groups');
|
|
$udb = db('users');
|
|
$group_id = input('id');
|
|
$date = input('date');
|
|
if (!$group_id || !$date) return json(resultJson(3, '参数错误'));
|
|
$group_info = $gdb->where(['id' => $group_id, 'status' => 1])->field('name')->find();
|
|
if (!$group_info) return json(resultJson(2, '平台未找到'));
|
|
$users = $udb->where(['group_id' => $group_id, 'status' => 1])->field('id,user_name')->select();
|
|
if (!$users) return json(resultJson(2, '暂无客服'));
|
|
// 处理平台下客服的聊天记录
|
|
foreach($users as $k => $v) {
|
|
$day_up = strtotime($date);
|
|
$day_down = strtotime($date) + (60 * 60 * 24);
|
|
$chat_log = [];
|
|
for($i=0; $i < config('chat_table_num'); $i++) {
|
|
$log_data = db('chat_log_'.$i)->where("(from_id = 'KF{$v['id']}' or to_id = 'KF{$v['id']}') and (time_line >= {$day_up} and time_line < $day_down)")->select();
|
|
if (!$log_data) continue;
|
|
foreach ($log_data as $vv) {
|
|
$chat_log[] = $vv;
|
|
}
|
|
}
|
|
|
|
$filePath = ROOT_PATH."public/uploads/export_service_log/{$group_info['name']}/{$date}/";
|
|
if(!is_dir($filePath)) {
|
|
if(!mkdir($filePath, 0775, true)) {
|
|
return json(resultJson(3, '创建文件夹失败'));
|
|
}
|
|
}
|
|
$title = $v['user_name'].'-'.$date;
|
|
$xlsCell = array(
|
|
array('from_id','发送方id'),
|
|
array('from_name','发送方名字'),
|
|
array('to_id','接收方id'),
|
|
array('to_name','接收方名字'),
|
|
array('content','发送内容'),
|
|
array('time_line','发送时间'),
|
|
array('is_offline_msg','是否离线消息'),
|
|
array('msg_id','消息id'),
|
|
);
|
|
ExcelRepository::exportExcel($title, $xlsCell, $chat_log, $filePath);
|
|
}
|
|
$zip_path = ROOT_PATH."public/uploads/export_service_log/{$group_info['name']}/{$date}/";
|
|
$zip_to_path = ROOT_PATH."public/uploads/export_service_log/{$group_info['name']}/{$date}.zip";
|
|
try{
|
|
// 重复压缩,会自动覆盖
|
|
$res = ZipRepository::zip($zip_path, $zip_to_path);
|
|
if(!$res){
|
|
return json(resultJson(0, '下载文件失败'));
|
|
}
|
|
$down_url = 'http://'.$_SERVER['HTTP_HOST']."/uploads/export_service_log/{$group_info['name']}/{$date}.zip";
|
|
return json(resultJson(1, '下载成功', ['url' => $down_url]));
|
|
}catch (Exception $e){
|
|
return json(resultJson(0, '系统错误', $e->getMessage()));
|
|
}
|
|
}
|
|
}
|