isAjax()) return $this->fetch('dialogue/dialogueList'); // 分页请求 $param = input('get.'); // 查询条件 $where['kf_id'] = $kf_id; $where['start_time'] = !empty($param['start_time']) ? ['egt', strtotime($param['start_time'])] : ''; $where['end_time'] = !empty($param['end_time']) ? ['elt', strtotime($param['end_time'])+86400] : ''; $where['user_name|user_id'] = is_string($param['search_input']) ? $param['search_input'] : ''; $where = array_filter($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('id desc') ->select(); // 对列表进行处理 foreach ($user_list as &$vo) { // 在线状态 $vo['line_status'] = $vo['end_time'] <= 0 ? '在线' : '离线'; // 时间戳转换 $vo['start_time'] = date("Y-m-d H:i:s", $vo['start_time']); $vo['end_time'] = $vo['end_time'] > 0 ? date("Y-m-d H:i:s", $vo['end_time']) : 0; } $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, '删除失败'); LogRepository::userLogWrite('登录成功', $id); 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 = session('l_user_id'); $id = input('get.id'); if (!$id) { echo ""; 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, ]); return $this->fetch('dialogue/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' . session('l_user_id')); })->whereOr(function($query) use($param){ $query->where('from_id', 'KF' . session('l_user_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' . session('l_user_id')); })->whereOr(function($query) use($param){ $query->where('from_id', 'KF' . session('l_user_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' . session('l_user_id')){ $logs[$key]['type'] = 'mine'; } } return json(['code' => 1, 'data' => $logs, 'msg' => intval($param['page']), 'total' => ceil($total / $limit)]); } } /** * 离线消息记录 * @return [type] [description] */ // public function outLineMsgLog() // { // $cl_db = db('chat_log'); // $kf_id = session('l_user_id'); // $page = input('get.page'); // $uid = input('get.uid'); // //计算出从那条开始查询 // $limit = 15; // $start = ($page-1)*$limit; // $msg_log_list = $cl_db // ->where("from_id = 'KF{$kf_id}' and to_id = {$uid} or from_id = {$uid} and to_id = 'KF{$kf_id}'") // ->limit($start, $limit) // ->order('time_line asc') // ->select(); // return resultJson(1, '获取成功', $msg_log_list); // } /** * 发送离线消息 * @return [type] [description] */ public function sendOffLineDialogue() { if (!request()->isAjax()) return false; $clog_db = db('chat_log'); $slog_db = db('service_log'); $user_db = db('users'); $kf_id = session('l_user_id'); $uid = input('post.user_id'); $content = input('post.content'); if (!$uid || empty($content)) return resultJson(0, '参数错误'); // 用户信息 $user = $slog_db->where(['user_id' => $uid, 'kf_id' => $kf_id])->find(); // 客服信息 $kf = $user_db->find($kf_id); if (!$user) return resultJson(0, '用户不存在'); // 消息id $msg_id = Uuid::uuid4(); // 数据入库 $save_data = [ 'from_name' => $kf['user_name'], 'from_id' => 'KF'.$kf_id, 'from_avatar' => $kf['user_avatar'], 'to_id' => $uid, 'to_name' => $user['user_name'], 'content' => $content, 'time_line' => time(), 'is_offline_msg' => 1, 'msg_id' => $msg_id->toString(), ]; $save = $clog_db->insert($save_data); // 消息远程推送 if(strpos($content,"img[") === 0){ $imgUrl = str_replace("]","",str_replace("img[","",$content)); $imgSize = getimagesize(ROOT_PATH.'public'.$imgUrl); } $msg_type = checkMsgType($content); $send_data = [ 'type' => 'single', 'target' => $uid, 'msg_type' => $msg_type, 'data' => [ 'id'=> $kf_id, 'name'=> $kf['user_name'], 'avatar'=> $kf['user_avatar'], 'img_type' => "", 'img_width' => empty($imgSize[0]) ? "" : $imgSize[0], 'img_height' => empty($imgSize[1]) ? "" : $imgSize[1], 'content'=> $content, 'data'=> '', 'time' => date('Y-m-d H:i:s'), 'msg_id' => $msg_id->toString(), ] ]; $call = self::callRemoteApi('/api/Customer/PushOfflineMsg', $uid, $send_data); // var_dump($send_data['data'], $call); if (!$call || $call['State'] != 1) return resultJson(0, '推送失败'); return resultJson(1, '成功'); } /** * 远程游戏api调用 * @param [type] $api [description] * @param [type] $token [description] * @param array $msg [description] * @return [type] [description] */ public static function callRemoteApi($api,$token,$msg = []) { $configFile['app_id'] = config('app_id'); $configFile['game_host'] = config('game_host'); $data = ['Param'=>['Token'=>$token]]; if(!empty($msg)){ $itemData = [ 'code' => 200, 'msg' => 'ok', 'message_type' => $msg['msg_type'], 'data' => $msg['data'], ]; $data = [ 'Param'=> [ "SnId" => $token, "Plateform" => 1, "Data" => json_encode($itemData) ] ]; } $data = json_encode($data); $AppId = $configFile['app_id']; $msectime = self::getMsectime(); $sign = md5($AppId.';'.$api.';'.$data.';'.$msectime); $url = $configFile['game_host'].$api.'?ts='.$msectime.'&sign='.$sign; // var_dump($url, $configFile); $res = self::curl_post($url,$data); // var_dump($res); $res = json_decode($res,true); return $res; } /** * 获取POST请求 * @param $url * @param array $params * @param $timeout * @return mixed */ public static function curl_post($url, $data, $timeout = 5) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json; charset=utf-8", "Content-Length: " . strlen($data)) ); $res = curl_exec($ch); curl_close($ch); // var_dump($url); return $res; } /** * 获取毫秒级时间戳 * @return [type] [description] */ public static function getMsectime() { return (float)sprintf('%.0f', microtime(true) * 1000); } /** * 选择账号 * @return [type] [description] */ public function selectAccount() { $ptdb = db('payment_type'); $pdb = db('payment'); $kf_id = session('l_user_id'); // 设置选用 if (request()->isPost()) { $id = input('post.id'); $a_type = input('post.a_type'); $type = input('post.type'); if (!$id) return resultJson(0, '参数错误'); switch ($type) { // 启用 case 1: // 清除选用 $pdb->where(['kf_id' => $kf_id, 'status' => 1, 'payment_type' => $a_type, 'is_use' => 1])->update(['is_use' => 0]); $save = $pdb->where(['kf_id' => $kf_id, 'status' => 1, 'id' => $id])->update(['is_use' => 1]); break; // 禁用 case 2: // 清除选用 $save = $pdb->where(['kf_id' => $kf_id, 'status' => 1, 'id' => $id])->update(['is_use' => 0]); break; } if (!$save) return resultJson(0, '失败'); return resultJson(1, '成功'); } // 默认支付 $default = $pdb->where(['kf_id' => $kf_id, 'status' => 1, 'is_use' => 1])->find(); // 获取 客服有效充值方式 $pay_arr = $ptdb->where('status', 1)->select(); // 获取支付类型下的账号 foreach ($pay_arr as $k => $vo) { $account_list = $pdb->where(['kf_id' => $kf_id, 'status' => 1, 'payment_type' => $vo['id']])->select(); if (count($account_list) < 1) { unset($pay_arr[$k]); continue; } // 默认支付 // $default_type = $pdb->where(['kf_id' => $kf_id, 'status' => 1, 'payment_type' => $vo['id'], 'is_use' => 1])->find(); // 账号列表 $pay_arr[$k]['account_list'] = $account_list; // $pay_arr[$k]['show'] = $default_type['payment_type'] == $vo['id'] ? true : false; // if ($default['payment_type'] == $vo['id']) { // $default['payment_name'] = $vo['type_name']; // } } // dump($pay_arr); $this->assign([ 'pay_list' => $pay_arr, 'default' => $default, ]); return $this->fetch('index/son/selectAccount'); } /** * 黑名单管理 */ public function blacklist() { $udb = db('users'); $gdb = db('groups'); $kf_id = session('l_user_id'); if (request()->isAjax()) { $kf_info = $udb->where('id', $kf_id)->find(); $redis = new Redis(); $origin = $redis::instance()->hget("blacklist_{$kf_info['group_id']}", $kf_id); $list = !$origin ? [] : json_decode($origin, true); $group_info = $gdb->where('id', $kf_info['group_id'])->find(); $data = []; foreach($list as $vo) { $vo['group'] = $kf_info['group_id']; $vo['group_name'] = $group_info['name']; $data[] = $vo; } return resultJson(0, '获取成功', $data); } return $this->fetch('dialogue/blacklist'); } /** * 添加黑名单 */ public function addBlacklist() { $udb = db('users'); $kf_id = session('l_user_id'); if (request()->isPost()) { $id = input('post.id'); if (!$id) return resultJson(0, '参数错误'); $kf_info = $udb->where('id', $kf_id)->find(); if (!isset($kf_info['group_id'])) return resultJson(2, '数据错误'); $redis = new Redis(); $origin = $redis::instance()->hget("blacklist_{$kf_info['group_id']}", $kf_id); if ($origin) { $origin_arr = json_decode($origin, true); if (in_array($id, $origin_arr)) return resultJson(2, 'id已存在'); $origin_arr[$id] = ['id' => $id, 'date' => date('Y-m-d H:i:s')]; } $data = isset($origin_arr) ? $origin_arr : [$id => ['id' => $id, 'date' => date('Y-m-d H:i:s')]]; $save = $redis::instance()->hset("blacklist_{$kf_info['group_id']}", $kf_id, json_encode($data)); // if ($save !== 0 && $save !== 1) return resultJson(0, '失败'); LogRepository::userLogWrite('黑名单管理', '客服['.$kf_info['user_name'].']将用户['.$id.']添加至黑名单成功'); return resultJson(1, '成功'); } return $this->fetch('dialogue/addBlacklist'); } /** * 删除黑名单 */ public function delBlacklist() { $udb = db('users'); $kf_id = session('l_user_id'); if (request()->isPost()) { $id = preg_replace('# #', '', input('post.id')); if (!$id) return resultJson(0, '参数错误'); $kf_info = $udb->where('id', $kf_id)->find(); if (!isset($kf_info['group_id'])) return resultJson(2, '数据错误'); $redis = new Redis(); $origin = $redis::instance()->hget("blacklist_{$kf_info['group_id']}", $kf_id); if (!$origin) return resultJson(2, '数据错误'); $origin_arr = json_decode($origin, true); // $key = array_search($id, $origin_arr); unset($origin_arr[$id]); $save = $redis::instance()->hset("blacklist_{$kf_info['group_id']}", $kf_id, json_encode($origin_arr)); // if ($save !== 0 || $save !== 1) return resultJson(0, '失败'); LogRepository::userLogWrite('黑名单管理', '客服['.$kf_info['user_name'].']将用户['.$id.']解除黑名单成功'); return resultJson(1, '成功'); } } }