AnnuallyMonthlyUser.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Modules\User\Services;
  3. use App\Consts\SysConsts;
  4. use App\Modules\Subscribe\Models\Order;
  5. use App\Modules\User\Models\UserMonthOrder;
  6. /**
  7. * 包年包月用户
  8. *
  9. * @property bool $is_monthly 是否包月用户
  10. * @property bool $is_annually 是否包年用户
  11. */
  12. class AnnuallyMonthlyUser
  13. {
  14. protected $is_monthly;
  15. protected $is_annually;
  16. private $uid;
  17. public function __construct(int $uid = 0)
  18. {
  19. if ($uid) {
  20. $this->uid = $uid;
  21. } else {
  22. $user = app()->make('user');
  23. $this->uid = $user->id;
  24. }
  25. }
  26. public function __get($property_name)
  27. {
  28. if (isset($this->$property_name)) {
  29. return $this->$property_name;
  30. } else {
  31. if ($property_name == "is_annually") {
  32. $this->setAnnuallyUser();
  33. return $this->is_annually;
  34. } else if ($property_name == "is_monthly") {
  35. $this->setMonthlyUser();
  36. return $this->is_monthly;
  37. }
  38. }
  39. }
  40. /**
  41. * 设置用户包年属性
  42. */
  43. private function setAnnuallyUser()
  44. {
  45. $year_order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('order_type', 'YEAR')->orderBy('id', 'desc')->first();
  46. if ($year_order && strtotime($year_order->created_at) > (time() - SysConsts::ONE_YEAR_DAYS * SysConsts::ONE_DAY_SECONDS)) {
  47. $this->is_annually = true;
  48. } else {
  49. $this->is_annually = false;
  50. }
  51. }
  52. /**
  53. * 设置用户包月属性
  54. */
  55. private function setMonthlyUser()
  56. {
  57. $month_order = UserMonthOrder::where('uid', $this->uid)->where('type', 'MONTH')->orderBy('id', 'desc')->first();
  58. if ($month_order && strtotime($month_order->created_at) > (time() - SysConsts::ONE_YEAR_DAYS * SysConsts::ONE_DAY_SECONDS)) {
  59. $this->is_monthly = true;
  60. } else {
  61. $this->is_monthly = false;
  62. }
  63. }
  64. }