ChatController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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','un-read-msg-count'];
  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 = 0;
  66. //未登录也会访问这个接口
  67. if (!empty($this->userId)) {
  68. $c = ChatService::getUnReadMsgCount($this->userId, 'message');
  69. }
  70. util::success(['msg_count' => $c]);
  71. }
  72. //用 redis key 获取商品报价
  73. public function actionGetQuotedPrice()
  74. {
  75. $key = Yii::$app->request->post('key');
  76. $keyArr = explode('-', $key);
  77. $userId = intval($keyArr[1]);
  78. if ($userId != $this->userId) {
  79. util::error(-1, '校验失败');
  80. }
  81. $key = ChatClass::getQuotedPriceKey($keyArr[0], $keyArr[1], $keyArr[2]);
  82. $val = Yii::$app->redis->executeCommand('GET', [$key]);
  83. if (empty($val)) {
  84. util::success(['price' => -1]);
  85. }
  86. util::success(['price' => $val]);
  87. }
  88. // message_server 远程调用专用方法,可用于:1.有未读消息的通知(可以额外添加触发条件)2.
  89. public function actionMessageServerCall()
  90. {
  91. Yii::info('message_server 远程调用');
  92. $callbackData = Yii::$app->request->post();
  93. if (empty($callbackData)) {
  94. $postStr = file_get_contents('php://input');
  95. // 处理转义字符,将JSON字符串正确解析为数组
  96. //$postStr = stripslashes($postStr);
  97. $callbackData = json_decode($postStr, true);
  98. if (empty($callbackData)) {
  99. util::fail('回调请求的数据为空');
  100. }
  101. }
  102. $shopId = $callbackData['shopId'] ?? 0;
  103. $userId = $callbackData['userId'] ?? '';
  104. $key = 'has_remind_' . $shopId . '_' . $userId;
  105. if (!empty($shopId) && !empty($userId)) {
  106. //10秒不重复通知
  107. $hasRemind = Yii::$app->redis->executeCommand('GET', [$key]);
  108. if (empty($hasRemind)) {
  109. $shop = ShopClass::getById($shopId, true);
  110. if (!empty($shop)) {
  111. WxMessageClass::newMessageInform($shop, "商城上客户有留言");
  112. Yii::$app->redis->executeCommand('SETEX', [$key, 10, 'has']);
  113. //$time = time();
  114. //noticeUtil::push("客户留言已通知花店,shopId:" . $shopId . ' userId:' . $userId . ' time:' . $time, '15280215347');
  115. }
  116. }
  117. }
  118. Yii::info(json_encode($callbackData));
  119. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']); // 成功状态的返回数据
  120. }
  121. }