Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

李洋 / 消息系统

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • CMS-Message
  • app
  • Http
  • Controllers
  • TableConfig.php
TableConfig.php 26 KB
李洋's avatar
Initial commit
60b5dc25
 
李洋 committed 7 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Config;
use DB;
use Exception;
use Log;
use Validator;

define('TYPE_INCREMENT', 10);   // 自增量
define('TYPE_INT',       11);   // 整数
define('TYPE_UINT',      12);   // 无符号整数
define('TYPE_FLOAT',     13);   // 浮点数
define('TYPE_BOOL',      14);   // bool
define('TYPE_DATE',      20);   // 日期
define('TYPE_DATETIME',  21);   // 日期时间
define('TYPE_TIMESTAMP', 22);   // 时间戳
define('TYPE_MTIME',     23);   // 修改时间
define('TYPE_CTIME',     24);   // 创建时间
define('TYPE_STRING',    30);   // 字符串
define('TYPE_JSON',      31);   // Json
define('TYPE_TEXT',      32);   // 长文本
define('TYPE_USER',      40);   // 用户名
define('TYPE_MUSER',     41);   // 修改者
define('TYPE_CUSER',     42);   // 创建者
define('TYPE_URL',       43);   // URL
define('TYPE_IMAGE',     44);   // 图片地址; 允许上传
define('TYPE_EMAIL',     45);   // 邮箱
define('TYPE_IP',        46);   // IP地址
define('TYPE_COMPLATE',  50);   // 自动补全

class TableConfig
{
    // 转换数据库的set,enum类型的定义
    private function converEnums($v)
    {
        if (is_array($v)) {
            $str = "";
            foreach ($v as $item)
                $str .= ',' . $item;
            return substr($str, 1);
        } else {
            return $v;
        }
    }

    private function enumValues($enums)
    {
        preg_match_all("/'(([^']|'')+)',?/", substr($enums, 5, -1), $match);
        foreach ($match[1] as &$elm)
            $elm = str_replace("''", "'", $elm);

        return $match[1];
    }


    // 检查输入
    private function checkInputFormt($fielddef, $v, $raw, $limit=null)
    {
        if (($v === '' || $v === null) && !$fielddef->need)
            return true;

        switch ($fielddef->type) {
        case TYPE_INCREMENT:    // 自增变量
        case TYPE_INT:          // 整数
            if (filter_var($v, FILTER_VALIDATE_INT) === false)
                return "错误的的整数格式";
            return true;
        case TYPE_UINT:         // 无符号整数
            if (filter_var($v, FILTER_VALIDATE_INT) === false)
                return "错误的的整数格式";
            if ($v < 0)
                return "输入值是负数";
            break;
        case TYPE_FLOAT:        // 浮点数
            if (filter_var($v, FILTER_VALIDATE_FLOAT) === false)
                return "错误的的整数格式";
            break;
        case TYPE_DATE:         // 日期
            if (date('Y-m-d', strtotime($v)) != $v)
                return "错误的的日期格式";
            break;
        case TYPE_DATETIME:     // 日期时间
            if (date('Y-m-d H:i:s', strtotime($v)) != $v)
                return "错误的的日期格式";
            break;
        case TYPE_TIMESTAMP:    // 时间戳
            if (filter_var($v, FILTER_VALIDATE_INT) === false)
                return "错误的的时间戳格式";
            break;
        case TYPE_USER:
            if ($v != 'ALL' && filter_var($v, FILTER_VALIDATE_EMAIL) === false)
                return "错误的用户名";
            break;
        case TYPE_EMAIL:
            if (filter_var($v, FILTER_VALIDATE_EMAIL) === false)
                return "错误的邮箱";
            break;
        case TYPE_URL:
            if (filter_var($v, FILTER_VALIDATE_URL) === false)
                return "错误的邮箱";
            break;
        case TYPE_IMAGE:
            if (filter_var($v, FILTER_VALIDATE_URL) === false)
                return "错误的邮箱";
            break;
        case TYPE_IP:
            if (filter_var($v, FILTER_VALIDATE_IP) === false)
                return "错误的邮箱";
            break;
        }

        switch ($fielddef->type) {
        case TYPE_INT: case TYPE_UINT: case TYPE_FLOAT: case TYPE_TIMESTAMP: case TYPE_INCREMENT:
            if (isset($fielddef->max) && $v > $fielddef->max)
                return "超过了最大值" . $fielddef->max;
            if (isset($fielddef->min) && $v < $fielddef->min)
                return "小于最小值" . $fielddef->min;
            break;
        default:
            if (isset($fielddef->maxlen) && $fielddef->maxlen > 0 && strlen($v) > $fielddef->maxlen)
                return "超过了最大长度" . $fielddef->maxlen;
            if (isset($raw["maxlen"]) && strlen($v) > $raw["maxlen"])
                return "超过了系统存储大小" . $raw["maxlen"];
            if (isset($fielddef->minlen) && $fielddef->minlen > 0 && strlen($v) < $fielddef->minlen)
                return "小于最大长度" . $fielddef->minlen;
            break;
        }

        if ($raw["type"] == 'set' || $raw["type"] == 'enum') {
            if (array_search($v, $raw["enums"]) === false)
                return "字段取值只能为(" . $this->converEnums($raw["enums"]) . ")";
        }

        if ($limit) {
            $name = $fielddef->name;
            $validator = Validator::make([$name => $v], [$name => $limit]);
            if ($validator->fails())
                return json_encode($validator->errors()->all());
        }

        return true;
    }

