ChatController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace mall\controllers;
  3. use bizMall\goods\services\GoodsService;
  4. use bizMall\merchant\services\MerchantService;
  5. use bizMall\message\services\ChatService;
  6. use common\components\business;
  7. use common\components\util;
  8. use Yii;
  9. use GatewayClient\Gateway;
  10. class ChatController extends BaseController
  11. {
  12. //聊天首页,前端使用
  13. public function actionIndex()
  14. {
  15. return $this->renderPartial('index');
  16. }
  17. //绑定 ssh 2019.12.28
  18. public function actionBindUser()
  19. {
  20. $post = Yii::$app->request->post();
  21. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  22. $userId = $this->userId;
  23. Gateway::bindUid($clientId, $userId);
  24. }
  25. //聊天双方的用户信息 ssh 2019.12.30
  26. public function actionBegin()
  27. {
  28. //新增最近联系人
  29. ChatService::addRecentlyUser($this->sjId, $this->userId);
  30. util::success(['merchant' => $this->sj->attributes, 'user' => $this->user]);
  31. }
  32. //发消息给老板 ssh 2019.12.28
  33. public function actionSend()
  34. {
  35. $post = Yii::$app->request->post();
  36. $type = isset($post['type']) ? $post['type'] : 'text';
  37. $arr = [];
  38. $arr['type'] = $type;
  39. $arr['source'] = 'user';
  40. $arr['time'] = '12:51';
  41. switch ($type) {
  42. case 'text':
  43. //文字
  44. $content = $post['content'];
  45. $arr['content'] = $content;
  46. break;
  47. case 'img':
  48. //图片
  49. $shortUrl = $post['shortUrl'];
  50. $return = business::formatUploadImg($shortUrl);
  51. $arr['url'] = $return['url'];
  52. $arr['smallUrl'] = $return['smallUrl'];
  53. break;
  54. case 'link':
  55. //常用链接
  56. $id = $post['linkId'];
  57. $respond = MerchantService::getCommonUrl($id);
  58. $arr['name'] = $respond['name'];
  59. $arr['img'] = $respond['img'];
  60. $arr['url'] = $respond['url'];
  61. break;
  62. case 'goods':
  63. //商品
  64. $id = $post['goodsId'];
  65. $info = GoodsService::getGoodsInfo($id);
  66. GoodsService::valid($info, $this->mainId);
  67. $arr['name'] = $info['name'];
  68. $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
  69. $arr['price'] = isset($info['price']) ? $info['price'] : 300;
  70. break;
  71. }
  72. $data[] = $arr;
  73. $id = 'merchantChat_' . $this->sjId;
  74. $chatOnline = Gateway::getClientIdByUid($id);
  75. if ($chatOnline) {
  76. //直接发送
  77. Gateway::sendToUid($id, json_encode($data));
  78. util::success($data);
  79. }
  80. //有打开后台但没打开聊天框,通知有新消息
  81. $consoleId = 'merchantConsole_' . $this->sjId;
  82. $consoleOnline = Gateway::getClientIdByUid($consoleId);
  83. if ($consoleOnline) {
  84. $data = ['type' => 'newMessage', 'status' => 1];
  85. Gateway::sendToUid($consoleId, json_encode($data));
  86. }
  87. util::success($data);
  88. }
  89. //关闭当前链接
  90. public function actionClose()
  91. {
  92. $post = Yii::$app->request->post();
  93. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  94. Gateway::closeClient($clientId);
  95. }
  96. //最近聊天的内容 ssh 2019.12.29
  97. public function actionRecentlyMessage()
  98. {
  99. $respond = ChatService::getMessageList($this->userId);
  100. util::success($respond);
  101. }
  102. }