调整角色&系统&脚手架写法
This commit is contained in:
@@ -2,10 +2,17 @@
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\traits\crud\CrudLogicTrait;
|
||||
use app\common\model\Corp;
|
||||
|
||||
class CorpLogic
|
||||
{
|
||||
use CrudLogicTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = Corp::class;
|
||||
|
||||
public function getList()
|
||||
{
|
||||
|
||||
@@ -8,86 +8,7 @@ use support\Log;
|
||||
|
||||
class RoleLogic
|
||||
{
|
||||
/**
|
||||
* 获取角色列表
|
||||
*/
|
||||
public function getRoleList(array $params)
|
||||
{
|
||||
// 假设我们需要根据system_id进行过滤,其他过滤条件也可以添加
|
||||
$query = Role::query();
|
||||
|
||||
|
||||
if (isset($params['system_id'])) {
|
||||
$query->where('system_id', $params['system_id']);
|
||||
}
|
||||
if (isset($params['status'])) {
|
||||
$query->where('status', $params['status']);
|
||||
}
|
||||
|
||||
// 添加分页
|
||||
$roles = $query->paginate($params['per_page'] ?? 15);
|
||||
|
||||
return $roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色详情
|
||||
*/
|
||||
public function getRoleDetail(int $roleId)
|
||||
{
|
||||
$role = Role::find($roleId);
|
||||
if (!$role) {
|
||||
throw new BusinessException('角色不存在', 404);
|
||||
}
|
||||
|
||||
return $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建角色
|
||||
*/
|
||||
public function createRole(array $data)
|
||||
{
|
||||
$role = new Role();
|
||||
$role->fill($data); // 根据字段填充数据
|
||||
try {
|
||||
$role->save();
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('创建角色失败:'.$e->getMessage());
|
||||
throw new BusinessException('创建角色失败', 500);
|
||||
}
|
||||
|
||||
return $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新角色
|
||||
*/
|
||||
public function updateRole(array $data)
|
||||
{
|
||||
$role = Role::find($data['id']);
|
||||
if (!$role) {
|
||||
throw new BusinessException('角色不存在', 404);
|
||||
}
|
||||
|
||||
$role->update($data); // 更新字段
|
||||
return $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*/
|
||||
public function deleteRole(int $roleId)
|
||||
{
|
||||
$role = Role::find($roleId);
|
||||
if (!$role) {
|
||||
throw new BusinessException('角色不存在', 404);
|
||||
}
|
||||
|
||||
try {
|
||||
$role->delete();
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('删除角色失败:'.$e->getMessage());
|
||||
throw new BusinessException('删除角色失败', 500);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,13 +2,20 @@
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\traits\crud\CrudLogicTrait;
|
||||
use app\common\model\Systems;
|
||||
|
||||
class SystemsLogic
|
||||
{
|
||||
use CrudLogicTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = Systems::class;
|
||||
|
||||
public function getList()
|
||||
{
|
||||
return Systems::where('status', 1)->orderBy('sort', 'asc')->get(['id', 'name', 'code', 'icon', 'type']);
|
||||
return Systems::where('status', 1)->orderBy('sort', 'asc')->get(['id', 'name', 'code', 'type', 'type']);
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,14 @@ class Role extends Model
|
||||
public $timestamps = true;
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
protected $fillable = [
|
||||
'system_id',
|
||||
'name',
|
||||
'desc',
|
||||
'sort',
|
||||
'status'
|
||||
];
|
||||
|
||||
// 角色下所有用户
|
||||
public function users()
|
||||
|
||||
@@ -4,21 +4,6 @@ namespace app\common\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
/**
|
||||
* systems 系统/应用表
|
||||
* @property integer $id 系统ID(主键)
|
||||
* @property string $code 系统编码(唯一英文标识)
|
||||
* @property string $name 系统名称
|
||||
* @property string $type 系统类型
|
||||
* @property string $desc 系统描述
|
||||
* @property string $icon 系统图标
|
||||
* @property string $domain_url 系统URL
|
||||
* @property string $is_role 是否区分角色权限
|
||||
* @property integer $sort 排序
|
||||
* @property integer $status 状态
|
||||
* @property integer $created_at 创建时间
|
||||
* @property integer $updated_at 更新时间
|
||||
*/
|
||||
class Systems extends Model
|
||||
{
|
||||
/**
|
||||
@@ -50,15 +35,15 @@ class Systems extends Model
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
// 系统下的资源
|
||||
public function resources()
|
||||
{
|
||||
return $this->hasMany(Resource::class, 'system_id', 'id');
|
||||
}
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
// 拥有该系统访问权限的用户
|
||||
public function users()
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'user_systems', 'system_id', 'user_id');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'code', 'name', 'type', 'desc', 'icon', 'domain_url', 'is_role', 'sort', 'status', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -64,27 +64,6 @@ class Generate
|
||||
return $ControllerStub . $ServiceStub . $LogicStub . $ModelStub . $ValidatorStub . "<info>文件生成成功!</info> <br/>";
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字段
|
||||
*
|
||||
* @param array $fields 字段数组
|
||||
*/
|
||||
public function fillableFields()
|
||||
{
|
||||
$tableName = $this->options['table'];
|
||||
$fields = TableInspector::getColumnNames($tableName);
|
||||
$str = '';
|
||||
foreach ($fields as $key => $val) {
|
||||
if ($key < (count($fields)-1)) {
|
||||
$str .= "'{$val}', ";
|
||||
} else {
|
||||
$str .= "'{$val}'";
|
||||
}
|
||||
}
|
||||
$fields = trim($str, '"');
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据模板生成文件
|
||||
*
|
||||
@@ -141,6 +120,27 @@ class Generate
|
||||
return "生成文件: <info>{$outputPath}</info> <br/>";
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字段
|
||||
*
|
||||
* @param array $fields 字段数组
|
||||
*/
|
||||
public function fillableFields()
|
||||
{
|
||||
$tableName = $this->options['table'];
|
||||
$fields = TableInspector::getColumnNames($tableName);
|
||||
$str = '';
|
||||
foreach ($fields as $key => $val) {
|
||||
if ($key < (count($fields)-1)) {
|
||||
$str .= "'{$val}', ";
|
||||
} else {
|
||||
$str .= "'{$val}'";
|
||||
}
|
||||
}
|
||||
$fields = trim($str, '"');
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果文件存在,删除它
|
||||
*
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class SystemsValidate extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
];
|
||||
|
||||
// 提示
|
||||
protected $message = [
|
||||
|
||||
];
|
||||
|
||||
// 场景
|
||||
protected $scene = [
|
||||
'store' => [
|
||||
|
||||
],
|
||||
|
||||
'update' => [
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user