ChatController.php 3.9 KB

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