Преглед на файлове

feat(template): 添加Admin和Api控制器模板

- 新增Admin控制器模板,包含完整的CRUD操作方法
- 新增Api控制器模板 控制器模板中,支持
-RESTful API接口集成用户权限验证逻辑
- 实现标准的响应格式处理(page_response/success_response)- 添加详细的PHPDoc注释和方法说明
- 支持自动生成方法参数和返回值类型提示
- 集成中间件支持和路由配置
- 添加基础的参数获取和校验逻辑占位符
- 实现与Entity层的标准调用方式
- 支持自动命名空间和类名生成
runphp преди 6 месеца
родител
ревизия
8991e6437e

+ 112 - 0
templates/src/Controller/Admin/Controller.php.tpl.php

@@ -0,0 +1,112 @@
+<?= "<?php\n" ?>
+declare(strict_types=1);
+
+namespace <?= $namespace ?>Controller\Admin;
+
+use <?= $namespace ?>Model\<?= $modelName ?>;
+use <?= $namespace ?>Entity\<?= $entityName ?>;
+use SixShop\Core\Request;
+use SixShop\System\Middleware\MacroPageMiddleware;
+use think\Response;
+use think\Route;
+use function SixShop\Core\page_response;
+use function SixShop\Core\success_response;
+
+/**
+ * <?= $tableComment ?: $tableName ?> * 
+ * Admin Management Controller for <?= $tableName ?> resource
+ * 
+ * @package <?= $namespace ?>Controller\Admin
+ */
+class <?= $controllerName ?>{
+
+    protected array $middlewares = [
+        MacroPageMiddleware::class
+    ];
+    /**
+     * Display a listing of the resource
+     * GET /{resource}
+     */
+    public function index(Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $params =  $request->get(); // 实际项目中请根据实际情况实现具体参数处理和校验
+        $userID = $request->userID; // // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $page = $<?= lcfirst($entityName) ?>->index<?= ucfirst(str_replace('_', '', $tableName)) ?>($params, $request->pageAndLimit());
+        $data = []; // 分页之外的其他数据
+        return page_response(page:$page, data:$data, msg:'获取列表数据成功');
+    }
+    
+    /**
+     * Show the form for creating a new resource
+     * GET /{resource}/create
+     */
+    public function create(Request $request,<?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $userID = $request->userID; // // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data = $<?= lcfirst($entityName) ?>->create<?= ucfirst(str_replace('_', '', $tableName)) ?>();
+        return success_response(data:$data, msg:'获取新增数据表单成功');
+    }
+    
+    /**
+     * Store a newly created resource in storage
+     * POST /{resource}
+     */
+    public function save(Request $request,<?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $params = $request->post(); // 实际项目中请根据实际情况实现具体参数处理和校验
+        $userID = $request->userID; // // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data= $<?= lcfirst($entityName) ?>->save<?= ucfirst(str_replace('_', '', $tableName)) ?>($params);
+        return success_response(data:$data, msg:'保存成功');
+    }
+    
+    /**
+     * Display the specified resource
+     * GET /{resource}/{id}
+     */
+    public function read(int $id, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        // Call entity method
+        $data= $<?= lcfirst($entityName) ?>->read<?= ucfirst(str_replace('_', '', $tableName)) ?>($id);
+        return success_response(data:$data, msg:'获取详情数据成功');
+    }
+    
+    /**
+     * Show the form for editing the specified resource
+     * GET /{resource}/{id}/edit
+     */
+    public function edit(int $id, Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $userID = $request->userID; // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data= $<?= lcfirst($entityName) ?>->edit<?= ucfirst(str_replace('_', '', $tableName)) ?>($id);
+        return success_response(data:$data, msg:'获取编辑数据表单成功');
+    }
+    
+    /**
+     * Update the specified resource in storage
+     * PUT/PATCH /{resource}/{id}
+     */
+    public function update(int $id, Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $params = $request->put(); // 实际项目中请根据实际情况实现具体参数处理和校验
+        $userID = $request->userID;  // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data= $<?= lcfirst($entityName) ?>->update<?= ucfirst(str_replace('_', '', $tableName)) ?>($id, $params);
+        return success_response(data:$data, msg:'更新成功');
+    }
+    
+    /**
+     * Remove the specified resource from storage
+     * DELETE /{resource}/{id}
+     */
+    public function delete(int $id, Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $userID = $request->userID; // // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data = $<?= lcfirst($entityName) ?>->delete<?= ucfirst(str_replace('_', '', $tableName)) ?>($id);
+        return success_response(data:$data, msg:'删除成功');
+    }
+}

