Service.php 5.1 KB

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