DeedFriendManager.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Managers;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Deed\FriendsModel;
  5. use App\Models\Friends\FriendModel;
  6. class DeedFriendManager
  7. {
  8. /**
  9. * 星标某好友
  10. * @param int $uid
  11. * @param int $friend_uid
  12. * @return bool
  13. * @throws AlertException
  14. */
  15. public function starFriend(int $uid, int $friend_uid)
  16. {
  17. $friend = FriendsModel::where([['uid', $uid], ['friend_uid', $friend_uid]])->first();
  18. if (!$friend) {
  19. throw new AlertException("非好友关系", 404);
  20. }
  21. if (0 == $friend->star_at) {
  22. $friend->star_at = time();
  23. } else {
  24. $friend->star_at = 0;
  25. }
  26. $friend->save();
  27. return true;
  28. }
  29. /**
  30. * 隐藏某好友
  31. * @param int $uid
  32. * @param int $friend_uid
  33. * @return bool
  34. * @throws AlertException
  35. */
  36. public function hideFriend(int $uid, int $friend_uid)
  37. {
  38. /** @var FriendModel|null $friend */
  39. $friend = FriendModel::where([['uid', $uid], ['friend_uid', $friend_uid]])->first();
  40. if (!$friend) {
  41. throw new AlertException("非好友关系", 404);
  42. }
  43. $friend->is_hide = 1;
  44. $friend->is_friend = 0;
  45. $friend->unfriend_at = time();
  46. /** @var FriendModel $other */
  47. $other = FriendModel::where([['uid', $friend_uid], ['friend_uid', $uid]])->first();
  48. $other->is_friend = 0;
  49. $other->is_hide = 1;
  50. $other->unfriend_at = time();
  51. $other->last_msg = "对方解除了与你的好友关系";
  52. $other->unread_cnt = $other->unread_cnt + 1;
  53. $friend->save();
  54. $other->save();
  55. return true;
  56. }
  57. }