ChatController.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace client\controllers;
  3. use common\components\util;
  4. use Yii;
  5. use GatewayClient\Gateway;
  6. class ChatController extends BaseController
  7. {
  8. public function beforeAction($action)
  9. {
  10. return parent::beforeAction($action);
  11. }
  12. //聊天首页,前端使用
  13. public function actionIndex()
  14. {
  15. return $this->renderPartial('index');
  16. }
  17. //绑定 shish 2019.12.28
  18. public function actionBind()
  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.28
  26. public function actionSend()
  27. {
  28. $post = Yii::$app->request->post();
  29. $msg = isset($post['msg']) ? $post['msg'] : '';
  30. $id = 'merchantChat_' . $this->merchantId;
  31. $data = json_encode(array(
  32. 'type' => 'msg',
  33. 'msg' => $msg,
  34. ));
  35. $chatOnline = Gateway::getClientIdByUid($id);
  36. if ($chatOnline) {
  37. //直接发送
  38. Gateway::sendToUid($id, json_encode($data));
  39. util::success($data);
  40. }
  41. $consoleId = 'merchantConsole_' . $this->merchantId;
  42. $consoleOnline = Gateway::getClientIdByUid($consoleId);
  43. if ($consoleOnline) {
  44. //通知打开的控制台有新消息
  45. $data = json_encode(array(
  46. 'type' => 'msg',
  47. 'msg' => $msg,
  48. ));
  49. Gateway::sendToUid($consoleId, json_encode($data));
  50. }
  51. //保存起来
  52. }
  53. }