User.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Auth\Authenticatable;
  5. use Laravel\Lumen\Auth\Authorizable;
  6. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  7. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  8. use Tymon\JWTAuth\Contracts\JWTSubject;
  9. class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
  10. {
  11. use Authenticatable, Authorizable;
  12. /**
  13. * The attributes that are mass assignable.
  14. *
  15. * @var array
  16. */
  17. // protected $fillable = [
  18. // 'name', 'id'
  19. // ];
  20. protected $guarded = [];
  21. /**
  22. * The attributes excluded from the model's JSON form.
  23. *
  24. * @var array
  25. */
  26. protected $hidden = [
  27. 'password',
  28. ];
  29. /**
  30. * Get the identifier that will be stored in the subject claim of the JWT.
  31. *
  32. * @return mixed
  33. */
  34. public function getJWTIdentifier()
  35. {
  36. return $this->getKey();
  37. }
  38. /**
  39. * Return a key value array, containing any custom claims to be added to the JWT.
  40. *
  41. * @return array
  42. */
  43. public function getJWTCustomClaims()
  44. {
  45. return ['role' => 'user'];
  46. }
  47. // 注册来源
  48. const REGISTER_SOURCE_WEAPP = 'weapp';
  49. const REGISTER_SOURCE_MOBILE = 'mobile';
  50. const REGISTER_SOURCE_ALIPAYMINI = 'alipaymini';
  51. public static $registerSourceMaps = [
  52. self::REGISTER_SOURCE_WEAPP => '微信小程序',
  53. self::REGISTER_SOURCE_MOBILE => '手机号',
  54. self::REGISTER_SOURCE_ALIPAYMINI => '支付宝小程序',
  55. ];
  56. //账号状态
  57. const STATUS_OK = 1;
  58. const STATUS_PAUSE = 0;
  59. public static $statusMaps = [
  60. self::STATUS_PAUSE => '暂停使用',
  61. self::STATUS_OK => '正常'
  62. ];
  63. //是否领取新用户礼
  64. const IS_NEW_USER_COUPONS_OK = 1;
  65. const IS_NEW_USER_COUPONS_NO = 0;
  66. public static $isNewUserCouponsMaps = [
  67. self::IS_NEW_USER_COUPONS_NO => '已经领取',
  68. self::IS_NEW_USER_COUPONS_OK => '未领取'
  69. ];
  70. // 认证状态
  71. const CARD_OK = 1;
  72. const CARD_NO = 0;
  73. public static $cardMaps = [
  74. self::CARD_OK => '已实名认证',
  75. self::CARD_NO => '未实名认证'
  76. ];
  77. // 押金状态
  78. const DEPOSIT_OK = 1;
  79. const DEPOSIT_NO = 0;
  80. public static $depositMaps = [
  81. self::DEPOSIT_OK => '已缴纳押金',
  82. self::DEPOSIT_NO => '未缴纳押金',
  83. ];
  84. const DEPOSIT_MONEY = 1;
  85. const DEPOSIT_CARD = 2;
  86. //没有用
  87. const DEPOSIT_COUPON = 3;
  88. const DEPOSIT_STUDENT = 4;
  89. const DEPOSIT_ALIPAY_CARD = 5;
  90. const DEPOSIT_TYPE_NO = 0;
  91. public static $depositTypeMaps = [
  92. self::DEPOSIT_TYPE_NO => '没有缴纳押金',
  93. self::DEPOSIT_MONEY => '缴纳押金',
  94. self::DEPOSIT_CARD => '免押金卡',
  95. self::DEPOSIT_COUPON => '免押金劵',
  96. self::DEPOSIT_STUDENT => '师生认证',
  97. self::DEPOSIT_ALIPAY_CARD => '支付宝预授权',
  98. ];
  99. const IS_COUPON_DEPOSIT_FREE_OK = 1;
  100. const IS_COUPON_DEPOSIT_FREE_NO = 0;
  101. public static $isCouponDepositFreeMaps = [
  102. self::IS_COUPON_DEPOSIT_FREE_OK => '有免押金劵',
  103. self::IS_COUPON_DEPOSIT_FREE_NO => '没有免押金劵',
  104. ];
  105. // 是否到了骑车年龄的状态
  106. const RIDE_BIKE_AGE_OK = 1;
  107. const RIDE_BIKE_AGE_NO = 0;
  108. public static $rideBikeAgeMaps = [
  109. self::RIDE_BIKE_AGE_OK => '已到骑车的年龄',
  110. self::RIDE_BIKE_AGE_NO => '未到骑车的年龄'
  111. ];
  112. // 手机号绑定状态
  113. const BIND_MOBILE_OK = 1;
  114. const BIND_MOBILE_NO = 0;
  115. public static $bingMobileMaps = [
  116. self::BIND_MOBILE_OK => '已绑定收手机号',
  117. self::BIND_MOBILE_NO => '未绑定手机号'
  118. ];
  119. // 用户授权状态
  120. const REGISTER_OK = 1;
  121. const REGISTER_NO = 0;
  122. public static $registerMaps = [
  123. self::REGISTER_OK => '已授权',
  124. self::REGISTER_NO => '未授权'
  125. ];
  126. // 用户授权状态
  127. const IS_STUDENT_OK = 1;
  128. const IS_STUDENT_NO = 0;
  129. public static $isStudentMaps = [
  130. self::IS_STUDENT_OK => '学生',
  131. self::IS_STUDENT_NO => '不是'
  132. ];
  133. public function auth()
  134. {
  135. return $this->hasOne(Auth::class)->where('type', Auth::TYPE_WEAPP);
  136. }
  137. public function userPhoneDetail()
  138. {
  139. return $this->hasOne(UserPhoneDetail::class);
  140. }
  141. public function getIsCouponDepositFreeAttribute()
  142. {
  143. if ($this->attributes['is_coupon_deposit_free'] == self::IS_COUPON_DEPOSIT_FREE_NO) {
  144. return false;
  145. }
  146. $now = Carbon::now()->toDateTimeString();
  147. $re = CouponsUserBag::query()->where('user_id', $this->attributes['id'])->where('coupon_type', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE)->where('valid_end_time', '>=', $now)->where('status', CouponsUserBag::STATUS_OK)->exists();
  148. CouponsUserBag::query()->where('user_id', $this->attributes['id'])->where('coupon_type', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE)->where('valid_end_time', '<', $now)->where('status', CouponsUserBag::STATUS_OK)->update(['status' => CouponsUserBag::STATUS_NO]);
  149. if ($re === false) {
  150. User::where('id', $this->attributes['id'])->update(['is_coupon_deposit_free' => self::IS_COUPON_DEPOSIT_FREE_NO]);
  151. }
  152. return $re;
  153. }
  154. public function getIsDepositAttribute($value)
  155. {
  156. if ((int)$this->deposit_type === self::DEPOSIT_CARD && (int)$value === self::DEPOSIT_OK) {
  157. $deposit_expire_time = Carbon::parse($this->deposit_expire_time);
  158. if (Carbon::now()->gt($deposit_expire_time)) {
  159. //判断第一个日期是否比第二个日期大
  160. $this->updateDeposit($this->id);
  161. $this->save();
  162. return self::DEPOSIT_NO;
  163. }
  164. }
  165. return $value;
  166. }
  167. public function updateDeposit($user_id)
  168. {
  169. self::query()->where('id', $user_id)->update(['is_deposit' => User::DEPOSIT_NO, 'is_student' => self::IS_STUDENT_NO, 'deposit_type' => User::DEPOSIT_TYPE_NO]);
  170. }
  171. }