System.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Office;
  4. use think\cache\driver\Redis;
  5. /**
  6. * 管理系统系统设置类
  7. */
  8. class System extends Base
  9. {
  10. /**
  11. * 基础设置
  12. *
  13. * @access public
  14. */
  15. public function basics()
  16. {
  17. // 表单提交.
  18. if (request()->isPost()) {
  19. $param = input('post.');
  20. try {
  21. // 修改系统欢迎语.
  22. if (empty($param['advertisement_img']) === false) {
  23. $updateAstData['advertisement_img'] = $param['advertisement_img'];
  24. }
  25. if (empty($param['logo_img']) === false) {
  26. $updateinfo['logo'] = $param['logo_img'];
  27. }
  28. $updateinfo['enterprise_name'] = $param['enterprise_name'];
  29. $updateAstData['advertisement_url'] = $param['advertisementUrl'];
  30. $updateAstData['advertisement_status'] = $param['status'];
  31. model('Advertisement')->updateAst($updateAstData);
  32. // 修改系统欢迎语.
  33. $updateSysData['word'] = $param['systemWord'];
  34. $updateSysWhere['id'] = 1;
  35. model('Reply')->updateReply($updateSysWhere, $updateSysData);
  36. // 修改客服欢迎语.
  37. $updateSevData['word'] = $param['serverWord'];
  38. $updateSevWhere['id'] = 2;
  39. model('Reply')->updateReply($updateSevWhere, $updateSevData);
  40. if(!empty($updateinfo)){
  41. db('settings')->where('id',1)->update($updateinfo);
  42. }
  43. return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
  44. } catch (\Exception $e) {
  45. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  46. }//end try
  47. }//end if
  48. // 获取广告.
  49. $advertisement = model('Advertisement')->findAst();
  50. // 获取系统欢迎语.
  51. $replySystemWhere['id'] = 1;
  52. $replySystem = model('Reply')->findReply($replySystemWhere);
  53. // 获取客服欢迎语.
  54. $replyServerWhere['id'] = 2;
  55. $replyServer = model('Reply')->findReply($replyServerWhere);
  56. // 获取logo.
  57. $settings = db('settings')->find();
  58. $this->assign(
  59. [
  60. 'advertisement' => $advertisement,
  61. 'replySystem' => $replySystem,
  62. 'replyServer' => $replyServer,
  63. 'settings' => $settings,
  64. 'status' => config('kf_status'),
  65. ]
  66. );
  67. return $this->fetch();
  68. }//end basics()
  69. /**
  70. * 会话设置
  71. *
  72. * @access public
  73. */
  74. public function conversation()
  75. {
  76. // 表单提交.
  77. if (request()->isPost()) {
  78. $param = input('post.');
  79. try {
  80. // 修改会话超时.
  81. $updateOvertimeData['systemconfig_data'] = $param['overtime'];
  82. $updateOvertimeData['systemconfig_content'] = $param['overtimeInfo'];
  83. $updateOvertimeWhere['systemconfig_id'] = 1;
  84. model('Systemconfig')->updateSystemconfig($updateOvertimeWhere, $updateOvertimeData);
  85. // 修改访客静默.
  86. $upUptdData['systemconfig_data'] = $param['unoperated'];
  87. $upUptdData['systemconfig_content'] = $param['unoperatedInfo'];
  88. $upUptdWhere['systemconfig_id'] = 2;
  89. model('Systemconfig')->updateSystemconfig($upUptdWhere, $upUptdData);
  90. // 质检会话时长设置.
  91. $upAllTimeData['systemconfig_data'] = $param['verifyAllTime'];
  92. $upAllTimeWhere['systemconfig_id'] = 3;
  93. model('Systemconfig')->updateSystemconfig($upAllTimeWhere, $upAllTimeData);
  94. // 质检会话响应时长设置.
  95. $upReturnTimeData['systemconfig_data'] = $param['verifyReturnTime'];
  96. $upReturnTimeWhere['systemconfig_id'] = 4;
  97. model('Systemconfig')->updateSystemconfig($upReturnTimeWhere, $upReturnTimeData);
  98. // 满意度评价回合限制.
  99. $upRoundData['systemconfig_data'] = $param['round'];
  100. $upRoundWhere['systemconfig_id'] = 5;
  101. model('Systemconfig')->updateSystemconfig($upRoundWhere, $upRoundData);
  102. // 客服接待人数设置.
  103. $upMSWhere['systemconfig_enName'] = 'KFMaxServices';
  104. $upMSData['systemconfig_data'] = $param['max_service'];
  105. model('Systemconfig')->updateSystemconfig($upMSWhere, $upMSData);
  106. // 最大排队人数设置.
  107. $upMWWhere['systemconfig_enName'] = 'maxWait';
  108. $upMWData['systemconfig_data'] = $param['kfConfig_maxWait'];
  109. model('Systemconfig')->updateSystemconfig($upMWWhere, $upMWData);
  110. return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
  111. } catch (\Exception $e) {
  112. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  113. }//end try
  114. }//end if
  115. // 获取设置.
  116. $systemconfig = model('Systemconfig')->selectSystemconfig();
  117. $this->assign(
  118. [
  119. 'systemconfig' => $systemconfig,
  120. 'status' => config('kf_status'),
  121. ]
  122. );
  123. return $this->fetch();
  124. }//end conversation()
  125. // 自动回复设置
  126. public function reply()
  127. {
  128. if(request()->isPost()){
  129. $param = input('post.');
  130. if(empty($param['word'])){
  131. return json(['code' => -1, 'data' => '', 'msg' => '回复内容不能为空']);
  132. }
  133. try{
  134. db('reply')->where('id', 1)->update($param);
  135. }catch(\Exception $e){
  136. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  137. }
  138. return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
  139. }
  140. $info = db('reply')->where('id', 1)->find();
  141. $this->assign([
  142. 'info' => $info,
  143. 'status' => config('kf_status')
  144. ]);
  145. return $this->fetch();
  146. }
  147. // 历史会话记录
  148. public function wordsLog()
  149. {
  150. // $toExcel = input('param.toExcel', 0);
  151. if(request()->isAjax()){
  152. $param = input('param.');
  153. $limit = $param['pageSize'];
  154. $offset = ($param['pageNumber'] - 1) * $limit;
  155. // 默认显示最近7天
  156. $start = input('param.start');
  157. $end = input('param.end');
  158. $user_id = input('param.user_id');
  159. $group_id = input('param.group_id');
  160. $temp = db('service_log');
  161. $countTmp = db('service_log');
  162. if(strlen($param['searchText'])){
  163. $temp = $temp->whereLike('user_name', '%'.$param['searchText'].'%');
  164. $countTmp = $countTmp->whereLike('user_name', '%'.$param['searchText'].'%');
  165. }
  166. //日期
  167. if(!empty($start) && !empty($end) && $start <= $end){
  168. $temp = $temp->whereBetween('start_time', [strtotime($start), strtotime($end . ' 23:59:59')]);
  169. $countTmp = $countTmp->whereBetween('start_time', [strtotime($start), strtotime($end . ' 23:59:59')]);
  170. }
  171. //结束时间为空
  172. if(!empty($start) && empty($end)){
  173. $temp = $temp->where('start_time','>',strtotime($start));
  174. $countTmp = $countTmp->where('start_time','>',strtotime($start));
  175. }
  176. //开始时间为空
  177. if(empty($start) && !empty($end)){
  178. $temp = $temp->where('start_time','<',strtotime($end . ' 23:59:59'));
  179. $countTmp = $countTmp->where('start_time','<',strtotime($end . ' 23:59:59'));
  180. }
  181. //客服
  182. if($user_id != 0){
  183. $temp = $temp->where('kf_id', $user_id);
  184. $countTmp = $countTmp->where('kf_id', $user_id);
  185. }
  186. //客服组
  187. if($group_id != 0){
  188. $temp = $temp->where('group_id', $group_id);
  189. $countTmp = $countTmp->where('group_id', $group_id);
  190. }
  191. $result = $temp->limit($offset, $limit)->order('start_time', 'desc')->select();
  192. //所有客服
  193. $users = db('users')->select();
  194. //所有客服组
  195. $groups = db('groups')->select();
  196. //满意度
  197. $evaluate = db('evaluate')->select();
  198. //$alarm报警信息
  199. $alarm = db('alarm')->select();
  200. foreach($result as $key=>$vo){
  201. if($result[$key]['intime'] != 0){
  202. $result[$key]['intime'] = date('Y-m-d H:i:s', $vo['intime']);
  203. $date = explode(' ',$result[$key]['intime']);
  204. $year = explode('-',$result[$key]['intime']);
  205. if($date[0] == date('Y-m-d',time())){
  206. $result[$key]['intime'] = '<span style="float: right;">'.$date[1].'</span>';
  207. }else if($date[0] == date('Y-m-d',strtotime("-1 day"))){
  208. $result[$key]['intime'] = '<span style="float: right;">'.'昨天 '.$date[1].'</span>';
  209. }else{
  210. if($year[0] == date('Y',time())){
  211. $result[$key]['intime'] = '<span style="float: right;">'.date('m-d H:i:s',strtotime($result[$key]['intime'])).'</span>';
  212. }
  213. }
  214. }else{
  215. $result[$key]['intime'] = '-';
  216. }
  217. if($result[$key]['start_time'] != 0){
  218. $result[$key]['start_time'] = date('Y-m-d H:i:s', $vo['start_time']);
  219. $date = explode(' ',$result[$key]['start_time']);
  220. $year = explode('-',$result[$key]['start_time']);
  221. if($date[0] == date('Y-m-d',time())){
  222. $result[$key]['start_time'] = '<span style="float: right;">'.$date[1].'</span>';
  223. }else if($date[0] == date('Y-m-d',strtotime("-1 day"))){
  224. $result[$key]['start_time'] = '<span style="float: right;">'.'昨天 '.$date[1].'</span>';
  225. }else{
  226. if($year[0] == date('Y',time())){
  227. $result[$key]['start_time'] = '<span style="float: right;">'.date('m-d H:i:s',strtotime($result[$key]['start_time'])).'</span>';
  228. }
  229. }
  230. }else{
  231. $result[$key]['start_time'] = '-';
  232. }
  233. if($result[$key]['end_time'] != 0){
  234. $result[$key]['end_time'] = date('Y-m-d H:i:s', $vo['end_time']);
  235. $date = explode(' ',$result[$key]['end_time']);
  236. $year = explode('-',$result[$key]['end_time']);
  237. if($date[0] == date('Y-m-d',time())){
  238. $result[$key]['end_time'] = '<span style="float: right;">'.$date[1].'</span>';
  239. }else if($date[0] == date('Y-m-d',strtotime("-1 day"))){
  240. $result[$key]['end_time'] = '<span style="float: right;">'.'昨天 '.$date[1].'</span>';
  241. }else{
  242. if($year[0] == date('Y',time())){
  243. $result[$key]['end_time'] = '<span style="float: right;">'.date('m-d H:i:s',strtotime($result[$key]['end_time'])).'</span>';
  244. }
  245. }
  246. }else{
  247. $result[$key]['end_time'] = '-';
  248. }
  249. //客服名称
  250. for($i=0;$i<count($users);$i++){
  251. if($result[$key]['kf_id'] == $users[$i]['id']){
  252. $result[$key]['kefu_name'] = $users[$i]['user_name'];
  253. }
  254. }
  255. //满意度
  256. for($j=0;$j<count($evaluate);$j++){
  257. if($result[$key]['evaluate_id'] == $evaluate[$j]['evaluate_id']){
  258. $result[$key]['evaluate_name'] = $evaluate[$j]['evaluate_name'];
  259. }
  260. }
  261. //客服所在组
  262. for($a=0;$a<count($groups);$a++){
  263. if($result[$key]['group_id'] == $groups[$a]['id']){
  264. $result[$key]['group_name'] = $groups[$a]['name'];
  265. }
  266. }
  267. //会话时长/响应时长
  268. for($b=0;$b<count($alarm);$b++){
  269. if($result[$key]['servicelog_id'] == $alarm[$b]['servicelog_id']){
  270. //会话时长
  271. $conversation_min = intval($alarm[$b]['alarm_cvtOvertime']/60);
  272. $conversation_s = $alarm[$b]['alarm_cvtOvertime']%60;
  273. $result[$key]['conversation'] = $conversation_min.'分'.$conversation_s.'秒';
  274. //响应时长
  275. $response_min = intval($alarm[$b]['alarm_corresponding']/60);
  276. $response_s = $alarm[$b]['alarm_corresponding']%60;
  277. $result[$key]['response'] = $response_min.'分'.$response_s.'秒';
  278. }
  279. }
  280. if($vo['servicelog_close_type'] == 0){
  281. $result[$key]['servicelog_close_type'] = '未知';
  282. }
  283. if($vo['servicelog_close_type'] == 1){
  284. $result[$key]['servicelog_close_type'] = '无效会话';
  285. }
  286. if($vo['servicelog_close_type'] == 2){
  287. $result[$key]['servicelog_close_type'] = '双方静默';
  288. }
  289. if($vo['servicelog_close_type'] == 3){
  290. $result[$key]['servicelog_close_type'] = '客服关闭';
  291. }
  292. if($vo['servicelog_close_type'] == 4){
  293. $result[$key]['servicelog_close_type'] = '客服掉线';
  294. }
  295. if($vo['servicelog_close_type'] == 5){
  296. $result[$key]['servicelog_close_type'] = '转出';
  297. }
  298. // 生成操作按钮
  299. if(0 != $vo['servicelog_id']){
  300. $result[$key]['operate'] = $this->makeBtn($vo['servicelog_id']);
  301. }
  302. }
  303. $return['total'] = $countTmp->count(); //总数据
  304. $return['rows'] = $result;
  305. // if (!$toExcel) {
  306. // return json($return);
  307. // } else {
  308. // $head = ['工单id', '访客进线时间', '接待客服', '所在组', '访客账号', '开始时间', '结束时间', '会话时长', '响应时长', '关闭原因', '满意度'];
  309. // $key = ['servicelog_id', 'intime', 'kefu_name', 'group_name', 'user_name', 'start_time', 'end_time', 'conversation', 'response', 'servicelog_close_type', 'evaluate_name'];
  310. // (new Office())->outdata('工作报表数据导出', $result, $head, $key);
  311. // return true;
  312. // }
  313. return json($return);
  314. }
  315. //所有客服
  316. $users = db('users')->select();
  317. $useroption = '';
  318. if(!empty($users)){
  319. $option = '<option value="0">全部客服</option>';
  320. for($i=0;$i<count($users);$i++){
  321. $option = $option.'<option value="'.$users[$i]['id'].'">'.$users[$i]['user_name'].'</option>';
  322. }
  323. $useroption = '<select class="selector_user" lay-verify="required" lay-filter="user_id">'.$option.'</select>';
  324. }
  325. //所有客服组
  326. $groups = db('groups')->select();
  327. $groupoption = '';
  328. if(!empty($groups)){
  329. $option = '<option value="0">全部客服组</option>';
  330. for($j=0;$j<count($groups);$j++){
  331. $option = $option.'<option value="'.$groups[$j]['id'].'">'.$groups[$j]['name'].'</option>';
  332. }
  333. $groupoption = '<select class="selector_group" lay-verify="required" lay-filter="group_id">'.$option.'</select>';
  334. }
  335. $this->assign([
  336. 'useroption' => $useroption,
  337. 'groupoption' => $groupoption
  338. ]);
  339. return $this->fetch('wordslog');
  340. }
  341. function matching($str, $a, $b)
  342. {
  343. $pattern = '/('.$a.')(.*)(?)('.$b.')/'; //正则规则匹配支付串中任何一个位置字符串
  344. //$pattern = '/(#\[)(.*)(?)(\]\/)/';
  345. preg_match_all($pattern,$str,$m);
  346. //preg_match_all($pattern, $str, $m); //返回一个匹配结果
  347. //print_r($m);die; //到时候在这里书写返回值就好了 .die;
  348. }
  349. // 历史会话记录详情
  350. public function detail($id, $type='')
  351. {
  352. $chat = db('chat_log')->where('servicelog_id',$id)->order('time_line')->select();
  353. $html = '';
  354. for($i=0;$i<count($chat);$i++){
  355. $content = json_decode($chat[$i]['content'], true);
  356. $chat[$i]['time_line'] = date('H:i',$chat[$i]['time_line']);
  357. if(!empty($content['text'])){
  358. $content['content'] = '&nbsp&nbsp'.$content['text'].'&nbsp&nbsp';
  359. }
  360. if(!empty($content['img'])){
  361. $content['content'] = '<img width="100%" src="'.$content['img'].'"/>';
  362. }
  363. if(!empty(strstr($chat[$i]['to_id'], 'KF'))){
  364. /*$preg1 = "/^#[*]$/";
  365. $preg= '/xue[\s\S]*?om/i';
  366. preg_match_all($preg1,"#[哈哈]/",$res);
  367. var_dump($res);*/
  368. $this->matching("#[哈哈]/","#\[","\]\/");
  369. //$this->getFacesIcon();
  370. $html = $html . '<div style="margin-top:15px;width:75%"><div>'.$chat[$i]['from_name'].'&nbsp&nbsp&nbsp'.$chat[$i]['time_line'].'</div>';
  371. $html = $html . '<div style="margin-top:5px;display:inline-block;*display:inline;*zoom:1;word-break:break-all;word-wrap:break-word" class="form-content">'.$content['content'].'</div></div>';
  372. }else{
  373. $html = $html . '<div style="margin-top:15px;width:75%;margin-left:25%;text-align:right;"><div>'.$chat[$i]['from_name'].'&nbsp&nbsp&nbsp'.$chat[$i]['time_line'].'</div>';
  374. $html = $html . '<div style="margin-top:5px;display:inline-block;*display:inline;*zoom:1;text-align:left;word-break:break-all;word-wrap:break-word" class="form-content">'.$content['content'].'</div></div>';
  375. }
  376. }
  377. $redis = new Redis;
  378. if ($type === 'onLine') {
  379. $servicelog = json_decode($redis->handler()->Hget('SERVICELOG', $id), true);
  380. } else {
  381. $servicelog = db('service_log')->where('servicelog_id',$id)->find();
  382. }
  383. //满意度
  384. $evaluate = db('evaluate')->where('evaluate_id',$servicelog['evaluate_id'])->find();
  385. $evaluate = '<img width="40px" style="margin-top:15px;" src="'.$evaluate['evaluate_url'].'"/>';
  386. //$alarm报警信息
  387. if ($type === 'onLine') {
  388. $alarm = json_decode($redis->handler()->Hget('SERVICELOG', $id), true);
  389. } else {
  390. $alarm = db('alarm')->where('servicelog_id',$id)->find();
  391. }
  392. //会话超时标准
  393. $verifyAllTime = db('systemconfig')->where('systemconfig_name','质检会话时长设置')->find();
  394. //会话响应时长标准
  395. $verifyReturnTime = db('systemconfig')->where('systemconfig_name','质检会话响应时长设置')->find();
  396. $span = '';
  397. if(!empty($alarm)){
  398. if($alarm['alarm_userSensitive'] != 0){
  399. $span = $span . '<span class="alarm_info">访客敏感词</span>';
  400. }
  401. if($alarm['alarm_serverSensitive'] != 0){
  402. $span = $span . '<span class="alarm_info">客服敏感词</span>';
  403. }
  404. if($alarm['alarm_corresponding'] > $verifyReturnTime['systemconfig_data']){
  405. $span = $span . '<span class="alarm_info">响应超时</span>';
  406. }
  407. if($alarm['alarm_cvtOvertime'] > $verifyAllTime['systemconfig_data']){
  408. $span = $span . '<span class="alarm_info">会话超时</span>';
  409. }
  410. if($alarm['alarm_respond'] == 1){
  411. $span = $span . '<span class="alarm_info">客服未回应</span>';
  412. }
  413. }
  414. //用户信息
  415. $account = db('accounts')->where('id',$servicelog['user_id'])->find();
  416. $label = db('accountslabel')->where('id', $account['label_id'])->find();
  417. $account['label'] = $label['name'];
  418. $this->assign([
  419. 'html' => $html,
  420. 'span' => $span,
  421. 'evaluate' => $evaluate,
  422. 'servicelog' => $servicelog,
  423. 'account' => $account
  424. ]);
  425. return $this->fetch();
  426. }
  427. // 生成按钮
  428. private function makeBtn($id)
  429. {
  430. $operate = '<a href="' . url('system/detail', ['id' => $id]) . '">';
  431. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 详情</button></a> ';
  432. return $operate;
  433. }
  434. public function getFacesIcon($facesIcon) {
  435. $data = ["[微笑]", "[嘻嘻]", "[哈哈]", "[可爱]", "[可怜]", "[挖鼻]", "[吃惊]", "[害羞]", "[挤眼]", "[闭嘴]", "[鄙视]",
  436. "[爱你]", "[泪]", "[偷笑]", "[亲亲]", "[生病]", "[太开心]", "[白眼]", "[右哼哼]", "[左哼哼]", "[嘘]", "[衰]",
  437. "[委屈]", "[吐]", "[哈欠]", "[抱抱]", "[怒]", "[疑问]", "[馋嘴]", "[拜拜]", "[思考]", "[汗]", "[困]", "[睡]",
  438. "[钱]", "[失望]", "[酷]", "[色]", "[哼]", "[鼓掌]", "[晕]", "[悲伤]", "[抓狂]", "[黑线]", "[阴险]", "[怒骂]",
  439. "[互粉]", "[心]", "[伤心]", "[猪头]", "[熊猫]", "[兔子]", "[ok]", "[耶]", "[good]", "[NO]", "[赞]", "[来]",
  440. "[弱]", "[草泥马]", "[神马]", "[囧]", "[浮云]", "[给力]", "[围观]", "[威武]", "[奥特曼]", "[礼物]", "[钟]",
  441. "[话筒]", "[蜡烛]", "[蛋糕]"];
  442. $key = array_search($facesIcon, $data);
  443. return $key;
  444. }
  445. public function toexcel()
  446. {
  447. $param = input('param.');
  448. $limit = $param['pageSize'];
  449. $offset = ($param['pageNumber'] - 1) * $limit;
  450. $start = $param['start'];
  451. $end = $param['end'];
  452. $where = [];
  453. if($param['user_id'] != 0){
  454. $where['kf_id'] = $param['user_id'];
  455. }
  456. if($param['group_id'] != 0){
  457. $where['group_id'] = $param['group_id'];
  458. }
  459. if(!empty($param['username'])){
  460. $where['user_name'] = $param['username'];
  461. }
  462. $result = db('service_log')->whereBetween('start_time', [strtotime($start), strtotime($end . ' 23:59:59')])->where($where)->limit($offset, $limit)->order('start_time', 'desc')->select();
  463. //所有客服
  464. $users = db('users')->select();
  465. //所有客服组
  466. $groups = db('groups')->select();
  467. //满意度
  468. $evaluate = db('evaluate')->select();
  469. //$alarm报警信息
  470. $alarm = db('alarm')->select();
  471. foreach($result as $key=>$vo){
  472. if($result[$key]['intime'] != 0){
  473. $result[$key]['intime'] = date('Y-m-d H:i:s', $vo['intime']);
  474. }else{
  475. $result[$key]['intime'] = '-';
  476. }
  477. if($result[$key]['start_time'] != 0){
  478. $result[$key]['start_time'] = date('Y-m-d H:i:s', $vo['start_time']);
  479. }else{
  480. $result[$key]['start_time'] = '-';
  481. }
  482. if($result[$key]['end_time'] != 0){
  483. $result[$key]['end_time'] = date('Y-m-d H:i:s', $vo['end_time']);
  484. }else{
  485. $result[$key]['end_time'] = '-';
  486. }
  487. //客服昵称
  488. for($i=0;$i<count($users);$i++){
  489. if($result[$key]['kf_id'] == $users[$i]['id']){
  490. $result[$key]['kefu_name'] = $users[$i]['user_name'];
  491. }
  492. }
  493. //满意度
  494. $result[$key]['evaluate_name'] = '';
  495. for($j=0;$j<count($evaluate);$j++){
  496. if($result[$key]['evaluate_id'] == $evaluate[$j]['evaluate_id']){
  497. $result[$key]['evaluate_name'] = $evaluate[$j]['evaluate_name'];
  498. }
  499. }
  500. //客服所在组
  501. for($a=0;$a<count($groups);$a++){
  502. if($result[$key]['group_id'] == $groups[$a]['id']){
  503. $result[$key]['group_name'] = $groups[$a]['name'];
  504. }
  505. }
  506. //会话时长/响应时长
  507. $result[$key]['conversation'] = '';
  508. $result[$key]['response'] = '';
  509. for($b=0;$b<count($alarm);$b++){
  510. if($result[$key]['servicelog_id'] == $alarm[$b]['servicelog_id']){
  511. //会话时长
  512. $conversation_min = intval($alarm[$b]['alarm_cvtOvertime']/60);
  513. $conversation_s = $alarm[$b]['alarm_cvtOvertime']%60;
  514. $result[$key]['conversation'] = $conversation_min.'分'.$conversation_s.'秒';
  515. //响应时长
  516. $response_min = intval($alarm[$b]['alarm_corresponding']/60);
  517. $response_s = $alarm[$b]['alarm_corresponding']%60;
  518. $result[$key]['response'] = $response_min.'分'.$response_s.'秒';
  519. }
  520. }
  521. if($vo['servicelog_close_type'] == 0){
  522. $result[$key]['servicelog_close_type'] = '未知';
  523. }
  524. if($vo['servicelog_close_type'] == 1){
  525. $result[$key]['servicelog_close_type'] = '访客静默';
  526. }
  527. if($vo['servicelog_close_type'] == 2){
  528. $result[$key]['servicelog_close_type'] = '会话超时';
  529. }
  530. if($vo['servicelog_close_type'] == 3){
  531. $result[$key]['servicelog_close_type'] = '客服关闭';
  532. }
  533. if($vo['servicelog_close_type'] == 4){
  534. $result[$key]['servicelog_close_type'] = '客服掉线';
  535. }
  536. if($vo['servicelog_close_type'] == 5){
  537. $result[$key]['servicelog_close_type'] = '转接';
  538. }
  539. //工单聊天详情
  540. $chat_log = db('chat_log')->where('servicelog_id',$vo['servicelog_id'])->order('time_line', 'desc')->select();
  541. $result[$key]['detail'] = '';
  542. for($c=0;$c<count($chat_log);$c++){
  543. $content = json_decode($chat_log[$c]['content'], true);
  544. $chat_log[$c]['time_line'] = date('Y-m-d H:i:s',$chat_log[$c]['time_line']);
  545. $result[$key]['detail'] = $result[$key]['detail'].' '.$chat_log[$c]['time_line'].' '.$chat_log[$c]['from_name'];
  546. if(!empty($content['text'])){
  547. $content['content'] = $content['text'];
  548. }
  549. if(!empty($content['img'])){
  550. $content['content'] = '<img width="100%" src="'.$content['img'].'"/>';
  551. }
  552. $result[$key]['detail'] = $result[$key]['detail'].' '.$content['content'];
  553. }
  554. }
  555. $head = ['工单id', '访客进线时间', '接待客服', '所在组', '访客账号', '开始时间', '结束时间', '会话时长', '响应时长', '关闭原因', '满意度','来源ip','来源网站','来源系统','来源浏览器','聊天详情'];
  556. $key = ['servicelog_id', 'intime', 'kefu_name', 'group_name', 'user_name', 'start_time', 'end_time', 'conversation', 'response', 'servicelog_close_type', 'evaluate_name','user_ip','website','system','browse','detail'];
  557. (new Office())->outdata('工单数据导出', $result, $head, $key);
  558. return true;
  559. }
  560. }