+ 108 - 0
templates/src/Controller/Api/Controller.php.tpl.php

@@ -0,0 +1,108 @@
+<?= "<?php\n" ?>
+declare(strict_types=1);
+
+namespace <?= $namespace ?>Controller\Api;
+
+use <?= $namespace ?>Model\<?= $modelName ?>;
+use <?= $namespace ?>Entity\<?= $entityName ?>;
+use SixShop\Core\Request;
+use think\Response;
+use function SixShop\Core\page_response;
+use function SixShop\Core\success_response;
+
+/**
+ * <?= $tableComment ?: $tableName ?> API
+ * 
+ * RESTful API Controller for <?= $tableName ?> resource
+ * 
+ * @package <?= $namespace ?>Controller\Api
+ */
+class <?= $controllerName ?>
+{
+    /**
+     * Display a listing of the resource
+     * GET /{resource}
+     */
+    public function index(Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $params =  $request->get(); // 实际项目中请根据实际情况实现具体参数处理和校验
+        $userID = $request->userID; // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $page = $<?= lcfirst($entityName) ?>->index<?= ucfirst(str_replace('_', '', $tableName)) ?>($params, $request->pageAndLimit());
+        $data = []; // 分页之外的其他数据
+        return page_response(page:$page, data:$data, msg:'获取列表数据成功');
+    }
+    
+    /**
+     * Show the form for creating a new resource
+     * GET /{resource}/create
+     */
+    public function create(Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $userID = $request->userID; // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data = $<?= lcfirst($entityName) ?>->create<?= ucfirst(str_replace('_', '', $tableName)) ?>();
+        return success_response(data:$data, msg:'获取新增数据表单成功');
+    }
+    
+    /**
+     * Store a newly created resource in storage
+     * POST /{resource}
+     */
+    public function save(Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $params = $request->post(); // 实际项目中请根据实际情况实现具体参数处理和校验
+        $userID = $request->userID; // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data= $<?= lcfirst($entityName) ?>->save<?= ucfirst(str_replace('_', '', $tableName)) ?>($params);
+        return success_response(data:$data, msg:'保存成功');
+    }
+    
+    /**
+     * Display the specified resource
+     * GET /{resource}/{id}
+     */
+    public function read(int $id, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        // Call entity method
+        $data= $<?= lcfirst($entityName) ?>->read<?= ucfirst(str_replace('_', '', $tableName)) ?>($id);
+        return success_response(data:$data, msg:'获取详情数据成功');
+    }
+    
+    /**
+     * Show the form for editing the specified resource
+     * GET /{resource}/{id}/edit
+     */
+    public function edit(int $id, Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $userID = $request->userID; // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data= $<?= lcfirst($entityName) ?>->edit<?= ucfirst(str_replace('_', '', $tableName)) ?>($id);
+        return success_response(data:$data, msg:'获取编辑数据表单成功');
+    }
+    
+    /**
+     * Update the specified resource in storage
+     * PUT/PATCH /{resource}/{id}
+     */
+    public function update(int $id, Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $params = $request->put(); // 实际项目中请根据实际情况实现具体参数处理和校验
+        $userID = $request->userID;  // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data= $<?= lcfirst($entityName) ?>->update<?= ucfirst(str_replace('_', '', $tableName)) ?>($id, $params);
+        return success_response(data:$data, msg:'更新成功');
+    }
+    
+    /**
+     * Remove the specified resource from storage
+     * DELETE /{resource}/{id}
+     */
+    public function delete(int $id, Request $request, <?= $entityName ?> $<?= lcfirst($entityName) ?>): Response
+    {
+        $userID = $request->userID; // 获取用户ID, 实际项目可能需要判断用户所有权限
+        // Call entity method
+        $data = $<?= lcfirst($entityName) ?>->delete<?= ucfirst(str_replace('_', '', $tableName)) ?>($id);
+        return success_response(data:$data, msg:'删除成功');
+    }
+}

