浏览代码

自定义表单

rkljw 2 天之前
父节点
当前提交
8ce94f2481

+ 153 - 0
app/JsonRpc/FormService.php

@@ -0,0 +1,153 @@
+<?php
+namespace App\JsonRpc;
+
+
+use Hyperf\RpcServer\Annotation\RpcService;
+use App\Tools\Result;
+use App\Model\GlobalTable;
+use Hyperf\DbConnection\Db;
+use App\Model\GlobalTableField;
+
+#[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
+class FormService implements FormServiceInterface
+{
+
+    /**
+     * 添加全局表单
+     * @param array $data
+     * @return array|mixed
+     */
+    public function addGlobalTable(array $data): array
+    {
+        // 过滤掉空值
+        $data = array_filter($data, function($value) {
+            return !empty($value);
+        });
+        // 检查是否已存在相同名称的记录
+        $globalTable = GlobalTable::on('global')->where(['table'=> $data['table'],'website_id'=>$data['website_id']])->first();
+        if (empty($globalTable)) {
+            $id = GlobalTable::on('global')->insertGetId($data);
+            //给global库创建表 表名为$data['table'] 的值,并初始化一个字段id,类型为int,自增,主键,再初始化一个字段title,类型为varchar,长度为255
+            Db::connection('global')->statement("CREATE TABLE IF NOT EXISTS `{$data['table']}` (
+                `id` int(11) NOT NULL AUTO_INCREMENT,
+                `title` varchar(255) DEFAULT NULL,
+                PRIMARY KEY (`id`)
+            )");
+            //给GlobalTableField表初始化一条数据,表table_id为$id,field_name为title字符串,title为标题,field_type为varchar,length为255,is_required为1,is_unique为0,is_primary为0,is_index为0,is_foreign为0,is_auto_increment为1,is_default为0,default_value为'',description为'',website_id为$data['website_id']
+            GlobalTableField::on('global')->insert([
+                    'table_id' => $id,
+                    'field_name' => 'title',
+                    'title' => '标题',
+                    'field_type' => '1',
+                    'length' => 255,
+                    'sort' => 0,
+                    'is_check' => 1,
+                    'admin_display' => 1,
+                    'home_display' => 1,
+                ]);
+ 
+            return Result::success('添加成功');
+        }
+        return Result::error('表单已存在');
+    }
+    /**
+     * 获取全局表单列表
+     * @param array $data
+     * @return array
+     */
+    public function getGlobalTableList(array $data): array
+    {
+        try {
+            // 构建查询
+            $query = GlobalTable::query()
+                ->when(!empty($data['website_id']), fn($q) => $q->where('website_id', $data['website_id']))
+                ->when(!empty($data['name']), fn($q) => $q->where('name', 'like', '%' . $data['name'] . '%'));
+            
+            // 分页参数
+            $page = (int)($data['page'] ?? 1);
+            $pageSize = (int)($data['pageSize'] ?? 10);
+            
+            // 获取数据
+            $list = $query->orderBy('updated_at', 'desc')
+                         ->offset(($page - 1) * $pageSize)
+                         ->limit($pageSize)
+                         ->get();
+            
+            // 如果没有数据,直接返回空结果
+            if ($list->isEmpty()) {
+                return Result::success([
+                    'list' => [],
+                    'total' => 0,
+                    'page' => $page,
+                    'pageSize' => $pageSize
+                ]);
+            }
+            
+            // 获取并合并 website 数据
+            $websites = Db::connection('secondary')
+                ->table('website')
+                ->whereIn('id', $list->pluck('website_id'))
+                ->pluck('website_name', 'id');
+            
+            // 合并数据并返回
+            return Result::success([
+                'list' => $list->map(function($item) use ($websites) {
+                    $item->website_name = $websites[$item->website_id] ?? '';
+                    return $item;
+                }),
+                'total' => $query->count(),
+                'page' => $page,
+                'pageSize' => $pageSize
+            ]);
+            
+        } catch (\Throwable $e) {
+            return Result::error('查询失败:' . $e->getMessage());
+        }
+    }
+    /**
+     * 获取全局表单
+     * @param array $data
+     * @return array
+     */
+    public function getGlobalTable(array $data): array
+    {
+        $globalTable = GlobalTable::where('id',$data['id'])->first();
+        return Result::success($globalTable);
+    }
+    /**
+     * 修改全局表单
+     * @param array $data
+     * @return array
+     */
+    public function upGlobalTable(array $data): array   
+    {
+        var_dump($data);
+        $globalTable = GlobalTable::where('id',$data['id'])->first();
+        if(empty($globalTable)){
+            return Result::error('表单不存在');
+        }   
+        $data = array_filter($data,function($value){
+            return !empty($value);
+        });
+        GlobalTable::where(['id'=>$data['id']])->update($data);
+        return Result::success('修改成功');
+    }   
+    /**
+     * 删除全局表单
+     * @param array $data
+     * @return array
+     */
+    public function delGlobalTable(array $data): array          
+    {
+        $globalTable = GlobalTable::where('id', $data['id'])->first();
+        if(empty($globalTable)){
+            return Result::error('表单不存在');
+        }
+        //删除global库的表
+        Db::connection('global')->statement("DROP TABLE IF EXISTS `{$globalTable->table}`");
+        //删除GlobalTableField表中table_id为$data['id']的数据
+        GlobalTableField::where('table_id', $data['id'])->delete();
+        GlobalTable::where('id', $data['id'])->delete();
+        return Result::success('删除成功');
+    }
+}

