ChatController.php 4.4 KB

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