rkljw 1 giorno fa
parent
commit
27b5e5954d
4 ha cambiato i file con 264 aggiunte e 4 eliminazioni
  1. 146 1
      app/JsonRpc/FormService.php
  2. 2 0
      app/JsonRpc/FormServiceInterface.php
  3. 1 0
      composer.json
  4. 115 3
      composer.lock

+ 146 - 1
app/JsonRpc/FormService.php

@@ -9,10 +9,14 @@ use Hyperf\DbConnection\Db;
 use App\Model\GlobalTableField;
 use App\Model\GlobalTableFieldType;
 use App\Model\GlobalTableFieldValue;
+use Hyperf\Di\Annotation\Inject;
+use Hyperf\Redis\Redis;
 #[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class FormService implements FormServiceInterface
 {
 
+    #[Inject]
+    protected Redis $redis;
     /**
      * 添加全局表单
      * @param array $data
@@ -32,6 +36,8 @@ class FormService implements FormServiceInterface
             Db::connection('global')->statement("CREATE TABLE IF NOT EXISTS `{$data['table']}` (
                 `id` int(11) NOT NULL AUTO_INCREMENT,
                 `title` varchar(255) DEFAULT NULL,
+                `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
+                `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                 PRIMARY KEY (`id`)
             )");
             //给GlobalTableField表初始化数据
@@ -59,7 +65,31 @@ class FormService implements FormServiceInterface
                     'admin_display' => 1,
                     'home_display' => 1,
                     'disable_del' => 1,
-                ]
+                ],
+                [
+                    'table_id' => $id,
+                    'field_name' => 'updated_at',
+                    'title' => '更新时间',
+                    'field_type' => '1',
+                    'length' => 255,
+                    'sort' => 999,
+                    'is_check' => 1,
+                    'admin_display' => 1,
+                    'home_display' => 0,
+                    'disable_del' => 1,
+                ],
+                [
+                    'table_id' => $id,
+                    'field_name' => 'created_at',
+                    'title' => '创建时间',
+                    'field_type' => '1',
+                    'length' => 255,
+                    'sort' => 1000,
+                    'is_check' => 1,
+                    'admin_display' => 1,
+                    'home_display' => 0,
+                    'disable_del' => 1,
+                ],
                 
             ]);
  
@@ -183,6 +213,7 @@ class FormService implements FormServiceInterface
         $fields = GlobalTableField::from('global_table_field as a')
             ->join('global_table_field_type as b', 'a.field_type', '=', 'b.id')
             ->where('a.table_id', $data['id'])
+            ->whereNotIn('a.field_name',['id','created_at','updated_at'])
             ->orderBy('a.sort', 'asc')
             ->select(
             // global_table_field 所有字段
@@ -295,6 +326,21 @@ class FormService implements FormServiceInterface
         try {
             //查询global库的表GlobalTableField模型中table_id为$data['id']的数据条件为admin_display=1的数据
             $globalTableFields = GlobalTableField::where(['table_id'=>$data['id'],'admin_display'=>1])->get();
+            $globalTableFields->transform(function ($field) {
+                $optionValue = [];
+                $optionStr = trim((string) $field->option);
+                if (!empty($optionStr)) {
+                    $options = explode("\n", $optionStr);
+                    foreach ($options as $option) {
+                        $parts = explode('|', $option);
+                        if (count($parts) === 2) {
+                            $optionValue[$parts[1]] = $parts[0];
+                        }
+                    }
+                }
+                $field->option_value = $optionValue;
+                return $field;
+            });
             
             //查询global库的表GlobalTable模型中id为$data['id']的数据
             $globalTable = GlobalTable::where('id',$data['id'])->first(); 
@@ -415,6 +461,21 @@ class FormService implements FormServiceInterface
             $query->where('id',$data['id']);
             $dataInfo = $query->first();
             $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id']])->get();
+            $globalTableField->transform(function ($field) {
+                $optionValue = [];
+                $optionStr = trim((string) $field->option);
+                if (!empty($optionStr)) {
+                    $options = explode("\n", $optionStr);
+                    foreach ($options as $option) {
+                        $parts = explode('|', $option);
+                        if (count($parts) === 2) {
+                            $optionValue[$parts[1]] = $parts[0];
+                        }
+                    }
+                }
+                $field->option_value = $optionValue;
+                return $field;
+            });
             $return = [
                 'data'=>$dataInfo,
                 'tableFields'=>$globalTableField->toArray(),
@@ -424,4 +485,88 @@ class FormService implements FormServiceInterface
             return Result::error('查询失败:' . $e->getMessage());
         }
     }
+    /**
+     * 查询网站下表单自定义字段
+     */
+    public function getWebGlobalTableFieldList(array $data): array
+    {
+        try {
+            $globalTable = GlobalTable::where('id',$data['table_id'])->first();
+            $fields = GlobalTableField::from('global_table_field as a')
+                ->join('global_table_field_type as b', 'a.field_type', '=', 'b.id')
+                ->where('a.table_id', $data['table_id'])
+                ->where('a.field_name',"<>",'id')
+                ->where('a.home_display', 1)
+                ->orderBy('a.sort', 'asc')
+                ->select(
+                // global_table_field 所有字段
+                    'a.*',
+                    // global_table_field_type 字段
+                    'b.type_name',
+                    'b.type_name_alias',
+                    'b.field_type as type_definition'
+                )
+                ->get();
+      
+            $config = new \EasySwoole\VerifyCode\Config();
+            $code = new \EasySwoole\VerifyCode\VerifyCode($config);
+            $img_code = '';
+            $characters = '0123456789';
+            $charLength = strlen($characters);
+            for ($i = 0; $i < 4; $i++) {
+                $img_code .= $characters[rand(0, $charLength - 1)];
+            }
+            //重写验证码
+            $result = $code->DrawCode((string)$img_code);
+            $img_code = $result->getImageCode();
+            var_dump("验证码:",$img_code);
+            $code_uniqid = uniqid("code");
+            //写入缓存 用于其他方法验证 并且设置过期时间
+            $this->redis->set($code_uniqid,$img_code,60000);
+            $rep = [
+                'fields' => $fields,
+                'table'=>$globalTable,
+            ];
+            if($globalTable->is_code){
+                $rep['code']['code_uniqid'] = $code_uniqid;
+                $rep['code']['img'] = $result->getImageBase64();
+            }
+            return Result::success($rep);
+        }catch (\Throwable $e){
+            return Result::error('查询失败:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * web端创建数据
+     * @param array $data
+     * @return array
+     */
+    public function addWebGlobalTableData(array $data): array
+    {
+        try {
+            $globalTable = GlobalTable::where('id',$data['otherData']['table_id'])->first();
+            if($globalTable->is_code){
+                if(empty($data['otherData']['code'])){
+                    return Result::error('请输入验证码');
+                }
+                $code = $this->redis->get($data['otherData']['code_uniqid']);   
+                if(empty($code)){
+                    return Result::error('验证码已过期');
+                }
+                if($data['otherData']['code'] != $code){
+                    return Result::error('验证码错误');
+                }
+            }
+            if(empty($globalTable)){
+                return Result::error('表单不存在');
+            }
+            $query = Db::connection('global')->table($globalTable->table);
+      
+            $query->insert($data['data']);
+            return Result::success([]);
+        }catch (\Throwable $e){
+            return Result::error('添加失败:' . $e->getMessage());
+        }
+    }
 }

+ 2 - 0
app/JsonRpc/FormServiceInterface.php

@@ -80,6 +80,8 @@ interface FormServiceInterface
      * @return array
      */
     public function updateGlobalTableData(array $data):array;
+    public function getWebGlobalTableFieldList(array $data):array;
+    public function addWebGlobalTableData(array $data):array;
 
 
 

+ 1 - 0
composer.json

@@ -14,6 +14,7 @@
     "require": {
         "php": ">=8.1",
         "doctrine/annotations": "^2.0",
+        "easyswoole/verifycode": "^3.1",
         "hyperf/amqp": "^3.1",
         "hyperf/async-queue": "^3.1",
         "hyperf/cache": "~3.1.0",

+ 115 - 3
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "cccded5c20a475a6d67e9cd987ccd087",
+    "content-hash": "328ea22b284a6e78776b0e3c42d9a4fe",
     "packages": [
         {
             "name": "carbonphp/carbon-doctrine-types",
@@ -389,6 +389,118 @@
             ],
             "time": "2024-02-05T11:56:58+00:00"
         },
+        {
+            "name": "easyswoole/spl",
+            "version": "2.1.3",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/easy-swoole/spl.git",
+                "reference": "6ca7321e476a40a3b70b15b836830ff030eec516"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/easy-swoole/spl/zipball/6ca7321e476a40a3b70b15b836830ff030eec516",
+                "reference": "6ca7321e476a40a3b70b15b836830ff030eec516",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "ext-dom": "*",
+                "ext-json": "*",
+                "ext-simplexml": "*",
+                "php": ">=8.1.0"
+            },
+            "require-dev": {
+                "easyswoole/phpunit": "^1.0",
+                "easyswoole/swoole-ide-helper": "^1.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "EasySwoole\\Spl\\": "src/",
+                    "EasySwoole\\Spl\\Test\\": "test/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "Apache-2.0"
+            ],
+            "authors": [
+                {
+                    "name": "YF",
+                    "email": "291323003@qq.com"
+                }
+            ],
+            "description": "php stander lib",
+            "homepage": "https://www.easyswoole.com/",
+            "keywords": [
+                "async",
+                "easyswoole",
+                "framework",
+                "swoole"
+            ],
+            "support": {
+                "issues": "https://github.com/easy-swoole/spl/issues",
+                "source": "https://github.com/easy-swoole/spl/tree/2.1.3"
+            },
+            "time": "2024-07-09T14:44:25+00:00"
+        },
+        {
+            "name": "easyswoole/verifycode",
+            "version": "3.1.2",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/easy-swoole/verify-code.git",
+                "reference": "cfd7c1a7218e8b5f2319d9b3b73cf7a588c22ee6"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/easy-swoole/verify-code/zipball/cfd7c1a7218e8b5f2319d9b3b73cf7a588c22ee6",
+                "reference": "cfd7c1a7218e8b5f2319d9b3b73cf7a588c22ee6",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "easyswoole/spl": "^2.0",
+                "ext-gd": "*",
+                "php": ">=8.1"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "EasySwoole\\VerifyCode\\": "src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "Apache-2.0"
+            ],
+            "authors": [
+                {
+                    "name": "YF",
+                    "email": "291323003@qq.com"
+                },
+                {
+                    "name": "evalor",
+                    "email": "mipone@foxmail.com"
+                }
+            ],
+            "support": {
+                "issues": "https://github.com/easy-swoole/verify-code/issues",
+                "source": "https://github.com/easy-swoole/verify-code/tree/3.1.2"
+            },
+            "time": "2023-09-06T06:45:56+00:00"
+        },
         {
             "name": "fig/http-message-util",
             "version": "1.1.5",
@@ -11225,12 +11337,12 @@
     ],
     "aliases": [],
     "minimum-stability": "dev",
-    "stability-flags": [],
+    "stability-flags": {},
     "prefer-stable": true,
     "prefer-lowest": false,
     "platform": {
         "php": ">=8.1"
     },
-    "platform-dev": [],
+    "platform-dev": {},
     "plugin-api-version": "2.6.0"
 }