ChatController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace mall\controllers;
  3. use biz\wx\classes\WxMessageClass;
  4. use bizMall\merchant\classes\ShopClass;
  5. use bizMall\message\services\ChatService;
  6. use common\components\util;
  7. use Yii;
  8. use GatewayClient\Gateway;
  9. class ChatController extends BaseController
  10. {
  11. public $guestAccess = ['message-server-call'];
  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. //关闭当前链接
  33. public function actionClose()
  34. {
  35. $post = Yii::$app->request->post();
  36. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  37. Gateway::closeClient($clientId);
  38. }
  39. //最近聊天的内容 ssh 2019.12.29
  40. public function actionRecentlyMessage()
  41. {
  42. $respond = ChatService::getMessageList($this->userId);
  43. util::success($respond);
  44. }
  45. // ---------------------------- 新聊天功能的接口 ----------------------------------
  46. //最近聊天人
  47. public function actionLatestUsers()
  48. {
  49. $respond = ChatService::getLatestUsers(intval($this->userId));
  50. util::success($respond);
  51. }
  52. //聊天的内容
  53. public function actionChatHistory()
  54. {
  55. $roomName = Yii::$app->request->post('roomName');
  56. // 校验 $roomeName 的值,防止越权访问他人数据
  57. $roomName = 'room_messages:' . $roomName;
  58. $respond = ChatService::getChatHistory($roomName);
  59. util::success($respond);
  60. }
  61. //客户的未读消息总数
  62. public function actionUnReadMsgCount()
  63. {
  64. $c = ChatService::getUnReadMsgCount(intval($this->userId), 'message');
  65. util::success(['msg_count' => $c]);
  66. }
  67. //用 redis key 获取商品报价
  68. public function actionGetQuotedPrice()
  69. {
  70. $key = Yii::$app->request->post('key');
  71. $keyArr = explode('-', $key);
  72. $userId = intval($keyArr[1]);
  73. if ($userId != $this->userId) {
  74. util::error(-1, '校验失败');
  75. }
  76. $val = Yii::$app->redis->executeCommand('GET', [$key]);
  77. if (empty($val)) {
  78. util::success(['price' => -1]);
  79. }
  80. util::success(['price' => $val]);
  81. }
  82. // message_server 远程调用专用方法,可用于:1.有未读消息的通知(可以额外添加触发条件)2.
  83. public function actionMessageServerCall()
  84. {
  85. Yii::info('message_server 远程调用');
  86. $callbackData = Yii::$app->request->post();
  87. if (empty($callbackData)) {
  88. $postStr = file_get_contents('php://input');
  89. // 处理转义字符,将JSON字符串正确解析为数组
  90. //$postStr = stripslashes($postStr);
  91. $callbackData = json_decode($postStr, true);
  92. if (empty($callbackData)) {
  93. util::fail('回调请求的数据为空');
  94. }
  95. }
  96. $shopId = $callbackData['shopId'] ?? 0;
  97. $userId = $callbackData['userId'] ?? '';
  98. $key = 'has_remind_' . $shopId . '_' . $userId;
  99. if (!empty($shopId) && !empty($userId)) {
  100. //10秒不重复通知
  101. $hasRemind = Yii::$app->redis->executeCommand('GET', [$key]);
  102. if (empty($hasRemind)) {
  103. $shop = ShopClass::getById($shopId, true);
  104. if (!empty($shop)) {
  105. WxMessageClass::newMessageInform($shop, "商城上客户有留言");
  106. Yii::$app->redis->executeCommand('SETEX', [$key, 10, 'has']);
  107. //$time = time();
  108. //noticeUtil::push("客户留言已通知花店,shopId:" . $shopId . ' userId:' . $userId . ' time:' . $time, '15280215347');
  109. }
  110. }
  111. }
  112. Yii::info(json_encode($callbackData));
  113. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']); // 成功状态的返回数据
  114. }
  115. }