ChatController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace client\controllers;
  3. use biz\goods\services\GoodsService;
  4. use biz\merchant\services\MerchantService;
  5. use biz\MQ\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. public function beforeAction($action)
  13. {
  14. return parent::beforeAction($action);
  15. }
  16. //聊天首页,前端使用
  17. public function actionIndex()
  18. {
  19. return $this->renderPartial('index');
  20. }
  21. //绑定 shish 2019.12.28
  22. public function actionBindUser()
  23. {
  24. $post = Yii::$app->request->post();
  25. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  26. $userId = $this->userId;
  27. Gateway::bindUid($clientId, $userId);
  28. }
  29. //发消息给老板 shish 2019.12.28
  30. public function actionSend()
  31. {
  32. $post = Yii::$app->request->post();
  33. $type = isset($post['type']) ? $post['type'] : 'text';
  34. $arr = [];
  35. $arr['type'] = $type;
  36. $arr['source'] = 'user';
  37. $arr['time'] = '12:51';
  38. switch ($type) {
  39. case 'text':
  40. //文字
  41. $content = $post['content'];
  42. $arr['content'] = $content;
  43. break;
  44. case 'img':
  45. //图片
  46. $shortUrl = $post['shortUrl'];
  47. $return = business::formatUploadImg($shortUrl);
  48. $arr['url'] = $return['url'];
  49. $arr['smallUrl'] = $return['smallUrl'];
  50. break;
  51. case 'link':
  52. //常用链接
  53. $id = $post['linkId'];
  54. $respond = MerchantService::getCommonUrl($id);
  55. $arr['name'] = $respond['name'];
  56. $arr['img'] = $respond['img'];
  57. $arr['url'] = $respond['url'];
  58. break;
  59. case 'goods':
  60. //商品
  61. $id = $post['goodsId'];
  62. $info = GoodsService::getGoodsInfo($id);
  63. GoodsService::valid($info, $this->merchantId);
  64. $arr['goodsName'] = $info['goodsName'];
  65. $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
  66. $arr['price'] = isset($info['price']) ? $info['price'] : 300;
  67. break;
  68. }
  69. $data[] = $arr;
  70. $id = 'merchantChat_' . $this->merchantId;
  71. $merchantId = $this->merchantId;
  72. $userId = $this->userId;
  73. $chatOnline = Gateway::getClientIdByUid($id);
  74. if ($chatOnline) {
  75. //直接发送
  76. Gateway::sendToUid($id, json_encode($data));
  77. util::success($data);
  78. }
  79. //有打开后台但没打开聊天框,通知有新消息
  80. $consoleId = 'merchantConsole_' . $this->merchantId;
  81. $consoleOnline = Gateway::getClientIdByUid($consoleId);
  82. if ($consoleOnline) {
  83. $data = ['type' => 'newMessage', 'status' => 1];
  84. Gateway::sendToUid($consoleId, json_encode($data));
  85. }
  86. util::success($data);
  87. }
  88. //关闭当前链接
  89. public function actionClose()
  90. {
  91. $post = Yii::$app->request->post();
  92. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  93. Gateway::closeClient($clientId);
  94. }
  95. }