User.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Models;
  3. use App\Auth;
  4. use Carbon\Carbon;
  5. use Dcat\Admin\Traits\HasDateTimeFormatter;
  6. use Illuminate\Foundation\Auth\User as Authenticatable;
  7. use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
  8. class User extends Authenticatable implements JWTSubject
  9. {
  10. use HasDateTimeFormatter;
  11. protected $fillable = ['name', 'open_id', 'avatar', 'email', 'mobile', 'status', 'online', 'last_login_ip', 'register_ip', 'last_login_time', 'created_at', 'updated_at', 'deleted_at',
  12. 'is_black', 'sessionKey', 'share_pid', 'diamond', 'is_share', 'share_name', 'share_phone', 'share_pid', 'share_date', 'income', 'qr_code'];
  13. public function getJWTIdentifier()
  14. {
  15. return $this->getKey(); // TODO: Implement getJWTIdentifier() method.
  16. }
  17. public function getJWTCustomClaims()
  18. {
  19. return [
  20. 'role' => 'user',
  21. ]; // TODO: Implement getJWTCustomClaims() method.
  22. }
  23. protected function serializeDate(\DateTimeInterface $date)
  24. {
  25. return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
  26. }
  27. /**
  28. * 设置密码加密.
  29. *
  30. * @return string
  31. */
  32. public function setPasswordAttribute($value)
  33. {
  34. $this->attributes['password'] = bcrypt($value);
  35. }
  36. public function getAmountSumAmountAttribute($value)
  37. {
  38. return (float) ($value / 100);
  39. }
  40. public function getIncomeAttribute($value)
  41. {
  42. return (float) ($value / 100);
  43. }
  44. public function setIncomeAttribute($value)
  45. {
  46. return $value * 100;
  47. }
  48. public function getTotalIncomeAttribute($value)
  49. {
  50. return (float) ($value / 100);
  51. }
  52. public function parent()
  53. {
  54. return $this->belongsTo(User::class, 'parent_id');
  55. }
  56. public function child()
  57. {
  58. return $this->belongsTo(User::class, 'id', 'parent_id');
  59. }
  60. public function childOrder()
  61. {
  62. return $this->hasMany(UserShare::class, 'child_id', 'id');
  63. }
  64. public function team()
  65. {
  66. return $this->hasMany(User::class, 'share_pid', Auth::$userId);
  67. }
  68. public function info()
  69. {
  70. return $this->belongsTo(UserInfo::class, 'id', 'user_id');
  71. }
  72. public function consumeRecords()
  73. {
  74. return $this->hasMany(UserConsumeRecord::class, 'user_id', 'id');
  75. }
  76. public function rechargeRecords()
  77. {
  78. return $this->hasMany(UserRechargeRecord::class, 'user_id', 'id');
  79. }
  80. public function creation()
  81. {
  82. return $this->hasMany(TaskList::class, 'user_id', 'id');
  83. }
  84. public function invite_data()
  85. {
  86. return $this->hasMany(User::class, 'share_pid', 'id');
  87. }
  88. public function userShare()
  89. {
  90. return $this->hasOne(User::class, 'id', 'share_pid');
  91. }
  92. public function vipRecords()
  93. {
  94. return $this->hasMany(UserVipRecord::class, 'user_id', 'id');
  95. }
  96. public function amount()
  97. {
  98. return $this->hasMany(Order::class, 'user_id', 'id')->where('state', 1);
  99. }
  100. public function order()
  101. {
  102. return $this->hasMany(Order::class, 'user_id', 'id')->where('state', 1);
  103. }
  104. public function beComeShare($id)
  105. {
  106. $user = self::find($id);
  107. if (!$user->is_share) {
  108. $config = ShareConfig::first();
  109. $isBecome = false;
  110. if (1 == $config->become_type) {// 无条件
  111. $isBecome = true;
  112. } elseif (2 == $config->become_type) { // 成为对应的会员
  113. $vipRecords = UserVipRecord::where('combo_id', $config->become_vip_id)
  114. ->where('user_id', $user->id)
  115. ->where('status', 1)
  116. ->count();
  117. if ($vipRecords) {
  118. $isBecome = true;
  119. }
  120. } else { // 充值一定金币
  121. $RechargeGold = UserRechargeRecord::where('user_id', $user->id)
  122. ->where('status', 1)
  123. ->sum('gold');
  124. if ($RechargeGold >= $config->become_gold) {
  125. $isBecome = true;
  126. }
  127. }
  128. if ($isBecome) {
  129. $user->is_share = 1;
  130. $user->become_share_at = Carbon::now()->toDateTimeString();
  131. $user->save();
  132. }
  133. }
  134. }
  135. }