Kefu.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\admin\model\Admin;
  5. use app\admin\model\Sign;
  6. use app\admin\model\KefuWork;
  7. use app\admin\model\User;
  8. use app\admin\model\UserView;
  9. use app\admin\model\KefuTime;
  10. use app\admin\model\OperationData;
  11. use app\admin\model\Department;
  12. use app\admin\model\Config;
  13. use app\admin\model\Score;
  14. use app\admin\model\KefuLog;
  15. use app\enterprise\model\{Message,Friend};
  16. use Exception;
  17. use think\facade\Db;
  18. class Kefu extends BaseController
  19. {
  20. //当前数据统计
  21. public function statistics()
  22. {
  23. // 统计
  24. $data['visit_count'] = UserView::count(); //当前客服页访问量
  25. $data['online_count'] = User::where('role',3)->where('is_online', '>', 0)->count(); //当前客服在线人数
  26. $data['handling_count'] = User::where('service_status',2)->count(); //当前客服接线中的总数
  27. $data['today_handling_count'] = KefuTime::where('type', 3)->where('created_at', '>=', date('Y-m-d'))->count(); //今日客服接线总数
  28. return $this->success($data);
  29. }
  30. /**
  31. * @api {get} /today 今日在线客服列表
  32. */
  33. public function today()
  34. {
  35. try {
  36. $params = $this->request->param();
  37. $page = $params['page'] ?? 1;
  38. $limit = $params['limit'] ?? 15;
  39. $query = KefuWork::alias('kefu_work')
  40. ->join('user', 'kefu_work.admin_id = user.uid', 'left')
  41. ->where('kefu_work.created_at', '>=', date('Y-m-d 00:00:00'))
  42. ->where('user.role', 3);
  43. if (isset($params['is_online']) && $params['is_online'] != '') {
  44. $query->where('user.is_online', $params['is_online']);
  45. }
  46. $count = $query->count();
  47. $list = $query->field([' kefu_work.id', 'user.realname as nickname','user.is_online', 'user.chat_num','kefu_work.transfer_num','kefu_work.intervention_num','kefu_work.completed_num','kefu_work.created_at'])
  48. ->order('user.is_online', 'desc')
  49. ->limit($limit)
  50. ->page($page)
  51. ->select();
  52. foreach ($list as &$value) {
  53. $value['chat_num'] = intval($value['chat_num']);//当前接线数
  54. $value['transfer_num'] = intval($value['transfer_num']);
  55. $value['intervention_num'] = intval($value['intervention_num']);
  56. $value['completed_num'] = intval($value['completed_num']);
  57. $value['inprocess'] = User::where('cs_uid',$value['id'])->where('service_status',2)->count();//进行中
  58. $value['unprocessed'] = User::where('cs_uid',$value['id'])->where('service_status',1)->count();//待处理
  59. }
  60. } catch (Exception $e) {
  61. return $this->error($e->getMessage());
  62. }
  63. return $this->success(['count' => $count, 'list' => $list]);
  64. }
  65. /**
  66. * @api {get} /list 客服数据展板列表
  67. */
  68. public function list()
  69. {
  70. try {
  71. $params = $this->request->param();
  72. $page = $params['page'] ?? 1;
  73. $limit = $params['limit'] ?? 15;
  74. $order_field = $params['field'] ?? 'kefu_work.id';
  75. $order = $params['order'] ?? 'desc';
  76. $query = KefuWork::alias('kefu_work')
  77. ->join('admin', 'kefu_work.admin_id = admin.id', 'left')
  78. ->join('user', 'kefu_work.admin_id = user.uid', 'left')
  79. ->where('user.from', 0);
  80. if (isset($params['is_online'])) {
  81. $query = $query->where('user.is_online', $params['is_online']);
  82. }
  83. if (!empty($params['start_time'])) {
  84. $query = $query->where('kefu_work.created_at', '>=', $params['start_time'].' 00:00:00');
  85. }
  86. if (!empty($params['end_time'])) {
  87. $query = $query->where('kefu_work.created_at', '<=', $params['end_time'].' 23:59:59');
  88. }
  89. $count = $query->count();
  90. $list = $query->field(['admin.username','admin.nickname','user.is_online', 'kefu_work.*'])
  91. ->order($order_field, $order)
  92. ->order('kefu_work.id', 'desc')
  93. ->limit($limit)
  94. ->page($page)
  95. ->select();
  96. $kefu_time = KefuTime::where('admin_id', $this->admin_id)->where('status', 0)->column('start_time', 'type');
  97. foreach ($list as &$value) {
  98. if (isset($kefu_time[4])) {
  99. $value['service_time'] += time() - $kefu_time[4];
  100. }
  101. if (isset($kefu_time[3])) {
  102. $value['chat_time'] += time() - $kefu_time[3];
  103. }
  104. if (isset($kefu_time[1])) {
  105. $value['busy_time'] += time() - $kefu_time[1];
  106. }
  107. $value['avg_time'] = $value['service_time'] && $value['chat_num'] ? intval($value['service_time'] / $value['chat_num']) : 0; //客服平均服务时间
  108. $value['online_time'] = formatSecondsToTime($value['online_time']);
  109. $value['busy_time'] = formatSecondsToTime($value['busy_time']);
  110. $value['chat_time'] = formatSecondsToTime($value['chat_time']);
  111. $value['service_time'] = formatSecondsToTime($value['service_time']);
  112. }
  113. } catch (Exception $e) {
  114. return $this->error($e->getMessage());
  115. }
  116. return $this->success(['count' => $count, 'list' => $list]);
  117. }
  118. /**
  119. * @api {get} /chat 总接线数据
  120. */
  121. public function chat()
  122. {
  123. try {
  124. $params = $this->request->param();
  125. $start_time = empty($params['start_time']) ? date('Y-m-d') : $params['start_time'];
  126. $end_time = empty($params['end_time']) ? date('Y-m-d') : $params['end_time'];
  127. $list = OperationData::where('type', 1)
  128. ->where('date', '>=', $start_time)
  129. ->where('date', '<=', $end_time)
  130. ->limit(100)
  131. ->field(['num','date'])
  132. ->select();
  133. if ($start_time == date('Y-m-d') || $end_time == date('Y-m-d')) {
  134. $list[] = [
  135. 'num' => KefuTime::where('type', 3)
  136. ->where('created_at', '>=', date('Y-m-d 00:00:00'))
  137. ->where('created_at', '<=', date('Y-m-d 23:59:59'))
  138. ->count(),
  139. 'date' => date('Y-m-d'),
  140. ];
  141. }
  142. } catch (Exception $e) {
  143. return $this->error($e->getMessage());
  144. }
  145. return $this->success(['count' => count($list), 'list' => $list]);
  146. }
  147. //客服签到
  148. public function sign()
  149. {
  150. try {
  151. $admin_id = $this->admin_id;
  152. KefuWork::addNum($admin_id, 'sign_num');//客服签到次数更新
  153. $sign = Sign::where('admin_id', $admin_id)->where('created_at', '>=', date('Y-m-d'))->order('id', 'desc')->find();
  154. if ($sign && $sign->time == 0) {
  155. $sign->time = time() - strtotime($sign->created_at);
  156. $sign->updated_at = date('Y-m-d H:i:s');
  157. $sign->save();
  158. }
  159. //签到记录
  160. Sign::create([
  161. 'admin_id' => $admin_id,
  162. ]);
  163. } catch (\Exception $e) {
  164. return $this->error($e->getMessage());
  165. }
  166. //通知客服已签到
  167. $user_id = User::getCsId($admin_id);
  168. wsSendMsg($user_id,'sign',['is_sign'=>0]);
  169. //上线通知
  170. wsSendMsg(0,'isOnline',['id'=>$user_id, 'is_online'=>1]);
  171. return $this->success([], '签到成功');
  172. }
  173. /**
  174. * 客服接线
  175. */
  176. public function handleChat()
  177. {
  178. try {
  179. Db::startTrans();
  180. $user_id = $this->request->param('user_id');
  181. $user = User::where('user_id', $user_id)->find();
  182. if (!$user) {
  183. return $this->error('用户不存在');
  184. }
  185. if ($user->service_status >= 1) {
  186. return $this->error('用户已接线');
  187. }
  188. $cs_uid = User::getCsId($this->admin_id);
  189. $user->cs_uid = $cs_uid;
  190. $user->service_status = 2;
  191. $user->service_start = time();
  192. $user->timeout_type = 0;
  193. $user->save();
  194. //更新客服接线数量
  195. User::where('user_id', $cs_uid)->update(['chat_num'=>Db::raw('chat_num+1')]);
  196. //客服接线次数更新
  197. KefuWork::addNum($this->admin_id, 'chat_num');
  198. //客服对接记录表
  199. KefuLog::addData($cs_uid, $user_id, 1);
  200. $friend = Friend::where('create_user', $user_id)->order('create_time', 'desc')->find();
  201. if ($friend) {
  202. $robot_id = $friend->friend_user_id;
  203. $friend->friend_user_id = $cs_uid;
  204. $friend->save();
  205. $chat_identify = $cs_uid . '-' . $user_id;
  206. Message::where(['from_user' => $user_id, 'to_user' => $robot_id])->update(['to_user' => $cs_uid, 'chat_identify' => $chat_identify, 'is_read' => 1]);
  207. Message::where(['from_user' => $robot_id, 'to_user' => $user_id])->update(['from_user' => $cs_uid, 'chat_identify' => $chat_identify, 'is_read' => 1]);
  208. }
  209. Db::commit();
  210. //通知客服已接线
  211. wsSendMsg(0,'handleChat',['user_id'=>$user_id]);
  212. } catch (\Exception $e) {
  213. Db::rollback();
  214. return $this->error($e->getMessage());
  215. }
  216. return $this->success([], '');
  217. }
  218. /**
  219. * 客服转线
  220. */
  221. public function transferChat()
  222. {
  223. try {
  224. Db::startTrans();
  225. $user_id = $this->request->param('user_id');
  226. $cs_uid = $this->request->param('cs_uid');
  227. $user = User::where('user_id', $user_id)->find();
  228. if (!$user) {
  229. return $this->error('用户不存在');
  230. }
  231. $old_cs_uid = $user->cs_uid;
  232. $admin_id = User::getAdminId($cs_uid);
  233. $user->cs_uid = $cs_uid;
  234. $user->service_status = 1;
  235. $user->service_start = time();
  236. $user->timeout_type = 0;
  237. $user->save();
  238. //更新客服接线数量
  239. User::where('user_id', $cs_uid)->update(['chat_num'=>Db::raw('chat_num+1')]);
  240. //客服接线次数更新
  241. KefuWork::addNum($admin_id, 'chat_num');
  242. //客服对接记录表
  243. KefuLog::endData($old_cs_uid, $user_id);
  244. //客服对接记录表
  245. KefuLog::addData($cs_uid, $user_id, 2);
  246. $friend = Friend::where('create_user', $user_id)->order('create_time', 'desc')->find();
  247. if ($friend) {
  248. $friend->friend_user_id = $cs_uid;
  249. $friend->save();
  250. $chat_identify = $cs_uid . '-' . $user_id;
  251. Message::where(['from_user' => $user_id, 'to_user' => $old_cs_uid])->update(['to_user' => $cs_uid, 'chat_identify' => $chat_identify, 'is_read' => 1]);
  252. Message::where(['from_user' => $old_cs_uid, 'to_user' => $user_id])->update(['from_user' => $cs_uid, 'chat_identify' => $chat_identify, 'is_read' => 1]);
  253. KefuTime::endData($admin_id, 3, $old_cs_uid); //结束接线时间
  254. }
  255. Db::commit();
  256. //通知关闭聊天框
  257. wsSendMsg($old_cs_uid,'closeChat',['user_id'=>$user_id]);
  258. //通知客服已接线
  259. wsSendMsg(0,'handleChat',['user_id'=>$user_id]);
  260. } catch (\Exception $e) {
  261. Db::rollback();
  262. return $this->error($e->getMessage());
  263. }
  264. return $this->success([], '');
  265. }
  266. /**
  267. * 选择上级及部门一下的其他客服
  268. */
  269. public function select()
  270. {
  271. // $department_id = Admin::where('id', $this->admin_id)->value('department_id');
  272. // $parent_department_id = Department::where('id', $department_id)->value('parent_id');
  273. // $where[] = ['department_id', '=', $parent_department_id];
  274. $cs_uids = Department::getDepartmentCsUids($this->admin_id);
  275. $list = Admin::alias('admin')->join('user', 'admin.id = user.uid', 'left')
  276. ->where('user.role', '>', 0)
  277. ->where('user.status', 1)
  278. // ->where('admin.department_id', $department_id)->whereOr(function ($query) use ($parent_department_id) {
  279. // if ($parent_department_id) {
  280. // $query->where('admin.department_id', $parent_department_id);
  281. // }
  282. // })
  283. ->where('admin.id','<>', $this->admin_id)
  284. ->whereIn('user.user_id', $cs_uids)
  285. ->order('user.is_online', 'desc')
  286. ->order('admin.department_id', 'asc')
  287. ->field(['admin.id','user.user_id','user.account','user.realname','user.avatar','user.is_online','admin.nickname','admin.username'])
  288. ->select()
  289. ->toArray();
  290. return $this->success($list);
  291. }
  292. /**
  293. * 客服结束会话
  294. */
  295. public function finishedChat()
  296. {
  297. try {
  298. Db::startTrans();
  299. $user_id = $this->request->param('user_id');
  300. $user = User::where('user_id', $user_id)->where('role', 0)->find();
  301. if (!$user) {
  302. return $this->error('用户不存在');
  303. }
  304. if ($user->service_status != 3) {
  305. KefuTime::endData($user->uid, 3, $user->cs_uid); //结束接线时间
  306. $old_cs_uid = $user->cs_uid;
  307. //转成机器人聊天
  308. $user->service_status = -1;
  309. $user->service_time = 0;
  310. $user->timeout_type = 0;
  311. $user->cs_uid = getAutoCsUid();//获取机器人ID
  312. $user->save();
  313. $cs_uid = $user->cs_uid;
  314. $friend = Friend::where('create_user', $user_id)->order('create_time', 'desc')->find();
  315. if ($friend) {
  316. $old_cs_uid = $friend->friend_user_id;
  317. $friend->friend_user_id = $cs_uid;
  318. $friend->save();
  319. $chat_identify = $cs_uid . '-' . $user_id;
  320. Message::where(['from_user' => $user_id, 'to_user' => $old_cs_uid])->update(['to_user' => $cs_uid, 'chat_identify' => $chat_identify, 'is_read' => 1]);
  321. Message::where(['from_user' => $old_cs_uid, 'to_user' => $user_id])->update(['from_user' => $cs_uid, 'chat_identify' => $chat_identify, 'is_read' => 1]);
  322. }
  323. //客服对接记录表
  324. KefuLog::endData($old_cs_uid, $user_id);
  325. //客服对接记录表
  326. KefuLog::addData($cs_uid, $user_id, 3);
  327. Db::commit();
  328. //通知关闭聊天框
  329. wsSendMsg($old_cs_uid,'closeChat',['user_id'=>$user_id]);
  330. //通知客服已结束
  331. wsSendMsg(0,'handleChat',['user_id'=>$user_id]);
  332. //给用户发送客服评分的消息
  333. $user_open_comment = Config::getFieldValue('user_open_comment');
  334. if ($user_open_comment == 1 ) {
  335. $param = [
  336. 'id' => \utils\Str::getUuid(),
  337. 'type' => 'score',
  338. 'status' => 'going',
  339. 'sendTime' => time() * 1000,
  340. 'toContactId' => $user_id,
  341. 'content' => Config::getFieldValue('kefu_finished_chat'),
  342. 'file_id' => 0,
  343. 'is_group' => 0,
  344. 'user_id' => $cs_uid,
  345. 'extends' => json_encode([
  346. 'cs_uid' => $old_cs_uid,
  347. ]),
  348. ];
  349. Message::sendMsg($param, 0);
  350. }
  351. }
  352. } catch (\Exception $e) {
  353. Db::rollback();
  354. return $this->error($e->getMessage());
  355. }
  356. return $this->success([], '');
  357. }
  358. /**
  359. * @api {get} 客服评分列表
  360. */
  361. public function score()
  362. {
  363. try {
  364. $params = $this->request->param();
  365. $page = $params['page'] ?? 1;
  366. $limit = $params['limit'] ?? 15;
  367. $cs_uid = $params['user_id'] ?? User::getCsId($this->admin_id);
  368. $query = Score::alias('score')
  369. ->join('user', 'score.user_id = user.user_id', 'left')
  370. ->where('score.cs_uid', $cs_uid);
  371. $count = $query->count();
  372. $list = $query->field(['score.*','user.realname'])
  373. ->order('id', 'desc')
  374. ->limit($limit)
  375. ->page($page)
  376. ->select();
  377. } catch (Exception $e) {
  378. return $this->error($e->getMessage());
  379. }
  380. return $this->success(['count' => $count, 'list' => $list]);
  381. }
  382. /**
  383. * @api {get} 客服对接记录表
  384. */
  385. public function log()
  386. {
  387. try {
  388. $params = $this->request->param();
  389. $page = $params['page'] ?? 1;
  390. $limit = $params['limit'] ?? 15;
  391. $keyword = $params['keyword'] ?? '';
  392. $cs_name = $params['cs_name'] ?? '';
  393. $query = KefuLog::alias('log')
  394. ->join('user', 'log.user_id = user.user_id', 'left')
  395. ->join('user as kefu', 'log.cs_uid = kefu.user_id', 'left');
  396. if ($keyword) {
  397. $query = $query->where('user.realname|user.user_id', 'like', "%{$keyword}%");
  398. }
  399. if ($cs_name) {
  400. $query = $query->where('kefu.realname|user.user_id', 'like', "%{$cs_name}%");
  401. }
  402. $count = $query->count();
  403. $list = $query->field(['log.*','user.realname','user.avatar','kefu.realname as cs_name'])
  404. ->order('id', 'desc')
  405. ->limit($limit)
  406. ->page($page)
  407. ->select();
  408. } catch (Exception $e) {
  409. return $this->error($e->getMessage());
  410. }
  411. return $this->success(['count' => $count, 'list' => $list]);
  412. }
  413. }