123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace App\Models;
- use Carbon\Carbon;
- use Illuminate\Auth\Authenticatable;
- use Laravel\Lumen\Auth\Authorizable;
- use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
- use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
- use Tymon\JWTAuth\Contracts\JWTSubject;
- class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
- {
- use Authenticatable, Authorizable;
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- // protected $fillable = [
- // 'name', 'id'
- // ];
- protected $guarded = [];
- /**
- * The attributes excluded from the model's JSON form.
- *
- * @var array
- */
- protected $hidden = [
- 'password',
- ];
- /**
- * Get the identifier that will be stored in the subject claim of the JWT.
- *
- * @return mixed
- */
- public function getJWTIdentifier()
- {
- return $this->getKey();
- }
- /**
- * Return a key value array, containing any custom claims to be added to the JWT.
- *
- * @return array
- */
- public function getJWTCustomClaims()
- {
- return ['role' => 'user'];
- }
- // 注册来源
- const REGISTER_SOURCE_WEAPP = 'weapp';
- const REGISTER_SOURCE_MOBILE = 'mobile';
- const REGISTER_SOURCE_ALIPAYMINI = 'alipaymini';
- public static $registerSourceMaps = [
- self::REGISTER_SOURCE_WEAPP => '微信小程序',
- self::REGISTER_SOURCE_MOBILE => '手机号',
- self::REGISTER_SOURCE_ALIPAYMINI => '支付宝小程序',
- ];
- //账号状态
- const STATUS_OK = 1;
- const STATUS_PAUSE = 0;
- public static $statusMaps = [
- self::STATUS_PAUSE => '暂停使用',
- self::STATUS_OK => '正常'
- ];
- //是否领取新用户礼
- const IS_NEW_USER_COUPONS_OK = 1;
- const IS_NEW_USER_COUPONS_NO = 0;
- public static $isNewUserCouponsMaps = [
- self::IS_NEW_USER_COUPONS_NO => '已经领取',
- self::IS_NEW_USER_COUPONS_OK => '未领取'
- ];
- // 认证状态
- const CARD_OK = 1;
- const CARD_NO = 0;
- public static $cardMaps = [
- self::CARD_OK => '已实名认证',
- self::CARD_NO => '未实名认证'
- ];
- // 押金状态
- const DEPOSIT_OK = 1;
- const DEPOSIT_NO = 0;
- public static $depositMaps = [
- self::DEPOSIT_OK => '已缴纳押金',
- self::DEPOSIT_NO => '未缴纳押金',
- ];
- const DEPOSIT_MONEY = 1;
- const DEPOSIT_CARD = 2;
- //没有用
- const DEPOSIT_COUPON = 3;
- const DEPOSIT_STUDENT = 4;
- const DEPOSIT_ALIPAY_CARD = 5;
- const DEPOSIT_TYPE_NO = 0;
- public static $depositTypeMaps = [
- self::DEPOSIT_TYPE_NO => '没有缴纳押金',
- self::DEPOSIT_MONEY => '缴纳押金',
- self::DEPOSIT_CARD => '免押金卡',
- self::DEPOSIT_COUPON => '免押金劵',
- self::DEPOSIT_STUDENT => '师生认证',
- self::DEPOSIT_ALIPAY_CARD => '支付宝预授权',
- ];
- const IS_COUPON_DEPOSIT_FREE_OK = 1;
- const IS_COUPON_DEPOSIT_FREE_NO = 0;
- public static $isCouponDepositFreeMaps = [
- self::IS_COUPON_DEPOSIT_FREE_OK => '有免押金劵',
- self::IS_COUPON_DEPOSIT_FREE_NO => '没有免押金劵',
- ];
- // 是否到了骑车年龄的状态
- const RIDE_BIKE_AGE_OK = 1;
- const RIDE_BIKE_AGE_NO = 0;
- public static $rideBikeAgeMaps = [
- self::RIDE_BIKE_AGE_OK => '已到骑车的年龄',
- self::RIDE_BIKE_AGE_NO => '未到骑车的年龄'
- ];
- // 手机号绑定状态
- const BIND_MOBILE_OK = 1;
- const BIND_MOBILE_NO = 0;
- public static $bingMobileMaps = [
- self::BIND_MOBILE_OK => '已绑定收手机号',
- self::BIND_MOBILE_NO => '未绑定手机号'
- ];
- // 用户授权状态
- const REGISTER_OK = 1;
- const REGISTER_NO = 0;
- public static $registerMaps = [
- self::REGISTER_OK => '已授权',
- self::REGISTER_NO => '未授权'
- ];
- // 用户授权状态
- const IS_STUDENT_OK = 1;
- const IS_STUDENT_NO = 0;
- public static $isStudentMaps = [
- self::IS_STUDENT_OK => '学生',
- self::IS_STUDENT_NO => '不是'
- ];
- public function auth()
- {
- return $this->hasOne(Auth::class)->where('type', Auth::TYPE_WEAPP);
- }
- public function userPhoneDetail()
- {
- return $this->hasOne(UserPhoneDetail::class);
- }
- public function getIsCouponDepositFreeAttribute()
- {
- if ($this->attributes['is_coupon_deposit_free'] == self::IS_COUPON_DEPOSIT_FREE_NO) {
- return false;
- }
- $now = Carbon::now()->toDateTimeString();
- $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();
- 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]);
- if ($re === false) {
- User::where('id', $this->attributes['id'])->update(['is_coupon_deposit_free' => self::IS_COUPON_DEPOSIT_FREE_NO]);
- }
- return $re;
- }
- public function getIsDepositAttribute($value)
- {
- if ((int)$this->deposit_type === self::DEPOSIT_CARD && (int)$value === self::DEPOSIT_OK) {
- $deposit_expire_time = Carbon::parse($this->deposit_expire_time);
- if (Carbon::now()->gt($deposit_expire_time)) {
- //判断第一个日期是否比第二个日期大
- $this->updateDeposit($this->id);
- $this->save();
- return self::DEPOSIT_NO;
- }
- }
- return $value;
- }
- public function updateDeposit($user_id)
- {
- self::query()->where('id', $user_id)->update(['is_deposit' => User::DEPOSIT_NO, 'is_student' => self::IS_STUDENT_NO, 'deposit_type' => User::DEPOSIT_TYPE_NO]);
- }
- }
|