ChatController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\goods\classes\GoodsClass;
  4. use bizHd\goods\services\GoodsService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\message\services\ChatService;
  7. use bizHd\user\services\UserService;
  8. use common\components\business;
  9. use Yii;
  10. use common\components\util;
  11. use GatewayClient\Gateway;
  12. class ChatController extends BaseController
  13. {
  14. //聊天 ssh 2019.12.28
  15. public function actionIndex()
  16. {
  17. $get = Yii::$app->request->get();
  18. $userId = isset($get['userId']) ? $get['userId'] : 0;
  19. $user = UserService::getById($userId);
  20. UserService::valid($user, $this->sjId);
  21. return $this->renderPartial('index', ['userId' => $userId]);
  22. }
  23. //聊天双方的用户信息 ssh 2019.12.30
  24. public function actionBegin()
  25. {
  26. $id = Yii::$app->request->get('id', 0);
  27. $user = UserService::getUserInfo($id);
  28. UserService::valid($user, $this->sjId);
  29. //新增最近联系人
  30. ChatService::addRecentlyUser($this->sjId, $id);
  31. //获取最近联系人
  32. $recentlyUser = ChatService::getRecentlyUserList($this->sjId);
  33. util::success(['merchant' => $this->sj->attributes, 'user' => $user, 'recentlyUser' => $recentlyUser]);
  34. }
  35. //绑定 ssh 2019.12.28
  36. public function actionBindMerchant()
  37. {
  38. $post = Yii::$app->request->post();
  39. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  40. $id = 'merchantChat_' . $this->sjId;
  41. Gateway::bindUid($clientId, $id);
  42. //连接就将新消息置为已读
  43. $consoleId = 'merchantConsole_' . $this->sjId;
  44. $consoleOnline = Gateway::getClientIdByUid($consoleId);
  45. if ($consoleOnline) {
  46. $data = ['type' => 'newMessage', 'status' => 0];
  47. Gateway::sendToUid($consoleId, json_encode($data));
  48. }
  49. }
  50. //关闭当前链接
  51. public function actionClose()
  52. {
  53. $post = Yii::$app->request->post();
  54. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  55. Gateway::closeClient($clientId);
  56. }
  57. public function actionBindConsole()
  58. {
  59. $post = Yii::$app->request->post();
  60. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  61. $id = 'merchantConsole_' . $this->sjId;
  62. Gateway::bindUid($clientId, $id);
  63. }
  64. //发消息给客户 ssh 2019.12.28
  65. public function actionSend()
  66. {
  67. $post = Yii::$app->request->post();
  68. $type = isset($post['type']) ? $post['type'] : 'text';
  69. $userId = isset($post['userId']) ? $post['userId'] : 0;
  70. $user = UserService::getById($userId);
  71. UserService::valid($user, $this->sjId);
  72. $arr = [];
  73. $arr['type'] = $type;
  74. $arr['source'] = 'merchant';
  75. $arr['time'] = '12:51';
  76. switch ($type) {
  77. case 'text':
  78. //文字
  79. $content = $post['content'];
  80. $arr['content'] = $content;
  81. break;
  82. case 'img':
  83. //图片
  84. $shortUrl = $post['shortUrl'];
  85. $return = business::formatUploadImg($shortUrl);
  86. $arr['url'] = $return['url'];
  87. $arr['smallUrl'] = $return['smallUrl'];
  88. break;
  89. case 'link':
  90. //常用链接
  91. $id = $post['linkId'];
  92. $respond = MerchantService::getCommonUrl($id);
  93. $arr['name'] = $respond['name'];
  94. $arr['img'] = $respond['img'];
  95. $arr['url'] = $respond['url'];
  96. break;
  97. case 'goods':
  98. //商品
  99. $id = $post['goodsId'];
  100. $info = GoodsClass::getGoodsInfo($id);
  101. GoodsService::valid($info, $this->mainId);
  102. $arr['name'] = $info['name'];
  103. $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
  104. $arr['price'] = isset($info['price']) ? $info['price'] : 300;
  105. break;
  106. }
  107. $data[] = $arr;
  108. //放入缓存
  109. ChatService::saveMessage($data, $userId);
  110. $online = Gateway::getClientIdByUid($userId);
  111. if ($online) {
  112. //直接发送
  113. Gateway::sendToUid($userId, json_encode($data));
  114. util::success($data);
  115. }
  116. util::success($data);
  117. }
  118. //最近联系人 ssh 2019.12.29
  119. public function actionRecentlyUser()
  120. {
  121. $respond = ChatService::getRecentlyUserList($this->mainId);
  122. util::success($respond);
  123. }
  124. //最近聊天的内容 ssh 2019.12.29
  125. public function actionRecentlyMessage()
  126. {
  127. //商品 图片 快捷链接 文字
  128. $id = Yii::$app->request->get('id');
  129. $info = UserService::getById($id);
  130. UserService::valid($info, $this->sjId);
  131. $respond = ChatService::getMessageList($id);
  132. util::success($respond);
  133. }
  134. }