+ 110 - 0
templates/src/Entity/Entity.php.tpl.php

@@ -0,0 +1,110 @@
+<?= "<?php\n" ?>
+declare(strict_types=1);
+
+namespace <?= $namespace ?>Entity;
+
+use <?= $namespace ?>Model\<?= $modelName ?>;
+use SixShop\Core\Entity\BaseEntity;
+use think\Collection;
+use think\Paginator;
+use function SixShop\Core\throw_logic_exception;
+
+/**
+ * <?= $tableComment ?: $entityName . ' Entity' ?>
+<?php if (!empty($tableComment)): ?>
+ * 
+ * Table: <?= $tableName ?> - <?= $tableComment ?>
+<?php endif; ?>
+ * 
+ * @package <?= $namespace ?>Entity
+ */
+class <?= $entityName ?> extends BaseEntity
+{
+<?php 
+$methodName = ucfirst(str_replace('_', '', $tableName));
+// Generate singular method name based on table name
+$singularMethodName = $methodName;
+if (str_ends_with($methodName, 's') && !str_ends_with($methodName, 'ss')) {
+    $singularMethodName = substr($methodName, 0, -1);
+}
+?>
+    /**
+     * Display a listing of the resource
+     */
+    public function index<?= $singularMethodName ?>(array $params, array $pageAndLimit = []):Paginator| Collection
+    {
+        $query = $this->withSearch([
+            'id',
+            // ... 请根据实际情况完成需要搜索的字段
+        ], $params);
+        if (!empty($pageAndLimit)) {
+            return $query->paginate($pageAndLimit);
+        } else {
+            return $query->select();
+        }
+    }
+    
+    /**
+     * Show the form for creating a new resource
+     */
+    public function create<?= $singularMethodName ?>(): array
+    {
+        return [
+            // ... 请根据实际情况完成新建数据的表单字段
+        ];
+    }
+    
+    /**
+     * Store a newly created resource in storage
+     */
+    public function save<?= $singularMethodName ?>(array $data): <?= $modelName ?>
+    {
+        $model = new <?= $modelName ?>();
+        $model->save($data);
+        return $model;
+    }
+    
+    /**
+     * Display the specified resource
+     */
+    public function read<?= $singularMethodName ?>(int $id):self
+    {
+        return $this->find($id);
+    }
+    
+    /**
+     * Show the form for editing the specified resource
+     */
+    public function edit<?= $singularMethodName ?>(int $id): array
+    {
+        // TODO: Implement edit form logic
+        return [
+            // ... 请根据实际情况完成编辑数据的表单字段
+        ];
+    }
+    
+    /**
+     * Update the specified resource in storage
+     */
+    public function update<?= $singularMethodName ?>(int $id, array $params): self
+    {
+        $entity = $this->findOrEmpty($id);
+        if ($entity->isEmpty()) {
+            throw_logic_exception('数据不存在');
+        }
+        $entity->save($params);
+        return $entity;
+    }
+    
+    /**
+     * Remove the specified resource from storage
+     */
+    public function delete<?= $singularMethodName ?>(int $id): bool
+    {
+        $entity = $this->findOrEmpty($id);
+        if ($entity->isEmpty()) {
+            throw_logic_exception('数据不存在');
+        }
+        return $entity->delete();
+    }
+}

+ 158 - 0
templates/src/Model/Model.php.tpl.php

