User.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Auth\Authenticatable;
  5. use Laravel\Lumen\Auth\Authorizable;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  8. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  9. use Tymon\JWTAuth\Contracts\JWTSubject;
  10. class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
  11. {
  12. use Authenticatable, Authorizable;
  13. /**
  14. * The attributes that are mass assignable.
  15. *
  16. * @var array
  17. */
  18. // protected $fillable = [
  19. // 'name', 'id'
  20. // ];
  21. protected $guarded = [];
  22. /**
  23. * The attributes excluded from the model's JSON form.
  24. *
  25. * @var array
  26. */
  27. protected $hidden = [
  28. 'password',
  29. ];
  30. /**
  31. * Get the identifier that will be stored in the subject claim of the JWT.
  32. *
  33. * @return mixed
  34. */
  35. public function getJWTIdentifier()
  36. {
  37. return $this->getKey();
  38. }
  39. /**
  40. * Return a key value array, containing any custom claims to be added to the JWT.
  41. *
  42. * @return array
  43. */
  44. public function getJWTCustomClaims()
  45. {
  46. return ['role' => 'user'];
  47. }
  48. // 注册来源
  49. const REGISTER_SOURCE_WEAPP = 'weapp';
  50. const REGISTER_SOURCE_MOBILE = 'mobile';
  51. public static $registerSourceMaps = [
  52. self::REGISTER_SOURCE_WEAPP => '微信小程序',
  53. self::REGISTER_SOURCE_MOBILE => '手机号',
  54. ];
  55. //账号状态
  56. const STATUS_OK = 1;
  57. const STATUS_PAUSE = 0;
  58. public static $statusMaps = [
  59. self::STATUS_PAUSE => '暂停使用',
  60. self::STATUS_OK => '正常'
  61. ];
  62. // 认证状态
  63. const CARD_OK = 1;
  64. const CARD_NO = 0;
  65. public static $cardMaps = [
  66. self::CARD_OK => '已实名认证',
  67. self::CARD_NO => '未实名认证'
  68. ];
  69. // 押金状态
  70. const DEPOSIT_OK = 1;
  71. const DEPOSIT_NO = 0;
  72. public static $depositMaps = [
  73. self::DEPOSIT_OK => '已缴纳押金',
  74. self::DEPOSIT_NO => '未缴纳押金',
  75. ];
  76. const DEPOSIT_MONEY = 1;
  77. const DEPOSIT_CARD = 2;
  78. const DEPOSIT_TYPE_NO = 0;
  79. public static $depositTypeMaps = [
  80. self::DEPOSIT_TYPE_NO => '没有缴纳押金',
  81. self::DEPOSIT_MONEY => '缴纳押金',
  82. self::DEPOSIT_CARD => '免押金卡',
  83. ];
  84. // 是否到了骑车年龄的状态
  85. const RIDE_BIKE_AGE_OK = 1;
  86. const RIDE_BIKE_AGE_NO = 0;
  87. public static $rideBikeAgeMaps = [
  88. self::RIDE_BIKE_AGE_OK => '已到骑车的年龄',
  89. self::RIDE_BIKE_AGE_NO => '未到骑车的年龄'
  90. ];
  91. // 手机号绑定状态
  92. const BIND_MOBILE_OK = 1;
  93. const BIND_MOBILE_NO = 0;
  94. public static $bingMobileMaps = [
  95. self::BIND_MOBILE_OK => '已绑定收手机号',
  96. self::BIND_MOBILE_NO => '未绑定手机号'
  97. ];
  98. // 用户授权状态
  99. const REGISTER_OK = 1;
  100. const REGISTER_NO = 0;
  101. public static $registerMaps = [
  102. self::REGISTER_OK => '已授权',
  103. self::REGISTER_NO => '未授权'
  104. ];
  105. public function auth()
  106. {
  107. return $this->hasOne(Auth::class)->where('type', Auth::TYPE_WEAPP);
  108. }
  109. public function userPhoneDetail()
  110. {
  111. return $this->hasOne(UserPhoneDetail::class);
  112. }
  113. // 定义是否缴纳押金访问器 更新免押金卡过期
  114. public function getIsDepositAttribute($value)
  115. {
  116. if ((int)$this->deposit_type === self::DEPOSIT_CARD && (int)$value === self::DEPOSIT_OK) {
  117. $deposit_expire_time = Carbon::parse($this->deposit_expire_time);
  118. if (Carbon::now()->gt($deposit_expire_time)) {
  119. //判断第一个日期是否比第二个日期大
  120. $this->updateDeposit($this->id);
  121. $this->save();
  122. return self::DEPOSIT_NO;
  123. }
  124. }
  125. return $value;
  126. }
  127. public function updateDeposit($user_id)
  128. {
  129. self::query()->where('id', $user_id)->update(['is_deposit' => User::DEPOSIT_NO, 'deposit_type' => User::DEPOSIT_TYPE_NO]);
  130. }
  131. }