Finish mzl_dev_curd

This commit is contained in:
mzl
2025-07-22 09:53:04 +08:00
12 changed files with 163 additions and 159 deletions
@@ -0,0 +1,33 @@
<?php
namespace app\admin\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\admin\service\SystemsService;
class SystemsController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new SystemsService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
// protected function indexFormat($data)
// {
// if (isset($data['created_at']))
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
// if (isset($data['updated_at']))
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
// return $data;
// }
}
-17
View File
@@ -1,17 +0,0 @@
<?php
namespace app\admin\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\common\model\Corp;
class CorpLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = Corp::class;
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace app\admin\service;
use app\common\validate\SystemsValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\Systems;
class SystemsService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = Systems::class;
/**
* 验证器
*/
protected $validator = SystemsValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
}
+7
View File
@@ -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()
{
+1 -1
View File
@@ -153,7 +153,7 @@ class PermLogic
// —— 递归构建子节点 ——
$children = [];
if ($node->type === 'directory') {
if (in_array($node->type, ['directory', 'menu'])) {
$children = $this->buildTree($resources, $node->id, $currentPath);
}
+2 -81
View File
@@ -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);
}
}
}
+8 -1
View File
@@ -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']);
}
}
+8
View File
@@ -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()
+11 -26
View File
@@ -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 = [];
}
+21 -21
View File
@@ -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;
}
/**
* 如果文件存在,删除它
*
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace app\common\validate;
use think\Validate;
class SystemsValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
+12 -12
View File
@@ -22,18 +22,17 @@ class IndexController
return htmlpage();
}
/**
* 调试飞书
*/
public function view(Request $request)
{
return view('index/qrcode', ['name' => 'webman']);
}
public function json(Request $request)
{
return json(['code' => 0, 'msg' => 'ok']);
}
// 临时增加用户权限
/**
* 临时增加用户权限
*/
public function perm(Request $request)
{
try {
@@ -61,12 +60,13 @@ class IndexController
Cache::delete("user_resources_{$data['user_id']}");
return json(['code' => 0, 'msg' => 'success']);
} catch (\Throwable $e) {
var_dump($e);
return json(['code' => 1, 'msg' => $e->getMessage()]);
}
}
/**
* 生成资源
*/
public function createResource()
{
$options = [
@@ -75,7 +75,7 @@ class IndexController
'name' => 'systems'
],
'model' => [
'module' => 'admin',
'module' => 'common',
'name' => 'systems'
],
'service' => [
@@ -83,11 +83,11 @@ class IndexController
'name' => 'systems'
],
'logic' => [
'module' => 'admin',
'module' => 'common',
'name' => 'systems'
],
'validate' => [
'module' => 'admin',
'module' => 'common',
'name' => 'systems'
],
'table' => 'systems'