    private function converType($elm)
    {
        $datatype = [
            'name'          => $elm->COLUMN_NAME,
            'default_val'   => $elm->COLUMN_DEFAULT,
            'extra'         => $elm->EXTRA,
            'comment'       => $elm->COLUMN_COMMENT,
            'key'           => $elm->COLUMN_KEY
        ];

        switch ($elm->DATA_TYPE) {
        case 'bit':
            $datatype['type'] = 'int';
            $bits = 1;
            if (preg_match('/bit\(([0-9]+)\)/', $elm->COLUMN_TYPE, $matchs) == 1)
                $bits = (int)$matchs[2];

            $datatype['min'] = 0;
            if ($bits < 64)
                $datatype['max'] = 1 << ($bits - 1);
            else
                $datatype['max'] = "18446744073709551615";
            break;
        case 'tinyint':
            $datatype['type'] = 'int';
            if (strstr($elm->COLUMN_TYPE, "unsigned")) {
                $datatype['min'] = 0;
                $datatype['max'] = 255;
            } else {
                $datatype['min'] = -128;
                $datatype['max'] = 127;
            }
            break;
        case 'smallint':
            $datatype['type'] = 'int';
            if (strstr($elm->COLUMN_TYPE, "unsigned")) {
                $datatype['min'] = 0;
                $datatype['max'] = 65535;
            } else {
                $datatype['min'] = -32768;
                $datatype['max'] = 32767;
            }
            break;
        case 'mediumint':
            $datatype['type'] = 'int';
            if (strstr($elm->COLUMN_TYPE, "unsigned")) {
                $datatype['min'] = 0;
                $datatype['max'] = 16777215;
            } else {
                $datatype['min'] = -8388608;
                $datatype['max'] = 8388607;
            }
            break;
        case 'int':
            $datatype['type'] = 'int';
            if (strstr($elm->COLUMN_TYPE, "unsigned")) {
                $datatype['min'] = 0;
                $datatype['max'] = 4294967295;
            } else {
                $datatype['min'] = -2147483648;
                $datatype['max'] = 2147483647;
            }
            break;
        case 'bigint':
            $datatype['type'] = 'int';
            if (strstr($elm->COLUMN_TYPE, "unsigned")) {
                $datatype['min'] = 0;
                $datatype['max'] = "18446744073709551615";
            } else {
                $datatype['min'] = "-9223372036854775808";
                $datatype['max'] = "9223372036854775807";
            }
            break;
        case 'float':
            $datatype['type'] = 'float';
            if (strstr($elm->COLUMN_TYPE, "unsigned")) {
                $datatype['min'] = 0;
                $datatype['max'] = 3.402823466E+38;
            } else {
                $datatype['min'] = -3.402823466E+38;
                $datatype['max'] = 3.402823466E+38;
            }
            break;
        case 'decimal':
        case 'double':
            $datatype['type'] = 'float';
            if (strstr($elm->COLUMN_TYPE, "unsigned")) {
                $datatype['min'] = 0;
                $datatype['max'] = 1.7976931348623157e308;
            } else {
                $datatype['min'] = -1.7976931348623157e308;
                $datatype['max'] = 1.7976931348623157e308;
            }
            break;
        case 'date':
            $datatype['type'] = 'date';
            break;
        case 'datetime':
            $datatype['type'] = 'datetime';
            break;
        case 'timestamp':
            $datatype['type'] = 'datetime';
            break;
        case 'year':
            $datatype['type'] = 'year';
            break;
        case 'time':
            $datatype['type'] = 'time';
            break;
        case 'varchar':
        case 'binary':
        case 'tinyblob':
        case 'tinytext':
        case 'blob':
        case 'text':
        case 'mediumblob':
        case 'mediumtext':
        case 'longblob':
        case 'longtext':
        default:
            $datatype['type'] = 'string';
            $datatype['maxlen'] = $elm->CHARACTER_MAXIMUM_LENGTH;
            break;
        case 'enum':
            $datatype['type'] = 'enum';
            $datatypes['enums'] = $this->enumValues($elm->COLUMN_TYPE);
            $datatype['maxlen'] = $elm->CHARACTER_MAXIMUM_LENGTH;
            break;
        case 'set':
            $datatype['type'] = 'set';
            $datatypes['enums'] = $this->enumValues($elm->COLUMN_TYPE);
            $datatype['maxlen'] = $elm->CHARACTER_MAXIMUM_LENGTH;
            break;
        };

        return $datatype;
    }

