ChatController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace hd\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. //聊天 shish 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. //聊天双方的用户信息 shish 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, 'user' => $user, 'recentlyUser' => $recentlyUser]);
  33. }
  34. //绑定 shish 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. //发消息给客户 shish 2019.12.28
  64. public function actionSend()
  65. {
  66. $post = Yii::$app->request->post();
  67. $type = isset($post['type']) ? $post['type'] : 'text';
  68. $userId = isset($post['userId']) ? $post['userId'] : 0;
  69. $user = UserService::getById($userId);
  70. UserService::valid($user, $this->sjId);
  71. $arr = [];
  72. $arr['type'] = $type;
  73. $arr['source'] = 'merchant';
  74. $arr['time'] = '12:51';
  75. switch ($type) {
  76. case 'text':
  77. //文字
  78. $content = $post['content'];
  79. $arr['content'] = $content;
  80. break;
  81. case 'img':
  82. //图片
  83. $shortUrl = $post['shortUrl'];
  84. $return = business::formatUploadImg($shortUrl);
  85. $arr['url'] = $return['url'];
  86. $arr['smallUrl'] = $return['smallUrl'];
  87. break;
  88. case 'link':
  89. //常用链接
  90. $id = $post['linkId'];
  91. $respond = MerchantService::getCommonUrl($id);
  92. $arr['name'] = $respond['name'];
  93. $arr['img'] = $respond['img'];
  94. $arr['url'] = $respond['url'];
  95. break;
  96. case 'goods':
  97. //商品
  98. $id = $post['goodsId'];
  99. $info = GoodsService::getGoodsInfo($id);
  100. GoodsService::valid($info, $this->sjId);
  101. $arr['goodsName'] = $info['goodsName'];
  102. $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
  103. $arr['price'] = isset($info['price']) ? $info['price'] : 300;
  104. break;
  105. }
  106. $data[] = $arr;
  107. //放入缓存
  108. ChatService::saveMessage($data, $userId);
  109. $online = Gateway::getClientIdByUid($userId);
  110. if ($online) {
  111. //直接发送
  112. Gateway::sendToUid($userId, json_encode($data));
  113. util::success($data);
  114. }
  115. util::success($data);
  116. }
  117. //最近联系人 shish 2019.12.29
  118. public function actionRecentlyUser()
  119. {
  120. $respond = ChatService::getRecentlyUserList($this->sjId);
  121. util::success($respond);
  122. }
  123. //最近聊天的内容 shish 2019.12.29
  124. public function actionRecentlyMessage()
  125. {
  126. //商品 图片 快捷链接 文字
  127. $id = Yii::$app->request->get('id');
  128. $info = UserService::getById($id);
  129. UserService::valid($info, $this->sjId);
  130. $respond = ChatService::getMessageList($id);
  131. util::success($respond);
  132. }
  133. }