server/app/Model/Account.php

128 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
/**
* @property int $account_id
* @property int $account_type
* @property int $belong_id
* @property int $dept_id
* @property string $username
* @property string $password
* @property string $salt
* @property int $master_flag
* @property int $status
* @property string $nickname
* @property string $avatar
* @property string $bio
* @property string $tags
* @property int $sex
* @property string $birthday
* @property string $deleted_at
* @property string $create_time
* @property string $update_time
*/
class Account extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'account';
protected string $primaryKey = 'account_id';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['account_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'dept_id' => 'integer', 'master_flag' => 'integer', 'status' => 'integer', 'sex' => 'integer'];
public static function getByUsername(string $username, array $field = ['*'])
{
return self::query()->where("username", $username)->first($field);
}
public static function getAuths(int $account_id, int $account_type, int $master_flag)
{
return $master_flag ? Menu::getAuth1($account_type) : Menu::getAuth2($account_id, $account_type);
}
public static function getInfo(int $account_id): array
{
$info = self::with('roles')
->with('posts')
->with('dept')
->select(['account_id', 'username', 'nickname', 'avatar', 'bio', 'tags', 'sex', 'birthday', 'create_time'])
->find($account_id);
return $info->toArray();
}
public static function getById(int $account_id, array $field = ['*']): array
{
$info = self::where("account_id", $account_id)
->select($field)
->first();
return $info->toArray();
}
public static function getMenu(array $account)
{
// 总后台账号
$field = ['title', 'path', 'pid', 'name', 'menu_id', 'icon', 'hidden'];
// 获取角色
$roles = match ($account['account_type']) {
1 => ["ADMIN"],
2 => ["ORG"],
3 => ["MERCHANT"],
default => []
};
// 标识
if ($account['master_flag']) {
$menus = Menu::getMenu($account['account_type'], $field);
$buttons = Menu::getButton($account['account_type']);
$roles[] = 'MAIN';
} else {
$menus = Menu::getMenu2($account['account_id'], $account['account_type'], $field);
$buttons = Menu::getButton2($account['account_id'], $account['account_type']);
$roles[] = 'CHILD';
}
// 获取商户行业标识
return compact("menus", "buttons", "roles");
}
public function roles()
{
return $this->belongsToMany(
Role::class,
'account_role',
'account_id',
'role_id'
);
}
public function posts()
{
return $this->belongsToMany(
Post::class,
'account_post',
'account_id',
'post_id'
);
}
public function dept()
{
return $this->hasOne(
Dept::class,
'dept_id',
'dept_id'
);
}
}