    public function schemaByTable($key, $dbname, $table)
    {
        $sql = "SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA=? AND TABLE_NAME=?";

        $fields = [];
        foreach (DB::connection($key)->select($sql, [$dbname, $table]) as $elm)
            $fields[$elm->COLUMN_NAME] = $this->converType($elm);

        if (count($fields) == 0) {
            $msg = "找不到数据表 " . $dbname . '.' . $table;
            throw new Exception($msg, 404);
        }

        return $fields;
    }

    // 获取表结构
    public function getSchema($host, $port, $user, $passwd, $dbname, $table)
    {
        $confname = $host . ':' . $port;

        $conn = [
            'driver'    => 'mysql',
            'host'      => $host,
            'port'      => $port,
            'database'  => 'information_schema',
            'username'  => $user,
            'password'  => $passwd,
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ];

        Config::set('database.connections.' . $confname, $conn);

        return $this->schemaByTable($confname, $dbname, $table);
    }

    public function CheckTableconfig(&$input, $key, $config, $table_id=null)
    {
        $err = [];
        $raw_fileds = null;

        if (!isset($input->host) || !isset($input->port) || !isset($input->username) ||
            !isset($input->passwd) || !isset($input->dbname) || !isset($input->tablename)) {
            $err->host = "数据库配置错误";
        } else {
            try {
                $ret = DB::connection($key)->table($config->dbname . '.' . $config->tablename)
                    ->where('host', $input->host)
                    ->where('port', $input->port)
                    ->where('username', $input->username)
                    ->where('passwd', $input->passwd)
                    ->where('dbname', $input->dbname)
                    ->where('tablename', $input->tablename)->get();

                if ($ret && count($ret) > 0) {
                    // if ($table_id === null || $ret[0]->table_id != $table_id)
                        //return ["errcode" => 400, "errmsg" => "同配置id为" . $ret[0]->table_id . "的数据库配置相同, 不要重复创建配置" ];
                }

                $raw_fileds = $this->getSchema($input->host, $input->port, $input->username,
                                               $input->passwd, $input->dbname, $input->tablename);
            } catch (Exception $e) {
            }
        }

        if (!$raw_fileds) {
            $err["host"] = "数据库配置错误";
            return ["errcode" => 400, "errmsg" => "配置错误", "error" => $err];
        }

        // 检查keyfield;
        foreach ($raw_fileds as $elm) {
            if ($elm["extra"] != 'auto_increment')
                continue;
            if (!isset($input->keyfield))
                break;
            if (!in_array($elm["name"], $input->keyfield))
                break;
            if (count($input->keyfield) > 1) {
                $err["keyfield"] = "自增字段不是唯一主键字段";
                break;
            }
        }

        $fnames = ["keyfield", "sortfield", "groupfield"];
        foreach ($fnames as $fname) {
            if (!isset($input->$fname))
                continue;

            foreach ($input->$fname as $field) {
                if (!isset($raw_fileds[$field]) || !isset($input->fields->$field)) {
                    $err[$field] = "包含不存在的字段名";
                    continue 2;
                }
            }
        }

        if (isset($input->keyfield) && count($input->keyfield) > 0) {
            foreach ($input->keyfield as $kfield) {
                if (isset($input->fields->$kfield) && !$input->fields->$kfield->need) {
                    $err["fields"] = "主键字段" . $kfield . "没有设置为必填";
                    break;
                }
            }
        }

        if (!isset($input->fields)) {
            $err["fields"] = "配置为空";
            return ["errcode" => 400, "errmsg" => "配置错误", "error" => $err];
        }

        foreach ($input->fields as $name => &$field) {
            if (!isset($field->name) || $name != $field->name) {
                $err["fields"] = "字段". $name . "配置错误";
                break;
            }

            if (!isset($field->type)) {
                $err["fields"] = "字段". $name . "类型配置错误";
                break;
            }

            if (!isset($raw_fileds[$name])) {
                $err["fields"] = "字段". $name . "不存在";
                break;
            }

            $raw = $raw_fileds[$name];
            if ($raw["extra"] == "auto_increment" && $field->type != TYPE_INCREMENT) {
                $err["fields"] = "字段". $name . "数据库类型为自增变量,配置类型也需要为自增变量";
                break;
            }

            // 检查字典
            if (isset($field->dict)) {
                foreach ($field->dict as $dict) {
                    if (!isset($dict->key)) {
                        $err["fileds"] = "字段". $name . "的字典配置错误";
                        break 2;
                    }
                }
            }

            $comint = true;
            if ($raw["type"] == 'enum' || $raw["type"] == 'set') {
                if (!isset($field->dict)) {
                    foreach ($raw["enums"] as $enu)
                        $field->dict[] = ["key"=> $enu, "name"=> $enu];
                } else {
                    foreach ($field->dict as $dict) {
                        if (!in_array($dict->key, $raw["enums"])) {
                            $err["fileds"] = "字段". $name . "的字典配置和数据库定义不匹配";
                            break 2;
                        }
                    }
                }

                foreach ($raw["enums"] as $enu) {
                    if (!preg_match('/^[0-9]\+$/', $enu)) {
                        $comint = false;
                        break;
                    }
                }
            }

            // 检查类型是否匹配
            if (isset($field->islist) && $field->islist && ($raw["type"] != 'string' && $raw["type"] != 'set')) {
                $err["fileds"] = "字段". $name . "错误的设置为列表";
                break;
            }

            switch ($field->type) {
            case TYPE_INCREMENT:    // 自增
            case TYPE_INT:          // 整数
            case TYPE_BOOL:         // bool
            case TYPE_UINT:         // 无符号整数
                if ($raw["type"] != 'string' && $raw["type"] != 'int' && !(($raw["type"] == 'enum' || $raw["type"] == 'set') && $comint))
                    $err["fileds"] = "字段". $name . "类型错误, 和数据库的字段类型不匹配";
                break;
            case TYPE_FLOAT:        // 浮点数
                if ($raw["type"] != 'string' && $raw["type"] != 'float')
                    $err["fileds"] = "字段". $name . "类型错误, 和数据库的字段类型不匹配";
                break;
            case TYPE_TIMESTAMP:    // 时间戳
                if ($raw["type"] != 'string' && $raw["type"] != 'int')
                    $err["fileds"] = "字段". $name . "类型错误, 和数据库的字段类型不匹配";
                break;
            case TYPE_DATE:         // 日期
                if ($raw["type"] != 'string' && $raw["type"] != 'datetime' && $raw["type"] != 'date')
                    $err["fileds"] = "字段". $name . "类型错误, 和数据库的字段类型不匹配";
                break;
            case TYPE_DATETIME:     // 日期时间
            case TYPE_MTIME:        // 修改时间
            case TYPE_CTIME:        // 创建时间
                if ($raw["type"] != 'string' && $raw["type"] != 'datetime')
                    $err["fileds"] = "字段". $name . "类型错误, 和数据库的字段类型不匹配";
                break;
            case TYPE_STRING:       // 字符串
                if ($raw["type"] != 'string' && $raw["type"] != 'enum' && $raw["type"] != 'set')
                    $err["fileds"] = "字段". $name . "类型错误, 和数据库的字段类型不匹配";
                break;
            case TYPE_JSON:         // Json
                $field->ishide = true;
            case TYPE_TEXT:         // 长文本
                $field->ishide = true;
            case TYPE_USER:         // 用户名
            case TYPE_MUSER:        // 修改者
            case TYPE_CUSER:        // 创建者
            case TYPE_COMPLATE:     // 自动补全
            case TYPE_URL:
            case TYPE_IMAGE:
            case TYPE_EMAIL:
            case TYPE_IP:
                if ($raw["type"] != 'string')
                    $err["fileds"] = "字段". $name . "类型错误, 和数据库的字段类型不匹配";
                break;
            default:
                $err["fileds"] = "字段". $name . "未知类型";
                break 2;
            };
        }

        if (count($err) > 0)
            return ["errcode" => 400, "errmsg" => "配置错误", "error" => $err];

        $adv = new AdvConfig();

        $err = $adv->checkAdvConfig($input, $raw_fileds);
        if ($err !== true)
            return ["errcode" => 400, "errmsg" => "高级配置有错误", "error" => $err];
        return true;
    }