+ 35 - 0
app/JsonRpc/FormServiceInterface.php

@@ -0,0 +1,35 @@
+<?php
+namespace App\JsonRpc;
+interface FormServiceInterface
+{
+    
+    /**
+     * @param array $data
+     *  @return array
+    */
+    public function addGlobalTable(array $data):array;
+    /**
+     * @param array $data
+     *  @return array
+    */
+    public function getGlobalTableList(array $data):array;
+    /**
+     * @param array $data
+     *  @return array
+     */
+    public function getGlobalTable(array $data):array;
+    /**
+     * @param array $data
+     *  @return array
+    */
+    public function upGlobalTable(array $data):array;   
+    /**
+     * @param array $data
+     *  @return array
+    */
+    public function delGlobalTable(array $data):array;  
+    
+
+}
+
+

+ 34 - 0
app/Model/GlobalTable.php

@@ -0,0 +1,34 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class GlobalTable extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'global_table';
+    /**
+     * The connection name for the model.
+     */
+    protected ?string $connection = 'global';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [
+        'website_id'
+    ];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+
+}

+ 34 - 0
app/Model/GlobalTableField.php

@@ -0,0 +1,34 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class GlobalTableField extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'global_table_field';
+    /**
+     * The connection name for the model.
+     */
+    protected ?string $connection = 'global';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    // protected array $fillable = [
+    //     'website_id'
+    // ];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+
+}

+ 31 - 0
app/Model/Website.php

@@ -0,0 +1,31 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+/**
+ */
+class Website extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'website';
+    /**
+     * The connection name for the model.
+     */
+    protected ?string $connection = 'secondary';
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+
+   
+}

+ 102 - 73
config/autoload/databases.php

