6
0
Fork 0
jiqu-library-server/app/Models/UserInfo.php

133 lines
3.0 KiB
PHP

<?php
namespace App\Models;
use App\Helpers\Str;
use Illuminate\Database\Eloquent\Model;
class UserInfo extends Model
{
public const GENDER_UNKNOWN = 'unknown'; // 未知
public const GENDER_MALE = 'male'; // 男性
public const GENDER_FEMALE = 'female'; // 女性
public const AGENT_LEVEL_CIVILIAN = 0;
public const AGENT_LEVEL_VIP = 1;
public const AGENT_LEVEL_COMMUNITY = 2;
public const AGENT_LEVEL_DISTRICT = 3;
public const AGENT_LEVEL_CITY = 4;
public const AGENT_LEVEL_PROVINCE = 5;
public const AGENT_LEVEL_BRANCH = 6;
public const AGENT_LEVEL_DIRECTOR = 7;
/**
* @var string
*/
protected $primaryKey = 'user_id';
/**
* @var bool
*/
public $incrementing = false;
/**
* @var array
*/
protected $attributes = [
'gender' => 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';
}
}