Kefu.php 17 KB

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