    // 获取最大的id
    private function getMaxId($key, $config, $field)
    {
        return DB::connection($key)->table($config->dbname . '.' . $config->tablename)->max($field) + 1;
    }

    private function checkByJsonSchema($schema, $json)
    {
        $validator = new \JsonSchema\Validator();
        $validator->check($json, $schema);

        if (!$validator->isValid())
            return ['errcode' => 400, 'errmsg' => 'Bad Request: error data param', "errors" => $validator->getErrors()];
        return true;
    }

    // 检查输入参数
    public function checkData(Request $request, $input, $config, &$data, $raw_fileds, $create, $key)
    {
        $err = [];
        foreach ($config->fields as $fielddef) {
            $name = $fielddef->name;
            $limit = (isset($config->adv_config) && $config->adv_config && isset($config->adv_config->rules) &&
                isset($config->adv_config->rules->$name)) ? $config->adv_config->rules->$name : null;

            switch ($fielddef->type) {
            case TYPE_MTIME:
                $data[$name] = date("Y-m-d H:i:s");
                continue 2;
            case TYPE_CTIME: // 创建时间不允许修改
                if ($create)
                    $data[$name] = date("Y-m-d H:i:s");
                continue 2;
            case TYPE_MUSER:
                $data[$name] = $request->user->email;
                continue 2;
            case TYPE_CUSER: // 创建人不允许修改
                if ($create)
                    $data[$name] = $request->user->email;
                continue 2;
            case TYPE_INCREMENT: // 自增量
                if ($create) {
                    if ($raw_fileds[$name]["extra"] != 'auto_increment')
                        $data[$name] = $this->getMaxId($key, $config, $name);
                    continue 2;
                }
                break;
            }

            if (!isset($input->$name)) {
                if (isset($fielddef->need) && $fielddef->need)
                    $err[$name] = "必填";
                continue;
            }

            $v = $input->$name;
            if ($fielddef->islist) {
                if (!is_array($v)) {
                    $err[$name] = "数组格式错误";
                    continue;
                }

                if (isset($config->adv_config) && isset($config->adv_config->arraySizeLimit) &&
                    isset($config->adv_config->arraySizeLimit->$name))
                {
                    $count = $config->adv_config->arraySizeLimit->$name;
                    if (isset($count->max) && count($v) > $count->max)
                        $err[$name] = "数组个数超出了最大个数限制,最多为{$count->max}个元素";
                    if (isset($count->min) && count($v) < $count->min)
                        $err[$name] = "数组个数超出了最小个数限制,最小为{$count->min}个元素";
                }

                foreach ($v as $item) {
                    $r = $this->checkInputFormt($fielddef, $item, $raw_fileds[$name], $limit);
                    if ($r !== true) {
                        $err[$name] = $r;
                        continue 2;
                    }
                }

                if ($raw_fileds[$name]["type"] == 'set') {
                    $v = $this->converEnums($v);
                } else if ($raw_fileds[$name]["type"] == 'enum') {
                    return "和数据库存储类型不匹配";
                } else {
                    $v = json_encode($v);
                    if (strlen($v) > $raw_fileds[$name]["maxlen"]) {
                        $err[$name] = "超出了存储长度";
                        continue;
                    }
                }
            } else if ($fielddef->type == TYPE_JSON) {
                if (!is_array($v) && !is_object($v)) {
                    $err[$name] = "对象格式错误";
                    continue;
                }

                if (isset($config->adv_config) && isset($config->adv_config->jsonSchema) &&
                    isset($config->adv_config->jsonSchema->$name)) {
                    $e = $this->checkByJsonSchema($config->adv_config->jsonSchema->$name, $v);
                    if ($e !== true) {
                        $err[$name] = $e;
                        continue;
                    }
                }

                $v = json_encode($v);
                if (strlen($v) > $raw_fileds[$name]["maxlen"]) {
                    $err[$name] = "超出了存储长度";
                    continue;
                }
            } else {
                $r = $this->checkInputFormt($fielddef, $v, $raw_fileds[$name], $limit);
                if ($r !== true) {
                    $err[$name] = $r;
                    continue;
                }
            }

            $data[$name] = $v;
        }

        if ($config->table_id == 0)
            $data["last_author"] = $request->user->email;

        if (count($err) > 0)
            return ["errcode" => 400, "errmsg" => "参数错误", "error" => $err];
        return true;
    }