@@ -1,73 +1,102 @@
-<?php
-
-declare(strict_types=1);
-/**
- * This file is part of Hyperf.
- *
- * @link     https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact  group@hyperf.io
- * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
-use function Hyperf\Support\env;
-
-return [
-    'default' => [
-        'driver' => env('DB_DRIVER2', 'mysql'),
-        'host' => env('DB_HOST2', 'localhost'),
-        'database' => env('DB_DATABASE2', 'hyperf'),
-        'port' => env('DB_PORT2', 3306),
-        'username' => env('DB_USERNAME2', 'root'),
-        'password' => env('DB_PASSWORD2', ''),
-        'charset' => env('DB_CHARSET2', 'utf8'),
-        'collation' => env('DB_COLLATION2', 'utf8_unicode_ci'),
-        'prefix' => env('DB_PREFIX2', ''),
-        'pool' => [
-            'min_connections' => 1,
-            'max_connections' => 10,
-            'connect_timeout' => 10.0,
-            'wait_timeout' => 3.0,
-            'heartbeat' => -1,
-            'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
-        ],
-        'variables' => [
-            'time_zone' => '+08:00', // 设置MySQL连接的时区
-        ],
-        'commands' => [
-            'gen:model' => [
-                'path' => 'app/Model',
-                'force_casts' => true,
-                'inheritance' => 'Model',
-            ],
-        ],
-    ],
-    'secondary' => [
-        'driver' => env('DB_DRIVER', 'mysql'),
-        'host' => env('DB_HOST', 'localhost'),
-        'database' => env('DB_DATABASE', 'hyperf'),
-        'port' => env('DB_PORT', 3306),
-        'username' => env('DB_USERNAME', 'root'),
-        'password' => env('DB_PASSWORD', ''),
-        'charset' => env('DB_CHARSET', 'utf8'),
-        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
-        'prefix' => env('DB_PREFIX', ''),
-        'pool' => [
-            'min_connections' => 1,
-            'max_connections' => 10,
-            'connect_timeout' => 10.0,
-            'wait_timeout' => 3.0,
-            'heartbeat' => -1,
-            'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
-        ],
-        'variables' => [
-            'time_zone' => '+08:00', // 设置MySQL连接的时区
-        ],
-        'commands' => [
-            'gen:model' => [
-                'path' => 'app/Model',
-                'force_casts' => true,
-                'inheritance' => 'Model',
-            ],
-        ],
-    ],
-];
+<?php
+
+declare(strict_types=1);
+/**
+ * This file is part of Hyperf.
+ *
+ * @link     https://www.hyperf.io
+ * @document https://hyperf.wiki
+ * @contact  group@hyperf.io
+ * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
+ */
+use function Hyperf\Support\env;
+
+return [
+    'default' => [
+        'driver' => env('DB_DRIVER2', 'mysql'),
+        'host' => env('DB_HOST2', 'localhost'),
+        'database' => env('DB_DATABASE2', 'hyperf'),
+        'port' => env('DB_PORT2', 3306),
+        'username' => env('DB_USERNAME2', 'root'),
+        'password' => env('DB_PASSWORD2', ''),
+        'charset' => env('DB_CHARSET2', 'utf8'),
+        'collation' => env('DB_COLLATION2', 'utf8_unicode_ci'),
+        'prefix' => env('DB_PREFIX2', ''),
+        'pool' => [
+            'min_connections' => 1,
+            'max_connections' => 10,
+            'connect_timeout' => 10.0,
+            'wait_timeout' => 3.0,
+            'heartbeat' => -1,
+            'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
+        ],
+        'variables' => [
+            'time_zone' => '+08:00', // 设置MySQL连接的时区
+        ],
+        'commands' => [
+            'gen:model' => [
+                'path' => 'app/Model',
+                'force_casts' => true,
+                'inheritance' => 'Model',
+            ],
+        ],
+    ],
+    'secondary' => [
+        'driver' => env('DB_DRIVER', 'mysql'),
+        'host' => env('DB_HOST', 'localhost'),
+        'database' => env('DB_DATABASE', 'hyperf'),
+        'port' => env('DB_PORT', 3306),
+        'username' => env('DB_USERNAME', 'root'),
+        'password' => env('DB_PASSWORD', ''),
+        'charset' => env('DB_CHARSET', 'utf8'),
+        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
+        'prefix' => env('DB_PREFIX', ''),
+        'pool' => [
+            'min_connections' => 1,
+            'max_connections' => 10,
+            'connect_timeout' => 10.0,
+            'wait_timeout' => 3.0,
+            'heartbeat' => -1,
+            'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
+        ],
+        'variables' => [
+            'time_zone' => '+08:00', // 设置MySQL连接的时区
+        ],
+        'commands' => [
+            'gen:model' => [
+                'path' => 'app/Model',
+                'force_casts' => true,
+                'inheritance' => 'Model',
+            ],
+        ],
+    ],
+    'global' => [
+        'driver' => env('DB_DRIVER3', 'mysql'),
+        'host' => env('DB_HOST3', 'localhost'),
+        'database' => env('DB_DATABASE3', 'hyperf'),
+        'port' => env('DB_PORT3', 3306),
+        'username' => env('DB_USERNAME3', 'root'),
+        'password' => env('DB_PASSWORD3', ''),
+        'charset' => env('DB_CHARSET3', 'utf8'),
+        'collation' => env('DB_COLLATION3', 'utf8_unicode_ci'),
+        'prefix' => env('DB_PREFIX3', ''),
+        'pool' => [
+            'min_connections' => 1,
+            'max_connections' => 10,
+            'connect_timeout' => 10.0,
+            'wait_timeout' => 3.0,
+            'heartbeat' => -1,
+            'max_idle_time' => (float) env('DB_MAX_IDLE_TIME3', 60),
+        ],
+        'variables' => [
+            'time_zone' => '+08:00', // 设置MySQL连接的时区
+        ],
+        'commands' => [
+            'gen:model' => [
+                'path' => 'app/Model',
+                'force_casts' => true,
+                'inheritance' => 'Model',
+            ],
+        ],
+    ],
+];