History.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace app\service\controller;
  3. use think\cache\driver\Redis;
  4. /**
  5. * 客服系统会话历史类
  6. */
  7. class History extends Common
  8. {
  9. /**
  10. * 获取当前所有会话
  11. *
  12. * @access public
  13. * @return array JsonString
  14. */
  15. public function allConversation()
  16. {
  17. // 验证token.
  18. $tokenStatus = $this->verifyToken();
  19. $code = -2;
  20. $msg = '错误';
  21. if ($tokenStatus === false) {
  22. $msg = 'token错误';
  23. return json(['code' => $code, 'data' => [], 'msg' => $msg]);
  24. }
  25. try {
  26. // 获取用户信息.
  27. $getUserInfo = $this->getUserInfo();
  28. if ($getUserInfo['user_overview'] != 1) {
  29. $msg = '您没有权限查看';
  30. return json(['code' => 1, 'data' => [], 'msg' => $msg]);
  31. }
  32. $groups = model('Groups')->selectGroups();
  33. $redis = new Redis;
  34. $kefuInfo = $redis->handler()->hGetall('KFINFO');
  35. $servicelog = $redis->handler()->hGetall('SERVICELOG');
  36. // 获取所有用户ID.
  37. $userIds = [];
  38. foreach ($servicelog as $value) {
  39. $kefuInfoData = json_decode($value, true);
  40. $userIds[] = $kefuInfoData['user_id'];
  41. }
  42. $getAccountsField = [
  43. 'id',
  44. 'nick_name',
  45. ];
  46. $getAccountsWhere['id'] = ['in', $userIds];
  47. $getAllAccounts = model('Accounts')->selectAccounts($getAccountsField, $getAccountsWhere);
  48. foreach ($groups as $k => $v) {
  49. $data[$k] = (object) [
  50. 'label' => $v['name'],
  51. ];
  52. $n = 0;
  53. $data[$k]->children = [];
  54. foreach ($kefuInfo as $va) {
  55. $kefuInfoData = json_decode($va, true);
  56. if ($kefuInfoData['group'] == $v['id']) {
  57. $KFstatus = $kefuInfoData['status'] == 1 ? '(在线)'
  58. : ($kefuInfoData['status'] == 2 ? '(隐身)'
  59. : ($kefuInfoData['status'] == 3 ? '(休息)' : '(未知)'));
  60. $data[$k]->children[$n] = (object) [
  61. 'label' => $kefuInfoData['name'].$KFstatus,
  62. ];
  63. $m = 0;
  64. $data[$k]->children[$n]->children = [];
  65. foreach ($servicelog as $val) {
  66. $servicelogData = json_decode($val, true);
  67. if (('KF'.$servicelogData['kf_id']) == $kefuInfoData['id']) {
  68. $found_key = array_search($servicelogData['user_id'], array_column($getAllAccounts, 'id'));
  69. $data[$k]->children[$n]->children[$m] = (object) [
  70. 'label' => $servicelogData['user_name'],
  71. 'user_id' => $servicelogData['user_id'],
  72. 'nick_name' => $getAllAccounts[$found_key]['nick_name'],
  73. 'servicelog_id' => $servicelogData['servicelog_id'],
  74. ];
  75. }
  76. $m++;
  77. }
  78. $n++;
  79. }
  80. }//end foreach
  81. }//end foreach
  82. return json(['code' => 1, 'data' => $data, 'msg' => '成功']);
  83. } catch (\Exception $e) {
  84. return json(['code' => $code, 'data' => [], 'msg' => $msg]);
  85. }//end try
  86. }//end allConversation()
  87. /**
  88. * 获取会话历史
  89. *
  90. * @access public
  91. * @return array JsonString
  92. */
  93. public function historyList()
  94. {
  95. // 验证token.
  96. $tokenStatus = $this->verifyToken();
  97. $code = -2;
  98. $msg = '错误';
  99. if ($tokenStatus === false) {
  100. $msg = 'token错误';
  101. return json(['code' => $code, 'data' => [], 'msg' => $msg]);
  102. }
  103. try {
  104. // 获取用户信息.
  105. $getUserInfo = $this->getUserInfo();
  106. $serviceLogField = [
  107. 'user_id',
  108. 'user_name',
  109. 'user_avatar',
  110. 'user_ip',
  111. 'start_time',
  112. 'end_time',
  113. 'name',
  114. 'website',
  115. 'system',
  116. 'browse',
  117. 'a.status',
  118. 'evaluate_id',
  119. 'intime',
  120. 'servicelog_id',
  121. ];
  122. // 关联信息.
  123. $serviceLogJoin['groups b'] = 'a.group_id = b.id';
  124. $serviceLogWhere['kf_id'] = $getUserInfo->id;
  125. // 分页.
  126. $currentPage = input('post.currentPage', '1');
  127. $pageSize = input('post.pageSize', '10');
  128. $start = input('post.start');
  129. $end = input('post.end');
  130. $userName = input('post.user_name');
  131. $startTime = strtotime(date('Y-m-d').'-6 day');
  132. $endTime = strtotime(date('Y-m-d').'+1 day');
  133. if (strlen($start) && strlen($end) ) {
  134. $startTime = strtotime($start);
  135. $endTime = strtotime($end.'+1 day');
  136. }
  137. if (strlen($userName)) {
  138. $serviceLogWhere['user_name'] = ['like', '%' . $userName . '%'];
  139. }
  140. $serviceLogWhere['start_time'] = [
  141. 'between',
  142. [
  143. $startTime,
  144. $endTime,
  145. ],
  146. ];
  147. $offset = (($currentPage - 1) * $pageSize);
  148. // 获取用户信息.
  149. $serviceLog = model('ServiceLog')->selectServiceLog(
  150. $serviceLogField,
  151. $offset,
  152. $pageSize,
  153. $serviceLogWhere,
  154. $serviceLogJoin
  155. );
  156. $countServiceLog = model('ServiceLog')->countServiceLog($serviceLogWhere);
  157. $evaluate = model('Evaluate')->getEvaluate();
  158. foreach ($serviceLog as $k => $v) {
  159. foreach ($evaluate as $va) {
  160. if ($v->evaluate_id == $va->evaluate_id) {
  161. $serviceLog[$k]->evaluate_name = $va->evaluate_name;
  162. }
  163. }
  164. }
  165. $result['total'] = $countServiceLog;
  166. $result['countPage'] = (ceil(($result['total']) / $pageSize));
  167. $result['currentPage'] = $currentPage;
  168. $result['list'] = $serviceLog;
  169. $result['pageSize'] = $pageSize;
  170. return json(['code' => 1, 'data' => $result, 'msg' => '成功']);
  171. } catch (\Exception $e) {
  172. return json(['code' => $code, 'data' => [], 'msg' => $msg]);
  173. }//end try
  174. }//end historyList()
  175. /**
  176. * 获取会话历史详细
  177. *
  178. * @access public
  179. * @return array JsonString
  180. */
  181. public function historyInfo()
  182. {
  183. // 验证token.
  184. $tokenStatus = $this->verifyToken();
  185. $code = -2;
  186. $msg = '错误';
  187. if ($tokenStatus === false) {
  188. $msg = 'token错误';
  189. return json(['code' => $code, 'data' => [], 'msg' => $msg]);
  190. }
  191. try {
  192. // 获取用户信息.
  193. $servicelogId = input('get.servicelog_id');
  194. $chatLogField = ['*'];
  195. // 关联信息.
  196. $chatLogWhere['servicelog_id'] = $servicelogId;
  197. // 分页.
  198. $currentPage = input('get.currentPage', '1');
  199. $pageSize = input('get.pageSize', '10');
  200. $offset = (($currentPage - 1) * $pageSize);
  201. // 获取用户信息.
  202. $chatLog = model('ChatLog')->selectChatLog($chatLogField, $offset, $pageSize, $chatLogWhere);
  203. $countChatLog = model('ChatLog')->countChatLog($chatLogWhere);
  204. // 查询报警信息.
  205. $alarmField = [
  206. 'evaluate_id',
  207. 'alarm_corresponding',
  208. 'alarm_cvtOvertime',
  209. 'alarm_userSensitive',
  210. 'alarm_serverSensitive',
  211. 'alarm_respond',
  212. ];
  213. $alarmWhere['b.servicelog_id'] = $servicelogId;
  214. $serviceLogJoin['service_log b'] = 'a.servicelog_id = b.servicelog_id';
  215. $alarm = model('Alarm')->findAlarm($alarmField, $alarmWhere, $serviceLogJoin);
  216. // 查询系统设置.
  217. $systemconfig = model('Systemconfig')->selectSystemconfig($alarmField, $alarmWhere, $serviceLogJoin);
  218. // 响应超时.
  219. $verifyReturnTime = 0;
  220. // 会话超时.
  221. $cvtOvertime = 0;
  222. foreach ($systemconfig as $v) {
  223. if ($v['systemconfig_enName'] == 'verifyReturnTime') {
  224. if ($v['systemconfig_data'] < $alarm['alarm_corresponding']) {
  225. $verifyReturnTime = 1;
  226. }
  227. }
  228. if ($v['systemconfig_enName'] == 'verifyAllTime') {
  229. if ($v['systemconfig_data'] < $alarm['alarm_cvtOvertime']) {
  230. $cvtOvertime = 1;
  231. }
  232. }
  233. }
  234. $service_log = db('service_log')->where('servicelog_id',$servicelogId)->find();
  235. $account = db('accounts')
  236. ->alias('a')
  237. ->join('accountslabel b', 'a.label_id = b.id')
  238. ->field('a.id,account_name,nick_name,account_email,account_phone,address,remark,name as label')
  239. ->where('a.id',$service_log['user_id'])
  240. ->find();
  241. $account['user_ip'] = $service_log['user_ip'];
  242. $account['system'] = $service_log['system'];
  243. $account['browse'] = $service_log['browse'];
  244. $result['total'] = $countChatLog;
  245. $result['countPage'] = (ceil(($result['total']) / $pageSize));
  246. $result['currentPage'] = $currentPage;
  247. $result['list'] = $chatLog;
  248. $result['pageSize'] = $pageSize;
  249. $result['account'] = $account;
  250. $result['alarm'] = [
  251. 'evaluate_id' => $alarm['evaluate_id'],
  252. 'verifyReturnTime' => $verifyReturnTime,
  253. 'cvtOvertime' => $cvtOvertime,
  254. 'serverSensitive' => $alarm['alarm_serverSensitive'] ? 1 : 2,
  255. 'userSensitive' => $alarm['alarm_userSensitive'] ? 1 : 2,
  256. 'respond' => $alarm['alarm_respond'],
  257. ];
  258. return json(['code' => 1, 'data' => $result, 'msg' => '成功']);
  259. } catch (\Exception $e) {
  260. return json(['code' => $code, 'data' => [], 'msg' => $msg]);
  261. }//end try
  262. }//end historyInfo()
  263. /**
  264. * 获取用户会话历史详细
  265. *
  266. * @access public
  267. * @return array JsonString
  268. */
  269. public function userHistory()
  270. {
  271. // 验证token.
  272. $tokenStatus = $this->verifyToken();
  273. $code = -2;
  274. $msg = '错误';
  275. if ($tokenStatus === false) {
  276. $msg = 'token错误';
  277. return json(['code' => $code, 'data' => [], 'msg' => $msg]);
  278. }
  279. try {
  280. // 获取用户ID.
  281. $accountId = input('get.account_id');
  282. $chatLogField = ['*'];
  283. // 关联信息.
  284. $chatLogWhere['from_id'] = $accountId;
  285. $chatLogWhereOr['to_id'] = $accountId;
  286. // 分页.
  287. $currentPage = input('get.currentPage', '1');
  288. $pageSize = input('get.pageSize', '10');
  289. $offset = (($currentPage - 1) * $pageSize);
  290. // 获取用户信息.
  291. $chatLog = model('ChatLog')->userChatLog(
  292. $chatLogField,
  293. $chatLogWhere,
  294. $chatLogWhereOr,
  295. $offset,
  296. $pageSize
  297. );
  298. $countChatLog = model('ChatLog')->userChatLogCount($chatLogWhere, $chatLogWhereOr);
  299. $result['total'] = $countChatLog;
  300. $result['countPage'] = (ceil(($result['total']) / $pageSize));
  301. $result['currentPage'] = $currentPage;
  302. $result['list'] = $chatLog;
  303. $result['pageSize'] = $pageSize;
  304. return json(['code' => 1, 'data' => $result, 'msg' => '成功']);
  305. } catch (\Exception $e) {
  306. return json(['code' => $code, 'data' => [], 'msg' => $msg]);
  307. }//end try
  308. }//end userHistory()
  309. }