379 lines
14 KiB
PHP
379 lines
14 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 历史会话记录
|
|
* Date 2019.07.03
|
|
* @author chf
|
|
*/
|
|
namespace app\admin\controller;
|
|
|
|
use Repository\ExcelRepository;
|
|
use Repository\ZipRepository;
|
|
|
|
class Dialoguelogs extends Base
|
|
{
|
|
/**
|
|
* 客服列表
|
|
*/
|
|
public function index()
|
|
{
|
|
$udb = db('users');
|
|
// 返回视图
|
|
if(!request()->isAjax()) return $this->fetch('dialogue_log/index');
|
|
|
|
// 分页请求
|
|
$param = input('get.');
|
|
$where = $this->where;
|
|
$keyword = input('search_input');
|
|
if (!empty($keyword)) {
|
|
$where['user_name|id'] = $keyword;
|
|
}
|
|
|
|
//获取总条数
|
|
$count = $udb->where($where)->count();
|
|
// $count=count($list);
|
|
//获取每页显示的条数
|
|
$limit = $param['limit'];
|
|
//获取当前页数
|
|
$page = $param['page'];
|
|
//计算出从那条开始查询
|
|
$start = ($page-1)*$limit;
|
|
$user_list = $udb
|
|
->where($where)
|
|
->limit($start, $limit)
|
|
->order('id desc')
|
|
->select();
|
|
$result = resultJson(0, '获取成功', $user_list);
|
|
$result['count'] = $count;
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 会话记录(结合layer的table插件)
|
|
* @return [type] [description]
|
|
*/
|
|
public function dialogueList()
|
|
{
|
|
$slog = db('service_log');
|
|
$kf_id = input('id');
|
|
// 返回视图
|
|
if(!request()->isAjax()) return $this->fetch('dialogue_log/dialogueList', ['id' => $kf_id]);
|
|
|
|
// 分页请求
|
|
$param = input('get.');
|
|
$kf_id = $param['id'];
|
|
if (!$kf_id) return resultJson(0, '参数错误');
|
|
$where['kf_id'] = $kf_id;
|
|
// 查询条件
|
|
$keyword = input('search_input');
|
|
$start_time = input('start_time');
|
|
$end_time = input('end_time');
|
|
if (!empty($keyword)) {
|
|
$where['user_name|user_id'] = $keyword;
|
|
}
|
|
if (!empty($start_time)) {
|
|
$where['start_time'] = ['egt', strtotime($start_time)];
|
|
}
|
|
if (!empty($end_time)) {
|
|
$where['end_time'] = ['elt', strtotime($end_time)+86400];
|
|
}
|
|
// var_dump($where);
|
|
//获取总条数
|
|
$count = $slog->where($where)->count();
|
|
// $count=count($list);
|
|
//获取每页显示的条数
|
|
$limit = $param['limit'];
|
|
//获取当前页数
|
|
$page = $param['page'];
|
|
//计算出从那条开始查询
|
|
$start = ($page-1)*$limit;
|
|
$user_list = $slog
|
|
->where($where)
|
|
->limit($start, $limit)
|
|
->order('start_time desc')
|
|
->select();
|
|
// 对列表进行处理
|
|
$result = resultJson(0, '获取成功', $user_list);
|
|
$result['count'] = $count;
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 删除会话记录
|
|
* @return [type] [description]
|
|
*/
|
|
public function delDialogue()
|
|
{
|
|
if (!request()->isAjax()) return false;
|
|
$slog = db('service_log');
|
|
$kf_id = session('l_user_id');
|
|
$id = input('post.id');
|
|
if (!$id) return resultJson(0, '参数错误');
|
|
// 数据出库
|
|
$del = $slog->where(['kf_id' => $kf_id, 'id' => $id])->delete();
|
|
if (!$del) return resultJson(0, '删除失败');
|
|
return resultJson(1, '删除成功');
|
|
}
|
|
|
|
/**
|
|
* 对话室(离线)
|
|
* @return [type] [description]
|
|
*/
|
|
public function offLineDialogueRoom()
|
|
{
|
|
$pay_db = db('payment');
|
|
$slog_db = db('service_log');
|
|
$cl_db = db('chat_log');
|
|
$kf_id = input('kf_id');
|
|
$id = input('get.id');
|
|
if (!$id || !$kf_id) {
|
|
echo "<script>alert('参数错误'); window.history.go(-1);</script>";
|
|
return;
|
|
}
|
|
// 客服信息
|
|
$userInfo = db('users')
|
|
->alias('a')
|
|
->field('a.*,b.name')
|
|
->join('ws_groups b','b.id = a.group_id')
|
|
->where('a.id', $kf_id)
|
|
->find();
|
|
|
|
//获取该客服拥有的支付方式
|
|
$type = db('payment_type')->where('status',1)->select();
|
|
$payment_information = $pay_db
|
|
->alias('a')
|
|
->join('payment_type w','a.payment_type = w.id')
|
|
->where(['is_use'=>1,'a.status'=>1,'kf_id'=>$kf_id])
|
|
->select();
|
|
$payment_img = $pay_db
|
|
->alias('a')
|
|
->join('payment_type w','a.payment_type = w.id')
|
|
->where(['is_use'=>1,'a.status'=>1,'kf_id'=>$kf_id])
|
|
->find();
|
|
|
|
// 用户信息
|
|
$user = $slog_db->where(['id' => $id, 'kf_id' => $kf_id])->find();
|
|
|
|
// 获取 客服有效充值方式
|
|
$pay_arr = $pay_db->alias('p')
|
|
->join('payment_type pt', 'p.payment_type = pt.id')
|
|
->where(['p.kf_id' => $kf_id, 'p.status' => 1, 'p.is_use' => 1])
|
|
->column('pt.type_name', 'pt.id');
|
|
|
|
// 获取对话数据
|
|
$chat_log = $cl_db
|
|
->where("from_id = 'KF{$kf_id}' and to_id = {$user['user_id']} or from_id = {$user['user_id']} and to_id = 'KF{$kf_id}'")
|
|
->order('time_line asc')
|
|
->limit(15)
|
|
->select();
|
|
// dump($chat_log);
|
|
$now_time = strtotime(date('Y-m-d', strtotime('-3 day')));
|
|
$user_status = $user['start_time'] < $now_time ? false : true;
|
|
|
|
$this->assign([
|
|
'word' => db('words')->where('kf_id', $kf_id)->select(),
|
|
'uinfo'=> $userInfo,
|
|
'groups' => db('groups')->where('status', 1)->select(),
|
|
'status' => db('kf_config')->where('id', 1)->find(),
|
|
'token' => session('kf_token'),
|
|
'type' => $type,
|
|
'payment_information' => $payment_information,
|
|
'payment_img' => $payment_img,
|
|
'pay_types' => $pay_arr,
|
|
'user' => $user,
|
|
'amount' => $userInfo['remaining_amount'],
|
|
'chat_log' => $chat_log,
|
|
'user_status' => $user_status,
|
|
'kf_id' => $kf_id,
|
|
]);
|
|
return $this->fetch('dialogue_log/offLineDialogueRoom');
|
|
}
|
|
|
|
/**
|
|
* 获取离线消息记录
|
|
* @return [type] [description]
|
|
*/
|
|
public function getOutLineMsgLog()
|
|
{
|
|
if(request()->isAjax()){
|
|
|
|
$param = input('param.');
|
|
$cl_db = db($this->getTableName('chat_log', $param['uid']));
|
|
$limit = 15; // 一次显示15 条聊天记录
|
|
$offset = ($param['page'] - 1) * $limit;
|
|
|
|
$logs = $cl_db
|
|
->where(function($query) use($param){
|
|
$query->where('from_id', $param['uid'])->where('to_id', 'KF' . $param['kf_id']);
|
|
})->whereOr(function($query) use($param){
|
|
$query->where('from_id', 'KF' . $param['kf_id'])->where('to_id', $param['uid']);
|
|
})->limit($offset, $limit)->order('id', 'desc')->select();
|
|
|
|
$total = $cl_db
|
|
->where(function($query) use($param){
|
|
$query->where('from_id', $param['uid'])->where('to_id', 'KF' . $param['kf_id']);
|
|
})->whereOr(function($query) use($param){
|
|
$query->where('from_id', 'KF' . $param['kf_id'])->where('to_id', $param['uid']);
|
|
})->count();
|
|
|
|
foreach($logs as $key=>$vo){
|
|
$logs[$key]['type'] = 'user';
|
|
$logs[$key]['time_line'] = date('Y-m-d H:i:s', $vo['time_line']);
|
|
|
|
if($vo['from_id'] == 'KF' . $param['kf_id']){
|
|
$logs[$key]['type'] = 'mine';
|
|
}
|
|
}
|
|
|
|
return json(['code' => 1, 'data' => $logs, 'msg' => intval($param['page']), 'total' => ceil($total / $limit)]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 下载平台的客服聊天记录
|
|
* @return array|false|string
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function downServiceLogOld()
|
|
{
|
|
$gdb = db('groups');
|
|
$udb = db('users');
|
|
$group_id = input('id');
|
|
$date = input('date');
|
|
if (!$group_id || !$date) return resultJson(3, '参数错误');
|
|
$group_info = $gdb->where(['id' => $group_id, 'status' => 1])->field('name')->find();
|
|
if (!$group_info) return resultJson(2, '平台未找到');
|
|
$users = $udb->where(['group_id' => $group_id, 'status' => 1])->field('id,user_name')->select();
|
|
if (!$users) return 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 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 resultJson(0, '下载文件失败');
|
|
}
|
|
$down_url = 'http://'.$_SERVER['HTTP_HOST']."/uploads/export_service_log/{$group_info['name']}/{$date}.zip";
|
|
return resultJson(1, '下载成功', ['url' => $down_url]);
|
|
}catch (Exception $e){
|
|
return resultJson(0, '系统错误', $e->getMessage());
|
|
}
|
|
}
|
|
public function downServiceLog()
|
|
{
|
|
$gdb = db('groups');
|
|
$udb = db('users');
|
|
$group_id = input('id');
|
|
$kf_id = input('kf_id');
|
|
$date_start = input('date_start');
|
|
$date_end = input('date_end');
|
|
if (!$group_id || !$date_start || !$date_end) {
|
|
echo '参数错误';
|
|
exit;
|
|
}
|
|
try{
|
|
$group_info = $gdb->where(['id' => $group_id, 'status' => 1])->field('name')->find();
|
|
if (!$group_info) {
|
|
echo '平台未找到';
|
|
exit;
|
|
}
|
|
$where = [
|
|
'group_id' => $group_id,
|
|
'status' => 1
|
|
];
|
|
if ($kf_id) {
|
|
$where['id'] = $kf_id;
|
|
}
|
|
$users = $udb->where($where)->field('id,user_name')->select();
|
|
if (!$users) {
|
|
echo '暂无客服';
|
|
exit;
|
|
}
|
|
$date = $date_start . '-' . $date_end;
|
|
// 处理平台下客服的聊天记录
|
|
foreach($users as $k => $v) {
|
|
$day_up = strtotime($date_start);
|
|
$day_down = strtotime($date_end) + (60 * 60 * 24);
|
|
$chat_log = [];
|
|
for($i=0; $i < 5; $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)")
|
|
->order('time_line asc')->select();
|
|
if (!$log_data) continue;
|
|
foreach ($log_data as &$vv) {
|
|
$vv['time_line'] = date('Y-m-d H:i:s', $vv['time_line']);
|
|
$chat_log[] = $vv;
|
|
}
|
|
}
|
|
|
|
$filePath = ROOT_PATH."public/uploads/export_service_log/{$group_info['name']}/{$date}/";
|
|
if(!is_dir($filePath)) {
|
|
if(!mkdir($filePath, 0775, true)) {
|
|
echo '创建文件夹失败';
|
|
exit;
|
|
}
|
|
}
|
|
$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";
|
|
// 重复压缩,会自动覆盖
|
|
$res = ZipRepository::zip($zip_path, $zip_to_path);
|
|
if(!$res){
|
|
echo '下载文件失败';
|
|
exit;
|
|
}
|
|
$down_url = 'http://'.$_SERVER['HTTP_HOST']."/uploads/export_service_log/{$group_info['name']}/{$date}.zip";
|
|
echo '下载成功, url='.$down_url;
|
|
exit;
|
|
}catch (Exception $e){
|
|
var_dump($e->getMessage());
|
|
}
|
|
}
|
|
|
|
} |