cms first commit
This commit is contained in:
113
catch/cms/support/AuxiliaryTable.php
Normal file
113
catch/cms/support/AuxiliaryTable.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace catchAdmin\cms\support;
|
||||
|
||||
use catchAdmin\cms\model\ModelAuxiliaryTable;
|
||||
use catchAdmin\cms\model\Models;
|
||||
use catcher\exceptions\FailedException;
|
||||
|
||||
class AuxiliaryTable
|
||||
{
|
||||
protected $suffixes = [
|
||||
'first', 'second', 'third', 'fourth', 'fifth',
|
||||
'sixth', 'seventh', 'eighth', 'ninth', 'tenth'
|
||||
];
|
||||
|
||||
protected $mainTableId = 'main_id';
|
||||
|
||||
protected $auxiliaryTableName = null;
|
||||
|
||||
/**
|
||||
* 创建副表
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param int $modelId
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return string
|
||||
*/
|
||||
public function create(int $modelId): string
|
||||
{
|
||||
try {
|
||||
// 查询主表名称
|
||||
$tableName = Models::where('id', $modelId)->value('table_name');
|
||||
// 查询副表
|
||||
$auxiliaryTables = ModelAuxiliaryTable::where('model_id', $modelId)->select();
|
||||
// 目前最多允许创建 10 个副表
|
||||
if ($auxiliaryTables->count() > count($this->suffixes)) {
|
||||
throw new FailedException('最多只允许创建 ' . count($this->suffixes) . ' 个副表');
|
||||
}
|
||||
$defaultUsed = ModelAuxiliaryTable::NOT_USE;
|
||||
// 如果模型还没有关联的副表
|
||||
// 默认创建副表
|
||||
if (!$auxiliaryTables->count()) {
|
||||
$defaultUsed = ModelAuxiliaryTable::USED;
|
||||
|
||||
$this->auxiliaryTableName = $this->getName($tableName, array_shift($this->suffixes));
|
||||
} else {
|
||||
$existSuffixes = [];
|
||||
|
||||
$auxiliaryTables->each(function ($table) use (&$existSuffixes) {
|
||||
$name = explode('_', $table->name);
|
||||
|
||||
$existSuffixes[] = array_pop($name);
|
||||
});
|
||||
|
||||
$notUsed = array_diff($this->suffixes, $existSuffixes);
|
||||
|
||||
$this->auxiliaryTableName = $this->getName($tableName, array_shift($notUsed));
|
||||
}
|
||||
|
||||
if (Table::create($this->auxiliaryTableName)) {
|
||||
Table::addColumn($this->auxiliaryTableName, (new TableColumn([
|
||||
'type' => 'int',
|
||||
'name' => $this->mainTableId,
|
||||
'length' => 10,
|
||||
'title' => '主表数据的ID',
|
||||
'default_value' => 0,
|
||||
]))->get());
|
||||
}
|
||||
|
||||
app(ModelAuxiliaryTable::class)->storeBy([
|
||||
'model_id' => $modelId,
|
||||
'table_name' => $this->auxiliaryTableName,
|
||||
'used' => $defaultUsed,
|
||||
]);
|
||||
|
||||
return $this->auxiliaryTableName;
|
||||
} catch (\Exception $exception) {
|
||||
throw new FailedException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认使用的副表
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param int $modelId
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUsedAuxiliaryTable(int $modelId)
|
||||
{
|
||||
$auxiliaryTable = app(ModelAuxiliaryTable::class)->getUsed($modelId);
|
||||
|
||||
return $auxiliaryTable ? $auxiliaryTable->table_name : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取副表名称
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $mainTable
|
||||
* @param string $suffix
|
||||
* @return string
|
||||
*/
|
||||
protected function getName(string $mainTable, string $suffix): string
|
||||
{
|
||||
return $mainTable . '_relate_' . $suffix;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
384
catch/cms/support/DynamicFormFields.php
Normal file
384
catch/cms/support/DynamicFormFields.php
Normal file
@@ -0,0 +1,384 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | Catch-CMS Design On 2020
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace catchAdmin\cms\support;
|
||||
|
||||
use catchAdmin\cms\model\Models;
|
||||
use catcher\library\form\Form;
|
||||
use catchAdmin\cms\model\ModelFields;
|
||||
|
||||
class DynamicFormFields
|
||||
{
|
||||
protected $defaultRules = [
|
||||
'alpha' => ['^[A-Za-z]+$', '必须为纯字母'],
|
||||
'alphaNum' => ['^[A-Za-z0-9]+$', '必须为字母和数字'],
|
||||
'alphaDash' => ['^[A-Za-z0-9\-\_]+$', '必须为字母和数字,下划线_及破折号-'],
|
||||
'mobile' => ['^1[3-9]\d{9}$','请输入正确的手机号格式'],
|
||||
'idCard' => ['(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)','身份证输入格式不正确'],
|
||||
'zip' => ['\d{6}','请输入有效的邮政编码'],
|
||||
'ip' => ['((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))', '请输入正确的 IP 地址'],
|
||||
'password' => ['^[a-zA-Z]\w{5,17}$', '以字母开头,长度在6~18之间,只能包含字母、数字和下划线'],
|
||||
'strong_password' => ['^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$', '必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间'],
|
||||
'landLine' => ['\d{3}-\d{8}|\d{4}-\d{7}', '请输入正确的座机格式'],
|
||||
'chinese_character' => ['^[\u4e00-\u9fa5]{0,}$', '必须为纯汉字']
|
||||
];
|
||||
|
||||
/**
|
||||
* build form field
|
||||
*
|
||||
* @time 2021年03月10日
|
||||
* @param $modelId
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array
|
||||
*/
|
||||
public function build($tableName): array
|
||||
{
|
||||
$fields = [];
|
||||
|
||||
ModelFields::whereIn('model_id', Models::where('table_name', $tableName)->column('id'))
|
||||
->where('status', ModelFields::ENABLE)
|
||||
->select()
|
||||
->each(function ($field) use (&$fields){
|
||||
$formField = $this->{$field['type']}($field);
|
||||
|
||||
$formField = $this->getOptions($formField, $field['options'] ?? '');
|
||||
|
||||
$formField = $this->appendValidates($formField, $field['rules']);
|
||||
|
||||
$formField = $this->pattern($formField, $field['pattern']);
|
||||
|
||||
$fields[] = $formField;
|
||||
});
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Input
|
||||
*/
|
||||
public function string($field): \FormBuilder\UI\Elm\Components\Input
|
||||
{
|
||||
return Form::input($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 整型
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\InputNumber
|
||||
*/
|
||||
public function int($field): \FormBuilder\UI\Elm\Components\InputNumber
|
||||
{
|
||||
return Form::number($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 浮点
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\InputNumber
|
||||
*/
|
||||
public function float($field): \FormBuilder\UI\Elm\Components\InputNumber
|
||||
{
|
||||
return Form::number($field['name'], $field['tittle']);
|
||||
}
|
||||
|
||||
/**
|
||||
* textarea
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Input
|
||||
*/
|
||||
public function textarea($field): \FormBuilder\UI\Elm\Components\Input
|
||||
{
|
||||
return Form::textarea($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑器
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return mixed
|
||||
*/
|
||||
public function text($field)
|
||||
{
|
||||
return Form::editor($field['name'], $field['title'])->language();
|
||||
}
|
||||
|
||||
/**
|
||||
* longtext
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return mixed
|
||||
*/
|
||||
public function longtext($field)
|
||||
{
|
||||
return Form::editor($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期类型
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\DatePicker
|
||||
*/
|
||||
public function date($field): \FormBuilder\UI\Elm\Components\DatePicker
|
||||
{
|
||||
return Form::date($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期时间
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\DatePicker
|
||||
*/
|
||||
public function datetime($field): \FormBuilder\UI\Elm\Components\DatePicker
|
||||
{
|
||||
return Form::dateTime($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Upload
|
||||
*/
|
||||
public function image($field): \FormBuilder\UI\Elm\Components\Upload
|
||||
{
|
||||
return Form::image($field['title'], $field['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 多图
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Upload
|
||||
*/
|
||||
public function images($field): \FormBuilder\UI\Elm\Components\Upload
|
||||
{
|
||||
return Form::images($field['title'], $field['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return mixed
|
||||
*/
|
||||
public function file($field)
|
||||
{
|
||||
return Form::file($field['title'], $field['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传多个文件
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return mixed
|
||||
*/
|
||||
public function files($field)
|
||||
{
|
||||
return Form::files($field['title'], $field['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下拉框
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Select
|
||||
*/
|
||||
public function select($field): \FormBuilder\UI\Elm\Components\Select
|
||||
{
|
||||
return Form::select($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* checkbox
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Select
|
||||
*/
|
||||
public function checkbox($field): \FormBuilder\UI\Elm\Components\Select
|
||||
{
|
||||
return Form::select($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* radio
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Select
|
||||
*/
|
||||
public function radio($field): \FormBuilder\UI\Elm\Components\Select
|
||||
{
|
||||
return Form::select($field['name'], $field['title'])->options($this->getOptions($field['options']));
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Input
|
||||
*/
|
||||
public function password($field): \FormBuilder\UI\Elm\Components\Input
|
||||
{
|
||||
return Form::password($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 颜色
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\ColorPicker
|
||||
*/
|
||||
public function color($field): \FormBuilder\UI\Elm\Components\ColorPicker
|
||||
{
|
||||
return Form::color($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 省市选择
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Cascader
|
||||
*/
|
||||
public function city($field): \FormBuilder\UI\Elm\Components\Cascader
|
||||
{
|
||||
return Form::city($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 省市区选择
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $field
|
||||
* @return \FormBuilder\UI\Elm\Components\Cascader
|
||||
*/
|
||||
public function area($field): \FormBuilder\UI\Elm\Components\Cascader
|
||||
{
|
||||
return Form::cityArea($field['name'], $field['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* options
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $formField
|
||||
* @param $options
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getOptions($formField, $options)
|
||||
{
|
||||
if (!$options) {
|
||||
return $formField;
|
||||
}
|
||||
|
||||
return $formField->options(Helper::getOptions($options));
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证规则
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $formField
|
||||
* @param $validates
|
||||
* @return mixed
|
||||
*/
|
||||
protected function appendValidates($formField, $validates)
|
||||
{
|
||||
if (count($validates)) {
|
||||
foreach ($validates as $validate) {
|
||||
if ($validate === 'require') {
|
||||
$formField = $formField->required();
|
||||
}
|
||||
|
||||
switch ($validate) {
|
||||
case 'number':
|
||||
$formField->appendValidate(Form::validateNum()->message('请输入数字'));
|
||||
break;
|
||||
case 'integer':
|
||||
$formField->appendValidate(Form::validateInt()->message('请输入整型数字'));
|
||||
break;
|
||||
case 'float':
|
||||
$formField->appendValidate(Form::validateFloat()->message('请输入浮点型数字'));
|
||||
break;
|
||||
case in_array($validate, ['email', 'url', 'date']):
|
||||
$message = [
|
||||
'email' => '邮箱格式不正确',
|
||||
'url' => 'url 地址格式不正确',
|
||||
'date' => '日期格式不正确'
|
||||
];
|
||||
$method = 'validate' . ucfirst($validate);
|
||||
$formField->appendValidate(Form::{$method}()->message($message[$validate]));
|
||||
break;
|
||||
default:
|
||||
if (isset($this->defaultRules[$validate])) {
|
||||
list($pattern, $message) = $this->defaultRules[$validate];
|
||||
|
||||
$formField->appendValidate(
|
||||
Form::validateStr()->pattern($pattern)->message($message)
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $formField;
|
||||
}
|
||||
|
||||
/**
|
||||
* 正则
|
||||
*
|
||||
* @time 2021年03月10日
|
||||
* @param $formField
|
||||
* @param $pattern
|
||||
* @return mixed
|
||||
*/
|
||||
protected function pattern($formField, $pattern)
|
||||
{
|
||||
if ($pattern) {
|
||||
list($pattern, $message) = explode('|', $pattern);
|
||||
|
||||
return $formField->appendValidate(
|
||||
Form::validateStr()->pattern($pattern)->message($message)
|
||||
);
|
||||
}
|
||||
|
||||
return $formField;
|
||||
}
|
||||
}
|
68
catch/cms/support/Helper.php
Normal file
68
catch/cms/support/Helper.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | Catch-CMS Design On 2020
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace catchAdmin\cms\support;
|
||||
|
||||
use catcher\Utils;
|
||||
|
||||
class Helper
|
||||
{
|
||||
/**
|
||||
* 获取数组格式 options
|
||||
*
|
||||
* @time 2021年03月09日
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getOptions(string $value)
|
||||
{
|
||||
$options = [];
|
||||
|
||||
if (!$value) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
foreach (Utils::stringToArrayBy($value, PHP_EOL) as $option) {
|
||||
if ($option) {
|
||||
$option = explode('|', $option);
|
||||
|
||||
$options[] = [
|
||||
'value' => $option[0],
|
||||
'label' => $option[1],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理表单数组字符
|
||||
*
|
||||
* "[1, 2, 3, 4, 5]"
|
||||
*
|
||||
* @time 2021年03月07日
|
||||
* @param $arrayString
|
||||
* @return array|string[]
|
||||
*/
|
||||
public static function dealWithFormArrayString($arrayString): array
|
||||
{
|
||||
$array = trim(trim($arrayString, '['), ']');
|
||||
|
||||
if (!$array) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Utils::stringToArrayBy($array);
|
||||
}
|
||||
}
|
52
catch/cms/support/Table.php
Normal file
52
catch/cms/support/Table.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | Catch-CMS Design On 2020
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace catchAdmin\cms\support;
|
||||
|
||||
use catcher\generate\support\Table as _Table;
|
||||
|
||||
/**
|
||||
* @method static create(string $primaryKey, string $engine, string $comment)
|
||||
* @method static exist($tableName)
|
||||
* @method static drop($tableName)
|
||||
* @method static addColumn($tableName, $column)
|
||||
* @method static hasColumn($tableName, string $column)
|
||||
* @method static columns($tableName)
|
||||
* @method static dropColumn($tableName, string $column)
|
||||
* @method static addUniqueIndex($tableName, string $column)
|
||||
* @method static addIndex($tableName, string $column)
|
||||
* @method static addFulltextIndex($tableName, string $column)
|
||||
* @method static dropIndex($tableName, string $column)
|
||||
* @method static isIndex($tableName, string $column)
|
||||
*
|
||||
* @time 2021年04月30日
|
||||
*/
|
||||
class Table
|
||||
{
|
||||
/**
|
||||
* 静态访问
|
||||
*
|
||||
* @time 2021年04月30日
|
||||
* @param $method
|
||||
* @param $params
|
||||
* @return false|mixed
|
||||
*/
|
||||
public static function __callStatic($method, $params)
|
||||
{
|
||||
$table = new _Table($params[0]);
|
||||
|
||||
unset($params[0]);
|
||||
|
||||
return call_user_func_array([$table, $method], $params);
|
||||
}
|
||||
}
|
321
catch/cms/support/TableColumn.php
Normal file
321
catch/cms/support/TableColumn.php
Normal file
@@ -0,0 +1,321 @@
|
||||
<?php
|
||||
namespace catchAdmin\cms\support;
|
||||
|
||||
use catchAdmin\cms\model\ModelFields;
|
||||
use catchAdmin\permissions\model\search\DepartmentSearch;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class TableColumn
|
||||
{
|
||||
/**
|
||||
* ['value' => 'string', 'label' => '字符串'],
|
||||
['value' => 'int', 'label' => '整数'],
|
||||
['value' => 'float', 'label' => '小数'],
|
||||
['value' => 'textarea', 'label' => 'textarea文本'],
|
||||
['value' => 'text', 'label' => '编辑器(建议)'],
|
||||
['value' => 'longtext', 'label' => '编辑器(支持超大文本)'],
|
||||
['value' => 'date', 'label' => '日期型'],
|
||||
['value' => 'datetime', 'label' => '日期时间型'],
|
||||
['value' => 'image', 'label' => '图片上传'],
|
||||
['value' => 'images', 'label' => '多图上传'],
|
||||
['value' => 'file', 'label' => '文件上传'],
|
||||
['value' => 'files', 'label' => '多文件上传'],
|
||||
['value' => 'select', 'label' => '列表'],
|
||||
['value' => 'checkbox', 'label' => '复选框'],
|
||||
['value' => 'password', 'label' => '密码框'],
|
||||
['value' => 'color', 'label' => '颜色选项'],
|
||||
['value' => 'radio', 'label' => '单选'],
|
||||
['value' => 'city', 'label' => '省市二级级联动'],
|
||||
['value' => 'area', 'label' => '省市区三级联动'],
|
||||
*/
|
||||
|
||||
protected $column;
|
||||
|
||||
/**
|
||||
* TableColumn constructor.
|
||||
* @param array $field
|
||||
*/
|
||||
public function __construct(array $field)
|
||||
{
|
||||
/* $column \think\migration\db\Column */
|
||||
$length = $field['length'] ?? 0;
|
||||
|
||||
$column = $this->{$field['type']}($field['name'], (int)$length);
|
||||
|
||||
if ($field['default_value']) {
|
||||
$column->setDefault($field['default_value'] ?: '');
|
||||
}
|
||||
|
||||
$column->setComment($field['title'] ? : '');
|
||||
|
||||
if (isset($field['is_unique']) && $field['is_unique'] == ModelFields::IS_UNIQUE) {
|
||||
$column->setUnique();
|
||||
}
|
||||
|
||||
if (isset($field['is_index']) && $field['is_index'] == ModelFields::IS_INDEX) {
|
||||
$column->be_index = true;
|
||||
}
|
||||
|
||||
$this->column = $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取结果
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @return mixed
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
/**
|
||||
* string 类型
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function string(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* int 类型
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function int(string $name, int $length): Column
|
||||
{
|
||||
return Column::integer($name)->setLimit($length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 浮点数
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function float(string $name, int $length): Column
|
||||
{
|
||||
return Column::float($name)->setLimit($length);
|
||||
}
|
||||
|
||||
/**
|
||||
* varchar 类型
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function textarea(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(2000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通文本
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function text(string $name, int $length): Column
|
||||
{
|
||||
return Column::text($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 超大文本
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function longtext(string $name, int $length): Column
|
||||
{
|
||||
return Column::longText($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间类型
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function date(string $name, int $length): Column
|
||||
{
|
||||
return Column::date($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期时间
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function datetime(string $name, int $length): Column
|
||||
{
|
||||
return Column::dateTime($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片存储
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function image(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(255);
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片集合存储
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function images(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件存储
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function file(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(255);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件集合
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function files(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表类型
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function select(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(20);
|
||||
}
|
||||
|
||||
/**
|
||||
* checkbox 类型
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function checkbox(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(20);
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function password(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(255);
|
||||
}
|
||||
|
||||
/**
|
||||
* 颜色
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function color(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(50);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单选
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function radio(string $name, int $length): Column
|
||||
{
|
||||
return Column::string($name)->setLimit(20);
|
||||
}
|
||||
|
||||
/**
|
||||
* 省市
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function city(string $name, int $length): Column
|
||||
{
|
||||
// province_name/city_name
|
||||
return Column::string($name)->setLimit(255);
|
||||
}
|
||||
|
||||
/**
|
||||
* 省市区
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param string $name
|
||||
* @param int $length
|
||||
* @return Column
|
||||
*/
|
||||
public function area(string $name, int $length): Column
|
||||
{
|
||||
// province_name/city_name/district_name
|
||||
return Column::string($name)->setLimit(255);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user