AdminCreditAccountResource.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Http\Resources\Concerns\ResolvesActor;
  4. use App\Models\CreditLedgerEntry;
  5. use Illuminate\Http\Request;
  6. /**
  7. * 一个账号的积分汇总(按账号聚合的流水,不是单条流水)。**本类不查库**:user 由 Service 预加载。
  8. *
  9. * @mixin CreditLedgerEntry
  10. */
  11. class AdminCreditAccountResource extends BaseResource
  12. {
  13. use ResolvesActor;
  14. /**
  15. * @param Request $request
  16. * @return array{
  17. * account_type: 'user'|'guest',
  18. * account_id: string,
  19. * user: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null,
  20. * earned_balance: int,
  21. * earned_total: int,
  22. * adjusted_total: int,
  23. * spent_total: int,
  24. * free_used_today: int,
  25. * daily_free: int,
  26. * last_activity_at: string|null
  27. * }
  28. */
  29. public function toArray($request): array
  30. {
  31. return [
  32. 'account_type' => $this->account_type,
  33. 'account_id' => $this->account_id,
  34. // 游客的 account_id 是设备 id,不能拿去当用户 uid
  35. 'user' => $this->account_type === 'user' && $this->user ? $this->actor($this->user) : null,
  36. 'earned_balance' => (int) $this->earned_balance,
  37. 'earned_total' => (int) $this->earned_total,
  38. 'adjusted_total' => (int) $this->adjusted_total,
  39. 'spent_total' => (int) $this->spent_total,
  40. 'free_used_today' => (int) $this->free_used_today,
  41. 'daily_free' => (int) (config('mint.credits.policy.daily_free')[$this->account_type] ?? 0),
  42. 'last_activity_at' => $this->last_activity_at ? now('UTC')->parse($this->last_activity_at)->toIso8601ZuluString() : null,
  43. ];
  44. }
  45. }