    public function dataDecodeItem(&$data, $names)
    {
        foreach ($names as $name) {
            if (isset($data->$name))
                $data->$name = json_decode($data->$name);
        }
    }

    public function dataDecode(&$datas, $fields)
    {
        $names = [];
        foreach ($fields as $field) {
            if ($field->type == TYPE_JSON || $field->islist)
                $names[] = $field->name;
        }

        if (!is_array($datas)) {
            $this->dataDecodeItem($datas, $names);
        } else {
            foreach ($datas as &$data)
                $this->dataDecodeItem($data, $names);
        }
    }

    static function checkHistoryDepends($schema)
    {
        $type = 0;
        foreach ($schema['fields'] as $f => $def) {
            if ($def->type == TYPE_IMAGE) {
                $type |= 1;
            } else if ($def->type == TYPE_JSON) {
                if (isset($schema['adv_config']) && isset($schema['adv_config']->jsonSchema) &&
                    isset($schema['adv_config']->jsonSchema->$f))
                {
                    $type |= 2;
                } else {
                    $type |= 4;
                }
            }
        }
        if ($schema['table_id'] == 1 || $schema['table_id'] == 0)
            $type |= 4;

        return $type;

    }

    static function checkScriptDepends($schema)
    {
        $type = 0;
        foreach ($schema->fields as $f => $def) {
            if ($def->type == TYPE_IMAGE) {
                $type |= 1;
            } else if ($def->type == TYPE_JSON) {
                if (isset($schema->adv_config) && isset($schema->adv_config->jsonSchema) &&
                    isset($schema->adv_config->jsonSchema->$f))
                {
                    $type |= 2;
                } else {
                    $type |= 4;
                }
            }
        }
        if ($schema->table_id == 1 || $schema->table_id == 0)
            $type |= 4;

        return $type;
    }

    static function tableScriptDepends()
    {
        return 4;
    }
};