Service.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\home\controller;
  3. use BM\Unit\Pusher;
  4. use BM\Model\Chat;
  5. use BM\Model\ChatList;
  6. use BM\Model\ChatMessage;
  7. use BM\Model\Reply;
  8. class Service extends AdminControl
  9. {
  10. /**
  11. * @客服聊天界面
  12. */
  13. public function index()
  14. {
  15. //通讯节点
  16. $node_info = [
  17. 'address' => !empty(config('GATEWAY_NODE')) ? config('GATEWAY_NODE') : '',
  18. ];
  19. //第三方平台信息
  20. $site_info = [
  21. 'code' => 'kf_100001_1234566121',
  22. ];
  23. //当前聊天列表
  24. $res = ChatList::instance()->getChatList(['sid' => $this->admin_info['service_id']]);
  25. $chat_list = $res['status'] == 1 ? $res['data'] : [];
  26. //客服常用语
  27. $where = [
  28. 'product_companyid' => $this->admin_info['service_pid'],
  29. 'product_type' => 3,
  30. 'product_status' => 1,
  31. '_fields_' => [
  32. 'product_title AS reply_content'
  33. ],
  34. ];
  35. $replys = Reply::instance()->lists($where);
  36. $data = [
  37. 'node' => $node_info,
  38. 'site' => $site_info,
  39. 'chat_list' => $chat_list,
  40. 'replys' => $replys,
  41. ];
  42. $this->assign('data', $data);
  43. return $this->fetch();
  44. }
  45. /**
  46. * @绑定客服
  47. */
  48. public function bind()
  49. {
  50. $client_id = !empty($this->request->param('client_id')) ? $this->request->param('client_id') : '';
  51. if (empty($client_id)) {
  52. return ['status' => 0, 'msg' => 'error[expect client_id]'];
  53. }
  54. //绑定
  55. Pusher::instance()->bindUid($client_id, $this->admin_info['service_id']);
  56. return [
  57. 'status' => 200,
  58. 'msg' => 'success'
  59. ];
  60. }
  61. /**
  62. * @聊天
  63. */
  64. public function chat()
  65. {
  66. $to_id = !empty($this->request->param('to_id')) ? $this->request->param('to_id') : '';
  67. $content= !empty($this->request->param('content')) ? $this->request->param('content') : '';
  68. $type = !empty($this->request->param('type')) ? intval($this->request->param('type')) : ChatMessage::MESSAGE_TYPE_NORMAL;
  69. //获取聊天
  70. $chat = ChatList::instance()->info(['sid' => $this->admin_info['service_id'], 'vid' => $to_id, '_fields_' => 'log_id']);
  71. if (empty($chat)) {
  72. return ['status' => 0, 'msg' => 'fail["chat data not exist"]'];
  73. }
  74. $param = [
  75. 'log_id' => $chat['log_id'],
  76. 'uid' => $this->admin_info['service_pid'],
  77. 'to_id' => $to_id,//发送客户端
  78. 'from_id' => $this->admin_info['service_id'],//来源客户端
  79. 'type' => $type,//消息类型
  80. 'content' => $content,//内容
  81. ];
  82. $res = Chat::instance()->sendMessage($param);
  83. if ($res['status'] != 1) {
  84. return $res;
  85. }
  86. return [
  87. 'status' => 200,
  88. 'msg' => 'success'
  89. ];
  90. }
  91. /**
  92. * @消息记录
  93. */
  94. public function messageLog()
  95. {
  96. $to_id = !empty($this->request->param('to_id')) ? $this->request->param('to_id') : '';
  97. $page = !empty($this->request->param('page')) ? intval($this->request->param('page')) : 1;
  98. $page_size = 50;
  99. if (empty($to_id)) {
  100. return [
  101. 'status' => 0,
  102. 'msg' => '获取消息记录失败[无用户信息]',
  103. ];
  104. }
  105. $param = [
  106. 'sid' => $this->admin_info['service_id'],
  107. 'vid' => $to_id,
  108. 'page' => $page,
  109. 'page_size' => $page_size,
  110. ];
  111. $res = ChatMessage::instance()->messageLog($param);
  112. if ($res['status'] != 1) {
  113. return $res;
  114. }
  115. return [
  116. 'status' => 200,
  117. 'msg' => 'success',
  118. 'data' => $res['data'],
  119. ];
  120. }
  121. /**
  122. * @删除访问列表
  123. */
  124. public function delChatList()
  125. {
  126. $to_id = !empty($this->request->param('to_id')) ? $this->request->param('to_id') : '';
  127. if (empty($to_id)) {
  128. return [
  129. 'status' => 0,
  130. 'msg' => 'fail[expect to_id]'
  131. ];
  132. }
  133. $where = [
  134. 'sid' => $this->admin_info['service_id'],
  135. 'vid' => $to_id,
  136. ];
  137. ChatList::instance()->del($where);
  138. //断开连接
  139. $client_lists = Pusher::instance()->getClientIdByUid($to_id);
  140. $message = [
  141. 'method' => 'delVisitor',
  142. 'data' => [
  143. 'time' => date('Y-m-d H:i:s'),
  144. ],
  145. ];
  146. foreach ($client_lists as $item) {
  147. Pusher::instance()->closeClient($item, json_encode($message));
  148. }
  149. return [
  150. 'status' => 200,
  151. 'msg' => 'success'
  152. ];
  153. }
  154. }