@@ -0,0 +1,158 @@
+<?= "<?php\n" ?>
+declare(strict_types=1);
+
+namespace <?= $namespace ?>Model;
+
+use think\Model;
+
+/**
+ * <?= $tableComment ?: $modelName . ' Model' ?>
+<?php if (!empty($tableComment)): ?>
+ * 
+ * Table: <?= $tableName ?> - <?= $tableComment ?>
+<?php endif; ?>
+ * 
+<?php if (!empty($fields)): ?>
+<?php foreach ($fields as $field): ?>
+ * @property <?php
+        switch ($field['type']) {
+            case 'boolean':
+                echo 'bool';
+                break;
+            case 'integer':
+            case 'biginteger':
+                echo 'int';
+                break;
+            case 'decimal':
+            case 'float':
+                echo 'float';
+                break;
+            case 'json':
+                echo 'array';
+                break;
+            case 'datetime':
+            case 'timestamp':
+            case 'date':
+            case 'time':
+                echo 'string';
+                break;
+            default:
+                echo 'string';
+        }
+    ?> $<?= $field['name'] ?><?= !empty($field['comment']) ? ' ' . $field['comment'] : '' ?>
+<?php endforeach; ?>
+<?php endif; ?>
+ * @package <?= $namespace ?>Model
+ */
+class <?= $modelName ?> extends Model
+{
+    /**
+     * 获取模型选项配置
+     * 
+     * @return array
+     */
+    protected function getOptions(): array
+    {
+        return [
+            // 表名
+            'name' => '<?= $tableName ?>',
+            
+            // 主键
+            'pk' => 'id',
+            
+            // 自动时间戳
+            'auto_timestamp' => 'datetime',
+            
+            // 创建时间字段
+            'create_time' => 'create_time',
+            
+            // 更新时间字段
+            'update_time' => 'update_time',
+            
+            // 软删除时间字段
+            'delete_time' => false,
+<?php if (!empty($fields)): ?>
+            
+            // 字段类型转换
+            'type' => [
+<?php foreach ($fields as $field): ?>
+<?php if ($field['type'] === 'boolean'): ?>
+                '<?= $field['name'] ?>' => 'boolean',
+<?php elseif ($field['type'] === 'integer' || $field['type'] === 'biginteger'): ?>
+                '<?= $field['name'] ?>' => 'integer',
+<?php elseif ($field['type'] === 'decimal' || $field['type'] === 'float'): ?>
+                '<?= $field['name'] ?>' => 'float',
+<?php elseif ($field['type'] === 'json'): ?>
+                '<?= $field['name'] ?>' => 'json',
+<?php elseif (in_array($field['type'], ['datetime', 'timestamp'])): ?>
+                '<?= $field['name'] ?>' => 'datetime',
+<?php elseif ($field['type'] === 'date'): ?>
+                '<?= $field['name'] ?>' => 'date',
+<?php endif; ?>
+<?php endforeach; ?>
+            ],
+<?php endif; ?>
+<?php if (!empty($fillableFields)): ?>
+            
+            // 允许批量赋值的字段
+            'field' => [
+<?php foreach ($fillableFields as $field): ?>
+                '<?= $field ?>',
+<?php endforeach; ?>
+            ],
+<?php endif; ?>
+<?php if (!empty($requiredFields)): ?>
+            
+            // 验证规则
+            'validate' => [
+                'rule' => [
+<?php foreach ($requiredFields as $field): ?>
+                    '<?= $field['name'] ?>' => 'require<?= isset($field['unique']) && $field['unique'] ? '|unique:' . $tableName : '' ?>',
+<?php endforeach; ?>
+                ],
+                'message' => [
+<?php foreach ($requiredFields as $field): ?>
+                    '<?= $field['name'] ?>.require' => '<?= $field['comment'] ?: $field['name'] ?>不能为空',
+<?php if (isset($field['unique']) && $field['unique']): ?>
+                    '<?= $field['name'] ?>.unique' => '<?= $field['comment'] ?: $field['name'] ?>已存在',
+<?php endif; ?>
+<?php endforeach; ?>
+                ],
+            ],
+<?php endif; ?>
+        ];
+    }
+    
+    /**
+     * 搜索器:根据状态查询
+     */
+    public function searchStatusAttr($query, $value)
+    {
+        if ($value !== '') {
+            $query->where('status', $value);
+        }
+    }
+    
+    /**
+     * 搜索器:根据创建时间范围查询
+     */
+    public function searchCreateTimeAttr($query, $value)
+    {
+        if (is_array($value) && count($value) === 2) {
+            $query->whereBetweenTime('create_time', $value[0], $value[1]);
+        }
+    }
+    
+<?php if (!empty($relationshipFields)): ?>
+<?php foreach ($relationshipFields as $relation): ?>
+    /**
+     * 关联查询:<?= $relation['comment'] ?>
+     */
+    public function <?= $relation['method'] ?>()
+    {
+        return $this->belongsTo(<?= $relation['model'] ?>::class, '<?= $relation['foreign_key'] ?>', '<?= $relation['local_key'] ?>');
+    }
+    
+<?php endforeach; ?>
+<?php endif; ?>
+}