ChatController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. //聊天首页,前端使用
  13. public function actionIndex()
  14. {
  15. return $this->renderPartial('index');
  16. }
  17. //绑定 shish 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. //聊天双方的用户信息 shish 2019.12.30
  26. public function actionBegin()
  27. {
  28. util::success(['merchant'=>$this->merchant,'user'=>$this->user]);
  29. }
  30. //发消息给老板 shish 2019.12.28
  31. public function actionSend()
  32. {
  33. $post = Yii::$app->request->post();
  34. $type = isset($post['type']) ? $post['type'] : 'text';
  35. $arr = [];
  36. $arr['type'] = $type;
  37. $arr['source'] = 'user';
  38. $arr['time'] = '12:51';
  39. switch ($type) {
  40. case 'text':
  41. //文字
  42. $content = $post['content'];
  43. $arr['content'] = $content;
  44. break;
  45. case 'img':
  46. //图片
  47. $shortUrl = $post['shortUrl'];
  48. $return = business::formatUploadImg($shortUrl);
  49. $arr['url'] = $return['url'];
  50. $arr['smallUrl'] = $return['smallUrl'];
  51. break;
  52. case 'link':
  53. //常用链接
  54. $id = $post['linkId'];
  55. $respond = MerchantService::getCommonUrl($id);
  56. $arr['name'] = $respond['name'];
  57. $arr['img'] = $respond['img'];
  58. $arr['url'] = $respond['url'];
  59. break;
  60. case 'goods':
  61. //商品
  62. $id = $post['goodsId'];
  63. $info = GoodsService::getGoodsInfo($id);
  64. GoodsService::valid($info, $this->merchantId);
  65. $arr['goodsName'] = $info['goodsName'];
  66. $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
  67. $arr['price'] = isset($info['price']) ? $info['price'] : 300;
  68. break;
  69. }
  70. $data[] = $arr;
  71. $id = 'merchantChat_' . $this->merchantId;
  72. $merchantId = $this->merchantId;
  73. $userId = $this->userId;
  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->merchantId;
  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. }