InviteService.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Services\Goodnight;
  3. use App\Exceptions\AlertException;
  4. use App\Exceptions\DBException;
  5. use App\Models\Goodnight\InviteLogModel;
  6. use App\Models\Goodnight\UserModel;
  7. use App\Services\Service;
  8. class InviteService extends Service
  9. {
  10. public function invite(int $uid, int $invite_uid)
  11. {
  12. $user = UserModel::findOrFail($uid);
  13. if ($user->subscribed_at > 0) {
  14. throw new AlertException("新用户专享", 202);
  15. }
  16. if ($user->invite_uid == $invite_uid) {
  17. throw new AlertException("已经邀请过了", 203);
  18. }
  19. $user->invite_uid = $invite_uid;
  20. if ($user->save()) {
  21. return true;
  22. } else {
  23. throw new DBException("系统异常", 500);
  24. }
  25. }
  26. /**
  27. * 邀请历史列表
  28. * @param int $page
  29. * @param int $limit
  30. * @param int $uid
  31. * @return array
  32. */
  33. public function invitelist(int $page, int $limit, int $uid): array
  34. {
  35. $total = InviteLogModel::where('uid', $uid)->count();
  36. $invites = InviteLogModel::where('uid', $uid)->orderBy(
  37. 'created_at',
  38. 'desc'
  39. )->skip(($page - 1) * $limit)->take($limit)->get(['invite_uid', 'id', 'created_at', 'gnight_coin']);
  40. foreach ($invites as &$invite) {
  41. $user = \App\Models\User\UserModel::findOrFail($invite->invite_uid);
  42. $invite->nickname = $user->nickname;
  43. $invite->headimgurl = $user->headimgurl;
  44. }
  45. return array(
  46. 'total' => $total,
  47. 'list' => $invites,
  48. );
  49. }
  50. }