self::GENDER_UNKNOWN, 'bonusable' => true, ]; /** * @var array */ protected $fillable = [ 'inviter_id', 'nickname', 'avatar', 'gender', 'birthday', 'bonusable', 'depth', 'path', 'agent_level', 'quota_v1', 'quota_v2', 'growth_value', 'group_sales_value', ]; /** * @var array */ protected $casts = [ 'birthday' => 'date', 'bonusable' => 'bool', ]; /** * @var array */ public static $agentLevelMap = [ self::AGENT_LEVEL_CIVILIAN => 'civilian', self::AGENT_LEVEL_VIP => 'vip', self::AGENT_LEVEL_COMMUNITY => 'community', self::AGENT_LEVEL_DISTRICT => 'district', self::AGENT_LEVEL_CITY => 'city', self::AGENT_LEVEL_PROVINCE => 'province', self::AGENT_LEVEL_BRANCH => 'branch', self::AGENT_LEVEL_DIRECTOR => 'director', ]; /** * {@inheritdoc} */ protected static function booted() { parent::saving(function ($userInfo) { // 如果没有邀请码,则自动分配邀请码 if ($userInfo->code === null) { do { $userInfo->code = strtolower(Str::randomAlpha(6)); } while (static::where('code', $userInfo->code)->exists()); } elseif ($userInfo->isDirty('code')) { $userInfo->code = strtolower($userInfo->code); } }); } /** * 获取此用户的邀请人信息 * * @return void */ public function inviter() { return $this->belongsTo(UserInfo::class, 'inviter_id'); } /** * 获取此用户的所有父级ID * * @return array */ public function getParentIdsAttribute(): array { if ($path = trim($this->path, '/')) { return explode($path, '/'); } return []; } /** * 获取代理角色 * * @return string */ public function getAgentRoleAttribute(): string { return static::$agentLevelMap[$this->agent_level] ?? 'unknown'; } }