ChatController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace ghs\controllers;
  3. use bizHd\goods\services\GoodsService;
  4. use biz\sj\services\MerchantService;
  5. use bizHd\message\services\ChatService;
  6. use bizHd\user\services\UserService;
  7. use common\components\business;
  8. use Yii;
  9. use common\components\util;
  10. use GatewayClient\Gateway;
  11. class ChatController extends BaseController
  12. {
  13. //聊天 ssh 2019.12.28
  14. public function actionIndex()
  15. {
  16. $get = Yii::$app->request->get();
  17. $userId = isset($get['userId']) ? $get['userId'] : 0;
  18. $user = UserService::getById($userId);
  19. UserService::valid($user, $this->sjId);
  20. return $this->renderPartial('index', ['userId' => $userId]);
  21. }
  22. //聊天双方的用户信息 ssh 2019.12.30
  23. public function actionBegin()
  24. {
  25. $id = Yii::$app->request->get('id', 0);
  26. $user = UserService::getUserInfo($id);
  27. UserService::valid($user, $this->sjId);
  28. //新增最近联系人
  29. ChatService::addRecentlyUser($this->sjId, $id);
  30. //获取最近联系人
  31. $recentlyUser = ChatService::getRecentlyUserList($this->sjId);
  32. util::success(['merchant' => $this->sj->attributes, 'user' => $user, 'recentlyUser' => $recentlyUser]);
  33. }
  34. //绑定 ssh 2019.12.28
  35. public function actionBindMerchant()
  36. {
  37. $post = Yii::$app->request->post();
  38. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  39. $id = 'merchantChat_' . $this->sjId;
  40. Gateway::bindUid($clientId, $id);
  41. //连接就将新消息置为已读
  42. $consoleId = 'merchantConsole_' . $this->sjId;
  43. $consoleOnline = Gateway::getClientIdByUid($consoleId);
  44. if ($consoleOnline) {
  45. $data = ['type' => 'newMessage', 'status' => 0];
  46. Gateway::sendToUid($consoleId, json_encode($data));
  47. }
  48. }
  49. //关闭当前链接
  50. public function actionClose()
  51. {
  52. $post = Yii::$app->request->post();
  53. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  54. Gateway::closeClient($clientId);
  55. }
  56. public function actionBindConsole()
  57. {
  58. $post = Yii::$app->request->post();
  59. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  60. $id = 'merchantConsole_' . $this->sjId;
  61. Gateway::bindUid($clientId, $id);
  62. }
  63. //最近联系人 ssh 2019.12.29
  64. public function actionRecentlyUser()
  65. {
  66. $respond = ChatService::getRecentlyUserList($this->sjId);
  67. util::success($respond);
  68. }
  69. //最近聊天的内容 ssh 2019.12.29
  70. public function actionRecentlyMessage()
  71. {
  72. //商品 图片 快捷链接 文字
  73. $id = Yii::$app->request->get('id');
  74. $info = UserService::getById($id);
  75. UserService::valid($info, $this->sjId);
  76. $respond = ChatService::getMessageList($id);
  77. util::success($respond);
  78. }
  79. }