ChatController.php 4.5 KB

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