ApplyService.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Services\V2_17_3\Friends;
  3. use App\Exceptions\AlertException;
  4. use App\Jobs\GrowingIO\FriendInvitationReportJob;
  5. use App\Models\Deed\InvitationCardModel;
  6. use App\Models\Friends\FriendApplyModel;
  7. use App\Models\Friends\FriendModel;
  8. use App\Models\Friends\LogModel;
  9. use App\Services\Service;
  10. use Illuminate\Support\Facades\DB;
  11. class ApplyService extends Service
  12. {
  13. /**
  14. * 发起好友申请
  15. * @param int $uid
  16. * @param int $to_uid
  17. * @param array $questions
  18. * @return bool
  19. * @throws AlertException
  20. */
  21. public function send(int $uid, int $to_uid, array $questions)
  22. {
  23. if ($uid == $to_uid) {
  24. throw new AlertException("不能邀请自己参加活动哦", 422);
  25. }
  26. try {
  27. DB::beginTransaction();
  28. $invitationCard = InvitationCardModel::create([
  29. 'uid' => $uid,
  30. 'invite_uid' => $to_uid,
  31. ]);
  32. foreach ($questions as $question) {
  33. LogModel::create([
  34. 'uid' => $uid,
  35. 'tuid' => $to_uid,
  36. 'do' => 3,
  37. 'attach' => [$question],
  38. ]);
  39. }
  40. FriendApplyModel::send($uid, $to_uid);
  41. dispatch_now(new FriendInvitationReportJob($invitationCard, $questions));
  42. DB::commit();
  43. return true;
  44. } catch (\Exception $exception) {
  45. DB::rollBack();
  46. return false;
  47. }
  48. }
  49. public function agree(int $uid, int $apply_uid)
  50. {
  51. if ($uid == $apply_uid) {
  52. throw new AlertException("参数错误", 422);
  53. }
  54. $applyOrFriend = FriendModel::applyOrFriend($uid, $apply_uid);
  55. if (1 == $applyOrFriend) {
  56. } else {
  57. throw new AlertException("没有收到新未处理的好友申请", 423);
  58. }
  59. }
  60. }