AnnuallyMonthlyUser.php 1.8 KB

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