kefu/application/admin/controller/Dialoguelog.php

222 lines
7.2 KiB
PHP

<?php
/**
* 历史会话记录
* Date 2019.07.03
* @author chf
*/
namespace app\admin\controller;
class Dialoguelog 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');
if (!empty($keyword)) {
$where['user_name|user_id'] = $keyword;
}
//获取总条数
$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('id 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()){
$cl_db = db('chat_log');
$param = input('param.');
$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)]);
}
}
}