147 lines
3.2 KiB
PHP
147 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace app\common\model\Role;
|
|
|
|
use app\BaseModel;
|
|
use app\common\model\Menu\Menu;
|
|
use app\common\model\User\User;
|
|
use app\common\model\User\UserRole;
|
|
use think\db\Query;
|
|
use think\model\concern\SoftDelete;
|
|
use think\model\relation\BelongsToMany;
|
|
|
|
class Role extends BaseModel
|
|
{
|
|
use SoftDelete;
|
|
// 删除字段
|
|
protected $deleteTime = 'role_delete_time';
|
|
// 设置主键名
|
|
protected $pk = 'role_guid';
|
|
// 设置废弃字段
|
|
protected $disuse = [];
|
|
// 设置字段信息
|
|
protected $schema = [
|
|
'role_id' => 'int',
|
|
'role_name' => 'string',
|
|
'role_status' => 'int',
|
|
'role_create_time' => 'datetime',
|
|
'role_create_user_guid' => 'string',
|
|
'role_update_time' => 'datetime',
|
|
'role_update_user_guid' => 'string',
|
|
'role_delete_time' => 'datetime',
|
|
'role_delete_user_guid' => 'string',
|
|
'role_guid' => 'string',
|
|
];
|
|
// 开启自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'datetime';
|
|
// 创建时间
|
|
protected $createTime = 'role_create_time';
|
|
// 修改时间
|
|
protected $updateTime = 'role_update_time';
|
|
/**
|
|
* 状态 启用
|
|
*/
|
|
const STATUS_ENABLE = 1;
|
|
/**
|
|
* 状态 禁用
|
|
*/
|
|
const STATUS_DISABLE = 2;
|
|
// 状态查询范围
|
|
public function scopeStatus(Query $query, $status = self::STATUS_ENABLE)
|
|
{
|
|
$query->where('role_status', $status);
|
|
}
|
|
|
|
/**
|
|
* 新增前
|
|
*
|
|
* @date 2022-02-22
|
|
* @example
|
|
* @author admin
|
|
* @since 1.0.0
|
|
*/
|
|
public static function onBeforeInsert(self $model): void
|
|
{
|
|
$model->role_status = self::STATUS_ENABLE;
|
|
$model->completeCreateField();
|
|
}
|
|
|
|
/**
|
|
* 更新前
|
|
*
|
|
* @date 2022-02-28
|
|
* @example
|
|
* @author admin
|
|
* @since 1.0.0
|
|
*/
|
|
public static function onBeforeUpdate(self $model): void
|
|
{
|
|
$model->completeUpdateField();
|
|
}
|
|
|
|
/**
|
|
* 删除前
|
|
*
|
|
* @date 2022-02-28
|
|
* @example
|
|
* @author admin
|
|
* @since 1.0.0
|
|
*/
|
|
public static function onBeforeDelete(self $model): void
|
|
{
|
|
$model->completeDeleteField();
|
|
}
|
|
|
|
/**
|
|
* 获取状态
|
|
*
|
|
* @date 2022-02-22
|
|
* @example
|
|
* @author admin
|
|
* @since 1.0.0
|
|
*/
|
|
public function getRoleStatusTextAttr(): string
|
|
{
|
|
return [
|
|
self::STATUS_ENABLE => '启用',
|
|
self::STATUS_DISABLE => '停用'
|
|
][$this->role_status];
|
|
}
|
|
|
|
/**
|
|
* 获取角色用户
|
|
*
|
|
* @date 2022-03-02
|
|
* @example
|
|
* @author admin
|
|
* @since 1.0.0
|
|
*/
|
|
public function users(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(
|
|
User::class,
|
|
UserRole::class,
|
|
'user_guid',
|
|
'role_guid'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 获取角色菜单
|
|
*
|
|
* @date 2022-03-02
|
|
* @example
|
|
* @author admin
|
|
* @since 1.0.0
|
|
*/
|
|
public function menus(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(
|
|
Menu::class,
|
|
RoleMenu::class,
|
|
'menu_guid',
|
|
'role_guid'
|
|
);
|
|
}
|
|
}
|