UpdateContactListCache.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\ImSendMsg;
  4. use App\Models\Friends\FriendContact;
  5. use App\Models\User\UserModel;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 更新联系人列表缓存
  11. * Class UpdateContactListCache
  12. * @package App\Listeners
  13. */
  14. class UpdateContactListCache implements ShouldQueue
  15. {
  16. use InteractsWithQueue;
  17. /**
  18. * Create the event listener.
  19. *
  20. * @return void
  21. */
  22. public function __construct()
  23. {
  24. }
  25. /**
  26. * Handle the event.
  27. *
  28. * @param ImSendMsg $event
  29. * @return void
  30. */
  31. public function handle(ImSendMsg $event)
  32. {
  33. $sendUser = UserModel::where('im_account', $event->senderIMAccount)->first();
  34. $receiveUser = UserModel::where('im_account', $event->receiverIMAccount)->first();
  35. if ($sendUser && $receiveUser) {
  36. FriendContact::updateOrCreate([
  37. 'uid' => $sendUser->uid,
  38. 'friend_uid' => $receiveUser->uid
  39. ], [
  40. 'list_unread_msg_cnt' => DB::raw("`list_unread_msg_cnt` + 1"),
  41. 'last_at' => $event->timestamp,
  42. 'last_msg' => $event->contentArray
  43. ]);
  44. FriendContact::updateOrCreate([
  45. 'uid' => $receiveUser->uid,
  46. 'friend_uid' => $sendUser->uid
  47. ], [
  48. 'list_unread_msg_cnt' => DB::raw("`list_unread_msg_cnt` + 1"),
  49. 'last_at' => $event->timestamp,
  50. 'last_msg' => $event->contentArray
  51. ]);
  52. }
  53. }
  54. }