IMManager.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Managers;
  3. use App\Models\Friends\FriendContact;
  4. use App\Models\IM\UserLatestMessage;
  5. use App\Models\IM\UserMessage;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Support\Facades\Schema;
  8. use Illuminate\Database\Schema\Blueprint;
  9. class IMManager
  10. {
  11. /**
  12. * 是否可以IM通信
  13. * @param string $fromAccount
  14. * @param string $toAccount
  15. * @return bool
  16. */
  17. public function canIm(string $fromAccount, string $toAccount): bool
  18. {
  19. $userManager = new UserManager();
  20. $fromUid = $userManager->getUidByIMAccount($fromAccount);
  21. $toUid = $userManager->getUidByIMAccount($toAccount);
  22. if (
  23. FriendContact::where([
  24. array('uid', $fromUid), array('friend_uid', $toUid), array('is_blacklist', 1)
  25. ])->orWhere([
  26. array('uid', $toUid), array('friend_uid', $fromUid), array('is_blacklist', 1)
  27. ])->first()
  28. ) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. /**
  34. * 获取用户的最近消息
  35. * @param int $userId 获取消息的用户
  36. * @param array $uids 聊天的相关用户组
  37. * @return \Illuminate\Support\Collection
  38. */
  39. public function getUserLatestMessages(int $userId, array $uids)
  40. {
  41. $msgs = UserLatestMessage::where('uid', $userId)
  42. ->where(function (/** @var Builder $query */$query) use ($uids) {
  43. $query->orWhereIn('from_uid', $uids);
  44. $query->orWhereIn('to_uid', $uids);
  45. })->orderBy('msg_time_stamp', 'desc')
  46. ->orderBy('seq', 'desc')->get();
  47. return $msgs;
  48. }
  49. /**
  50. * 标记一条消息未已读
  51. * @param int $userId 用户
  52. * @param int $fromUid 发送者
  53. * @param int $toUid 接收者
  54. * @param int $timeStamp 消息发送的时间戳
  55. * @param $random
  56. * @param $seq
  57. */
  58. public function markMessageAsRead(int $userId, int $fromUid, int $toUid, int $timeStamp, $random, $seq)
  59. {
  60. UserLatestMessage::where('uid', $userId)
  61. ->where('to_uid', $toUid)
  62. ->where('from_uid', $fromUid)
  63. ->where('msg_time_stamp', $timeStamp)
  64. ->where('msg_random', $random)
  65. ->where('seq', $seq)
  66. ->update([
  67. 'read' => true
  68. ]);
  69. }
  70. /**
  71. * 删除一条消息
  72. * @param int $userId 用户
  73. * @param int $fromUid 发送者
  74. * @param int $toUid 接收者
  75. * @param int $timeStamp 消息发送的时间戳
  76. * @param $random
  77. * @param $seq
  78. * @throws \Exception
  79. */
  80. public function deleteMessage(int $userId, int $fromUid, int $toUid, int $timeStamp, $random, $seq)
  81. {
  82. UserLatestMessage::where('uid', $userId)
  83. ->where('to_uid', $toUid)
  84. ->where('from_uid', $fromUid)
  85. ->where('msg_time_stamp', $timeStamp)
  86. ->where('msg_random', $random)
  87. ->where('seq', $seq)
  88. ->delete();
  89. }
  90. /**
  91. * 撤回一条消息
  92. * @param int $userId 用户
  93. * @param int $toUid 接收者
  94. * @param int $timeStamp
  95. * @param $random
  96. * @param $seq
  97. * @throws \Exception
  98. */
  99. public function revokeMessage(int $userId, int $toUid, int $timeStamp, $random, $seq)
  100. {
  101. UserLatestMessage::where('uid', $userId)
  102. ->where('to_uid', $toUid)
  103. ->where('from_uid', $userId)
  104. ->where('msg_time_stamp', $timeStamp)
  105. ->where('msg_random', $random)
  106. ->where('seq', $seq)
  107. ->delete();
  108. UserLatestMessage::where('uid', $toUid)
  109. ->where('to_uid', $toUid)
  110. ->where('from_uid', $userId)
  111. ->where('msg_time_stamp', $timeStamp)
  112. ->where('msg_random', $random)
  113. ->where('seq', $seq)
  114. ->delete();
  115. }
  116. /**
  117. * 发送消息后的回调
  118. *
  119. * 写入被发方和发送方的最近消息
  120. * 并保持最近消息不冗余
  121. */
  122. public function sendMessageCallback()
  123. {
  124. // 写入发送者数据
  125. // 写入被发送者数据
  126. }
  127. public function mapAccountToUserId(array $accounts)
  128. {
  129. $userManager = new UserManager();
  130. return array_combine($accounts, array_map(function ($account) use ($userManager) {
  131. return $userManager->getUidByIMAccount($account);
  132. }, $accounts));
  133. }
  134. public function createUserLatestMessageByAccount(
  135. $account,
  136. $fromAccount,
  137. $toAccount,
  138. $msgTime,
  139. $msgRandom,
  140. $msgSeq,
  141. $data,
  142. $read = false
  143. ) {
  144. $userIds = $this->mapAccountToUserId([
  145. $account,
  146. $fromAccount,
  147. $toAccount,
  148. ]);
  149. $this->createUserLatestMessageByUserId(
  150. $userIds[$account] ?? 0,
  151. $userIds[$fromAccount] ?? 0,
  152. $userIds[$toAccount] ?? 0,
  153. $msgTime,
  154. $msgRandom,
  155. $msgSeq,
  156. $data,
  157. $read
  158. );
  159. }
  160. public function createUserLatestMessageByUserId(
  161. $userId,
  162. $fromUserId,
  163. $toUserId,
  164. $msgTime,
  165. $msgRandom,
  166. $msgSeq,
  167. $data,
  168. $read = false
  169. ) {
  170. UserLatestMessage::create([
  171. 'uid' => $userId,
  172. 'from_uid' => $fromUserId,
  173. 'to_uid' => $toUserId,
  174. 'msg_time_stamp' => $msgTime,
  175. 'msg_random' => $msgRandom,
  176. 'seq' => $msgSeq,
  177. 'read' => $read,
  178. 'content' => json_encode($data),
  179. ]);
  180. $count = UserLatestMessage::where([
  181. array('uid', $userId), array('to_uid', $toUserId), array('read', 1)
  182. ])->orWhere([
  183. array('uid', $userId), array('from_uid', $toUserId), array('read', 1)
  184. ])->count();
  185. if (($count - 10) > 0) {
  186. UserLatestMessage::where([
  187. array('uid', $userId),
  188. array('to_uid', $toUserId),
  189. array('read', 1)
  190. ])->orWhere([
  191. array('uid', $userId),
  192. array('from_uid', $toUserId),
  193. array('read', 1)
  194. ])->orderByDesc('msg_time_stamp')->take($count - 10)->delete();
  195. };
  196. }
  197. public function createUserMessageByAccount(
  198. $account,
  199. $fromAccount,
  200. $toAccount,
  201. $msgTime,
  202. $msgRandom,
  203. $msgSeq,
  204. $data
  205. ) {
  206. $userIds = $this->mapAccountToUserId([
  207. $account,
  208. $fromAccount,
  209. $toAccount,
  210. ]);
  211. $this->createUserMessageByUserId(
  212. $userIds[$account] ?? 0,
  213. $userIds[$fromAccount] ?? 0,
  214. $userIds[$toAccount] ?? 0,
  215. $msgTime,
  216. $msgRandom,
  217. $msgSeq,
  218. $data
  219. );
  220. }
  221. public function createUserMessageByUserId(
  222. $userId,
  223. $fromUserId,
  224. $toUserId,
  225. $msgTime,
  226. $msgRandom,
  227. $msgSeq,
  228. $data
  229. ) {
  230. UserMessage::create([
  231. 'uid' => $userId,
  232. 'from_uid' => $fromUserId,
  233. 'to_uid' => $toUserId,
  234. 'msg_time_stamp' => $msgTime,
  235. 'msg_random' => $msgRandom,
  236. 'seq' => $msgSeq,
  237. 'content' => json_encode($data),
  238. ]);
  239. }
  240. }