System.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 管理系统系统设置类
  5. */
  6. class System extends Base
  7. {
  8. /**
  9. * 基础设置
  10. *
  11. * @access public
  12. */
  13. public function basics()
  14. {
  15. // 表单提交.
  16. if (request()->isPost()) {
  17. $param = input('post.');
  18. try {
  19. // 修改系统欢迎语.
  20. if (empty($param['advertisement_img']) === false) {
  21. $updateAstData['advertisement_img'] = $param['advertisement_img'];
  22. }
  23. $updateAstData['advertisement_url'] = $param['advertisementUrl'];
  24. $updateAstData['advertisement_status'] = $param['status'];
  25. model('Advertisement')->updateAst($updateAstData);
  26. // 修改系统欢迎语.
  27. $updateSysData['word'] = $param['systemWord'];
  28. $updateSysWhere['id'] = 1;
  29. model('Reply')->updateReply($updateSysWhere, $updateSysData);
  30. // 修改客服欢迎语.
  31. $updateSevData['word'] = $param['serverWord'];
  32. $updateSevWhere['id'] = 2;
  33. model('Reply')->updateReply($updateSevWhere, $updateSevData);
  34. return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
  35. } catch (\Exception $e) {
  36. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  37. }//end try
  38. }//end if
  39. // 获取广告.
  40. $advertisement = model('Advertisement')->findAst();
  41. // 获取系统欢迎语.
  42. $replySystemWhere['id'] = 1;
  43. $replySystem = model('Reply')->findReply($replySystemWhere);
  44. // 获取客服欢迎语.
  45. $replyServerWhere['id'] = 2;
  46. $replyServer = model('Reply')->findReply($replyServerWhere);
  47. $this->assign(
  48. [
  49. 'advertisement' => $advertisement,
  50. 'replySystem' => $replySystem,
  51. 'replyServer' => $replyServer,
  52. 'status' => config('kf_status'),
  53. ]
  54. );
  55. return $this->fetch();
  56. }//end basics()
  57. /**
  58. * 会话设置
  59. *
  60. * @access public
  61. */
  62. public function conversation()
  63. {
  64. // 表单提交.
  65. if (request()->isPost()) {
  66. $param = input('post.');
  67. try {
  68. // 修改会话超时.
  69. $updateOvertimeData['systemconfig_data'] = $param['overtime'];
  70. $updateOvertimeWhere['systemconfig_id'] = 1;
  71. model('Systemconfig')->updateSystemconfig($updateOvertimeWhere, $updateOvertimeData);
  72. // 修改访客静默.
  73. $upUptdData['systemconfig_data'] = $param['unoperated'];
  74. $upUptdWhere['systemconfig_id'] = 2;
  75. model('Systemconfig')->updateSystemconfig($upUptdWhere, $upUptdData);
  76. // 质检会话时长设置.
  77. $upAllTimeData['systemconfig_data'] = $param['verifyAllTime'];
  78. $upAllTimeWhere['systemconfig_id'] = 3;
  79. model('Systemconfig')->updateSystemconfig($upAllTimeWhere, $upAllTimeData);
  80. // 质检会话响应时长设置.
  81. $upReturnTimeData['systemconfig_data'] = $param['verifyReturnTime'];
  82. $upReturnTimeWhere['systemconfig_id'] = 4;
  83. model('Systemconfig')->updateSystemconfig($upReturnTimeWhere, $upReturnTimeData);
  84. // 满意度评价回合限制.
  85. $upRoundData['systemconfig_data'] = $param['round'];
  86. $upRoundWhere['systemconfig_id'] = 5;
  87. model('Systemconfig')->updateSystemconfig($upRoundWhere, $upRoundData);
  88. // 客服接待人数设置.
  89. $upKfConfigData['max_service'] = $param['max_service'];
  90. $upKfConfigData['kfConfig_maxWait'] = $param['kfConfig_maxWait'];
  91. db('kf_config')->where('id', 1)->update($upKfConfigData);
  92. return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
  93. } catch (\Exception $e) {
  94. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  95. }//end try
  96. }//end if
  97. // 获取设置.
  98. $systemconfig = model('Systemconfig')->selectSystemconfig();
  99. $kfConfig = db('kf_config')->where('id', 1)->find();
  100. $this->assign(
  101. [
  102. 'systemconfig' => $systemconfig,
  103. 'kfConfig' => $kfConfig,
  104. 'status' => config('kf_status'),
  105. ]
  106. );
  107. return $this->fetch();
  108. }//end conversation()
  109. // 自动回复设置
  110. public function reply()
  111. {
  112. if(request()->isPost()){
  113. $param = input('post.');
  114. if(empty($param['word'])){
  115. return json(['code' => -1, 'data' => '', 'msg' => '回复内容不能为空']);
  116. }
  117. try{
  118. db('reply')->where('id', 1)->update($param);
  119. }catch(\Exception $e){
  120. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  121. }
  122. return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
  123. }
  124. $info = db('reply')->where('id', 1)->find();
  125. $this->assign([
  126. 'info' => $info,
  127. 'status' => config('kf_status')
  128. ]);
  129. return $this->fetch();
  130. }
  131. // 历史会话记录
  132. public function wordsLog()
  133. {
  134. if(request()->isAjax()){
  135. $param = input('param.');
  136. $limit = $param['pageSize'];
  137. $offset = ($param['pageNumber'] - 1) * $limit;
  138. // 默认显示最近7天
  139. $start = input('param.start');
  140. $end = input('param.end');
  141. $user_id = input('param.user_id');
  142. $group_id = input('param.group_id');
  143. $temp = db('service_log');
  144. $countTmp = db('service_log');
  145. if(!empty($param['searchText'])){
  146. $temp = $temp->whereLike('user_name', '%'.$param['searchText'].'%');
  147. $countTmp = $countTmp->whereLike('user_name', '%'.$param['searchText'].'%');
  148. }
  149. //日期
  150. if(!empty($start) && !empty($end) && $start <= $end){
  151. $temp = $temp->whereBetween('start_time', [strtotime($start), strtotime($end . ' 23:59:59')]);
  152. $countTmp = $countTmp->whereBetween('start_time', [strtotime($start), strtotime($end . ' 23:59:59')]);
  153. }
  154. //结束时间为空
  155. if(!empty($start) && empty($end)){
  156. $temp = $temp->where('start_time','>',strtotime($start));
  157. $countTmp = $temp->where('start_time','>',strtotime($start));
  158. }
  159. //开始时间为空
  160. if(empty($start) && !empty($end)){
  161. $temp = $temp->where('start_time','<',strtotime($end . ' 23:59:59'));
  162. $countTmp = $temp->where('start_time','<',strtotime($end . ' 23:59:59'));
  163. }
  164. //开始时间/结束时间都为空(默认查七天)
  165. // if(empty($start) && empty($end)){
  166. // $temp = $temp->where('start_time','<',time())->where('start_time','>',time()-604800);
  167. // $countTmp = $temp->where('start_time','<',time())->where('start_time','>',time()-604800);
  168. // }
  169. //客服
  170. if($user_id != 0){
  171. $temp = $temp->where('kf_id', $user_id);
  172. $countTmp = $countTmp->where('kf_id', $user_id);
  173. }
  174. //客服组
  175. if($group_id != 0){
  176. $temp = $temp->where('group_id', $group_id);
  177. $countTmp = $temp->where('group_id', $group_id);
  178. }
  179. $result = $temp->limit($offset, $limit)->order('start_time', 'desc')->select();
  180. //所有客服
  181. $users = db('users')->select();
  182. //所有客服组
  183. $groups = db('groups')->select();
  184. //满意度
  185. $evaluate = db('evaluate')->select();
  186. //$alarm报警信息
  187. $alarm = db('alarm')->select();
  188. foreach($result as $key=>$vo){
  189. if($result[$key]['intime'] != 0){
  190. $result[$key]['intime'] = date('Y-m-d H:i:s', $vo['intime']);
  191. }else{
  192. $result[$key]['intime'] = '-';
  193. }
  194. if($result[$key]['start_time'] != 0){
  195. $result[$key]['start_time'] = date('Y-m-d H:i:s', $vo['start_time']);
  196. }else{
  197. $result[$key]['start_time'] = '-';
  198. }
  199. if($result[$key]['end_time'] != 0){
  200. $result[$key]['end_time'] = date('Y-m-d H:i:s', $vo['end_time']);
  201. }else{
  202. $result[$key]['end_time'] = '-';
  203. }
  204. //客服名称
  205. for($i=0;$i<count($users);$i++){
  206. if($result[$key]['kf_id'] == $users[$i]['id']){
  207. $result[$key]['kefu_name'] = $users[$i]['user_name'];
  208. }
  209. }
  210. //满意度
  211. for($j=0;$j<count($evaluate);$j++){
  212. if($result[$key]['evaluate_id'] == $evaluate[$j]['evaluate_id']){
  213. $result[$key]['evaluate_name'] = $evaluate[$j]['evaluate_name'];
  214. }
  215. }
  216. //客服所在组
  217. for($a=0;$a<count($groups);$a++){
  218. if($result[$key]['group_id'] == $groups[$a]['id']){
  219. $result[$key]['group_name'] = $groups[$a]['name'];
  220. }
  221. }
  222. //会话时长/响应时长
  223. for($b=0;$b<count($alarm);$b++){
  224. if($result[$key]['servicelog_id'] == $alarm[$b]['servicelog_id']){
  225. //会话时长
  226. $conversation_min = intval($alarm[$b]['alarm_cvtOvertime']/60);
  227. $conversation_s = $alarm[$b]['alarm_cvtOvertime']%60;
  228. $result[$key]['conversation'] = $conversation_min.'分'.$conversation_s.'秒';
  229. //响应时长
  230. $response_min = intval($alarm[$b]['alarm_corresponding']/60);
  231. $response_s = $alarm[$b]['alarm_corresponding']%60;
  232. $result[$key]['response'] = $response_min.'分'.$response_s.'秒';
  233. }
  234. }
  235. if($vo['servicelog_close_type'] == 0){
  236. $result[$key]['servicelog_close_type'] = '未知';
  237. }
  238. if($vo['servicelog_close_type'] == 1){
  239. $result[$key]['servicelog_close_type'] = '访客静默';
  240. }
  241. if($vo['servicelog_close_type'] == 2){
  242. $result[$key]['servicelog_close_type'] = '会话超时';
  243. }
  244. if($vo['servicelog_close_type'] == 3){
  245. $result[$key]['servicelog_close_type'] = '客服关闭';
  246. }
  247. if($vo['servicelog_close_type'] == 4){
  248. $result[$key]['servicelog_close_type'] = '客服掉线';
  249. }
  250. if($vo['servicelog_close_type'] == 5){
  251. $result[$key]['servicelog_close_type'] = '转接';
  252. }
  253. // 生成操作按钮
  254. if(0 != $vo['servicelog_id']){
  255. $result[$key]['operate'] = $this->makeBtn($vo['servicelog_id']);
  256. }
  257. }
  258. $return['total'] = $countTmp->count(); //总数据
  259. $return['rows'] = $result;
  260. return json($return);
  261. }
  262. //所有客服
  263. $users = db('users')->select();
  264. $useroption = '';
  265. if(!empty($users)){
  266. $option = '<option value="0">全部客服</option>';
  267. for($i=0;$i<count($users);$i++){
  268. $option = $option.'<option value="'.$users[$i]['id'].'">'.$users[$i]['user_name'].'</option>';
  269. }
  270. $useroption = '<select lay-verify="required" lay-filter="user_id">'.$option.'</select>';
  271. }
  272. //所有客服组
  273. $groups = db('groups')->select();
  274. $groupoption = '';
  275. if(!empty($groups)){
  276. $option = '<option value="0">全部客服组</option>';
  277. for($j=0;$j<count($groups);$j++){
  278. $option = $option.'<option value="'.$groups[$j]['id'].'">'.$groups[$j]['name'].'</option>';
  279. }
  280. $groupoption = '<select lay-verify="required" lay-filter="group_id">'.$option.'</select>';
  281. }
  282. $this->assign([
  283. 'useroption' => $useroption,
  284. 'groupoption' => $groupoption
  285. ]);
  286. return $this->fetch('wordslog');
  287. }
  288. function matching($str, $a, $b)
  289. {
  290. $pattern = '/('.$a.')(.*)(?)('.$b.')/'; //正则规则匹配支付串中任何一个位置字符串
  291. //$pattern = '/(#\[)(.*)(?)(\]\/)/';
  292. preg_match_all($pattern,$str,$m);
  293. //preg_match_all($pattern, $str, $m); //返回一个匹配结果
  294. //print_r($m);die; //到时候在这里书写返回值就好了 .die;
  295. }
  296. // 历史会话记录详情
  297. public function detail($id)
  298. {
  299. $chat = db('chat_log')->where('servicelog_id',$id)->order('time_line')->select();
  300. $html = '';
  301. for($i=0;$i<count($chat);$i++){
  302. $content = json_decode($chat[$i]['content'], true);
  303. $chat[$i]['time_line'] = date('H:i',$chat[$i]['time_line']);
  304. if(!empty($content['text'])){
  305. $content['content'] = $content['text'];
  306. }
  307. if(!empty($content['img'])){
  308. $content['content'] = '<img width="100%" src="'.$content['img'].'"/>';
  309. }
  310. if(!empty(strstr($chat[$i]['to_id'], 'KF'))){
  311. /*$preg1 = "/^#[*]$/";
  312. $preg= '/xue[\s\S]*?om/i';
  313. preg_match_all($preg1,"#[哈哈]/",$res);
  314. var_dump($res);*/
  315. $this->matching("#[哈哈]/","#\[","\]\/");
  316. //$this->getFacesIcon();
  317. $html = $html . '<div style="margin-top:15px;width: 30%;"><div>'.$chat[$i]['from_name'].'&nbsp&nbsp&nbsp'.$chat[$i]['time_line'].'</div>';
  318. $html = $html . '<p style="margin-top:5px;" class="form-content">'.$content['content'].'</p></div>';
  319. }else{
  320. $html = $html . '<div style="margin-top:15px;width: 30%;margin-left:70%;text-align:right;"><div>'.$chat[$i]['from_name'].'&nbsp&nbsp&nbsp'.$chat[$i]['time_line'].'</div>';
  321. $html = $html . '<p style="margin-top:5px;" class="form-content">'.$content['content'].'</p></div>';
  322. }
  323. }
  324. $servicelog = db('service_log')->where('servicelog_id',$id)->find();
  325. //满意度
  326. $evaluate = db('evaluate')->where('evaluate_id',$servicelog['evaluate_id'])->find();
  327. $evaluate = '<img width="40px" style="margin-top:15px;" src="'.$evaluate['evaluate_url'].'"/>';
  328. //$alarm报警信息
  329. $alarm = db('alarm')->where('servicelog_id',$id)->find();
  330. //会话超时标准
  331. $verifyAllTime = db('systemconfig')->where('systemconfig_name','质检会话时长设置')->find();
  332. //会话响应时长标准
  333. $verifyReturnTime = db('systemconfig')->where('systemconfig_name','质检会话响应时长设置')->find();
  334. $span = '';
  335. if(!empty($alarm)){
  336. if($alarm['alarm_userSensitive'] != 0){
  337. $span = $span . '<span class="alarm_info">访客敏感词</span>';
  338. }
  339. if($alarm['alarm_serverSensitive'] != 0){
  340. $span = $span . '<span class="alarm_info">客服敏感词</span>';
  341. }
  342. if($alarm['alarm_corresponding'] > $verifyReturnTime['systemconfig_data']){
  343. $span = $span . '<span class="alarm_info">响应超时</span>';
  344. }
  345. if($alarm['alarm_cvtOvertime'] > $verifyAllTime['systemconfig_data']){
  346. $span = $span . '<span class="alarm_info">会话超时</span>';
  347. }
  348. if($alarm['alarm_respond'] == 1){
  349. $span = $span . '<span class="alarm_info">客服未回应</span>';
  350. }
  351. }
  352. //用户信息
  353. $account = db('accounts')->where('id',$servicelog['user_id'])->find();
  354. $label = db('accountslabel')->where('id', $account['label_id'])->find();
  355. $account['label'] = $label['name'];
  356. $this->assign([
  357. 'html' => $html,
  358. 'span' => $span,
  359. 'evaluate' => $evaluate,
  360. 'servicelog' => $servicelog,
  361. 'account' => $account
  362. ]);
  363. return $this->fetch();
  364. }
  365. // 生成按钮
  366. private function makeBtn($id)
  367. {
  368. $operate = '<a href="' . url('system/detail', ['id' => $id]) . '">';
  369. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 详情</button></a> ';
  370. return $operate;
  371. }
  372. public function getFacesIcon($facesIcon) {
  373. $data = ["[微笑]", "[嘻嘻]", "[哈哈]", "[可爱]", "[可怜]", "[挖鼻]", "[吃惊]", "[害羞]", "[挤眼]", "[闭嘴]", "[鄙视]",
  374. "[爱你]", "[泪]", "[偷笑]", "[亲亲]", "[生病]", "[太开心]", "[白眼]", "[右哼哼]", "[左哼哼]", "[嘘]", "[衰]",
  375. "[委屈]", "[吐]", "[哈欠]", "[抱抱]", "[怒]", "[疑问]", "[馋嘴]", "[拜拜]", "[思考]", "[汗]", "[困]", "[睡]",
  376. "[钱]", "[失望]", "[酷]", "[色]", "[哼]", "[鼓掌]", "[晕]", "[悲伤]", "[抓狂]", "[黑线]", "[阴险]", "[怒骂]",
  377. "[互粉]", "[心]", "[伤心]", "[猪头]", "[熊猫]", "[兔子]", "[ok]", "[耶]", "[good]", "[NO]", "[赞]", "[来]",
  378. "[弱]", "[草泥马]", "[神马]", "[囧]", "[浮云]", "[给力]", "[围观]", "[威武]", "[奥特曼]", "[礼物]", "[钟]",
  379. "[话筒]", "[蜡烛]", "[蛋糕]"];
  380. $key = array_search($facesIcon, $data);
  381. return $key;
  382. }
  383. }