📖 File Reader
<?php
namespace adminCreate\form;
class Form
{
//表单数据
protected $model = [];
//选项卡参数
protected $tabs = [];
//表单参数
protected $from = [];
//图片字段
protected $imgArr = [];
//编辑器字段
protected $editorArr = [];
//编辑器字段
protected $buttonArr = [];
//编辑器字段
protected $url = '';
//选项卡类型
protected $is_tabs = 0;
/**
* 选项卡
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
*/
public function tabs($field = '',$title ='',$value = [],$icon = ''){
$tabs['name'] = $field;
$tabs['title'] = $title;
$tabs['icon'] = $icon;
$tabs['value'] = $value;
if(empty($value)){
$this->is_tabs = 1;
}
$this->tabs[] = $tabs;
}
/**
* 文本框
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* type 前台方法名
* formType 前台显示样式
*/
public function input($field,$title,$value = '')
{
$create = new Create($field, $title,$value);
$create->type('add');
$create->type('text');
$create->formType('input');
$this->from[] = $create;
return $create;
}
/**
* 多选显示
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* type 前台方法名
* formType 前台显示样式
*/
function info($field,$title,$value = ['']){
$create = new Create($field, $title);
$create->type('add');
$create->formType('info');
$this->from[] = $create;
return $create;
}
/**
* 动态输入
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* type 前台方法名
* formType 前台显示样式
* tab_type 前台输入类型
*/
function text($field,$title,$value = ['']){
$create = new Create($field, $title,$value);
$create->type('add');
$create->tab_type('input');
$create->formType('input');
$this->from[] = $create;
return $create;
}
/**
* 数字输入框
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* type 前台方法名
* formType 前台显示样式
*/
public function number($field,$title,$value = '')
{
if (is_string($value) && $value != ''){$value = floatval($value);}
$create = new Create($field, $title,$value);
$create->type('int');
$create->formatter('');
$create->formType('inputNumber');
$this->from[] = $create;
return $create;
}
/**
* 单选框
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* type 前台方法名
* formType 前台显示样式
*/
public function radio($field, $title, $value = '')
{
if (is_int($value)){$value = strval($value);}
$create = new Create($field, $title,$value);
$create->formType('radio');
$create->type('large');
$this->from[] = $create;
return $create;
}
/**
* 复选框
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* type 前台方法名
* formType 前台显示样式
*/
public function checkbox($field, $title, $value = '')
{
//数据类型转换
if (is_array($value)){
foreach ($value as $k => $v){
if (is_int($v)){$value[$k] = strval($v);}
}
}
$create = new Create($field, $title,$value);
$create->type('large');
$create->formType('checkbox');
$this->from[] = $create;
return $create;
}
/**
* 开关
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* @param string $type 前台方法名
* @param string formType 前台显示样式
* open 选中显示文字
* close 未选中显示文字
*/
public function switch($field, $title, $value = ''){
$create = new Create($field, $title,$value);
$create->formType('switch');
$create->open('开');
$create->close('关');
$this->from[] = $create;
return $create;
}
/**
* 星星评分
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
*/
public function star($field, $title, $value=0){
$create = new Create($field, $title,$value);
$create->formType('star');
$create->count(5); //默认星星数量
$this->from[] = $create;
return $create;
}
/**
* 提示框-提示
* @param string $field 提示内容
* formType 前台显示样式
* size 字体大小
* color 文字颜色
*/
public function tips($field)
{
$create = new Create($field);
$create->size('14px');
$create->color('#333');
$create->formType('tips');
$this->from[] = $create;
return $create;
}
/**
* 提示框-警告
* @param string $title 标题
* @param string $describe 内容
* formType 前台显示样式
* size 字体大小
* color 文字颜色
*/
public function tips_warning($title='',$describe='')
{
$create = new Create('',$title,$describe);
$create->formType('tips_warning');
$this->from[] = $create;
return $create;
}
/*分行符*/
public function hr()
{
$create = new Create();
$create->formType('hr');
$this->from[] = $create;
return $create;
}
/**
* 图片上传
* @param string $field 字段名
* @param string $title 标题
* @param string $action 上传路径
* @param array $value value值
* @param array $fileType 上传类型
*/
public function upload($field, $title, $action = '', $value = [],$fileType = 'image')
{
$this->imgArr[] = $field;
$create = new Create($field, $title);
$create->action($action);
$arr = [];
foreach ($value as $v) {
if ($v) {
$leixing = substr(strrchr($v,'.'),1,10);
$file_type = 'image';
if (in_array($leixing, ['jpg', 'jpeg', 'png'])) {
$file_type = 'image';
}
if (in_array($leixing, ['mp4'])) {
$file_type = 'mp4';
}
if (in_array($leixing, ['mp3'])) {
$file_type = 'mp3';
}
if (in_array($leixing, ['xlsx','xls','vnd.ms-excel','csv','vnd.openxmlformats-officedocument.spreadsheetml.sheet'])) {
$file_type = 'excel';
}
if (in_array($leixing, ['zip','rar','gz'])) {
$file_type = 'zip';
}
if (in_array($leixing, ['pdf','PDF'])) {
$file_type = 'pdf';
}
if (in_array($leixing, ['docx','doc','rtf'])) {
$file_type = 'wps';
}
$arr[] = [
'title' =>$v,
'name' => $v,
'url' => $v,
'status' => 'finished',
'leixing'=> $file_type
];
}
}
$create->value($arr);
$create->formType('upload');
$create->file_name('file');
$create->maxSize(1024 * 1024 * 1024);
$create->type('drag');
$create->fileType($fileType);
$create->maxLength('1');
$create->is_show(true);
$create->nultiple('false');//多图传这个
if($create->props['fileType'] == 'image'){
$create->format(['jpg', 'jpeg', 'png']);
}else if($create->props['fileType'] == 'mp4'){
$create->format(['mp3', 'mp4']);
}else if($create->props['fileType'] == 'excel'){
$create->format(['xlsx','xls','vnd.ms-excel','csv','vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
}else if($create->props['fileType'] == 'zip'){
$create->format(['zip','rar','gz']);
}else if($create->props['fileType'] == 'pdf'){
$create->format(['pdf','PDF']);
}else if($create->props['fileType'] == 'wps'){
$create->format(['docx','doc','rtf']);
}else{
$create->format(['xlsx','xls','vnd.ms-excel','csv','vnd.openxmlformats-officedocument.spreadsheetml.sheet','zip','rar','gz','mp3', 'mp4','jpg', 'jpeg', 'png','doc','docx','pdf','PDF']);
}
$this->from[] = $create;
return $create;
}
/**
* 文件上传(列表)
* @param string $field 提示内容
* formType 前台显示样式
* size 字体大小
* color 文字颜色
*/
function upload_file($field, $title, $action = '', $value = [],$fileType = 'image')
{
$this->imgArr[] = $field;
$create = new Create($field, $title);
$create->action($action);
$arr = [];
foreach ($value as $v) {
if ($v) {
$leixing = substr(strrchr($v["url"],'.'),1,10);
$file_type = 'image';
if (in_array($leixing, ['jpg', 'jpeg', 'png'])) {
$file_type = 'image';
}
if (in_array($leixing, ['mp4'])) {
$file_type = 'mp4';
}
if (in_array($leixing, ['mp3'])) {
$file_type = 'mp3';
}
if (in_array($leixing, ['xlsx','xls','vnd.ms-excel','csv','vnd.openxmlformats-officedocument.spreadsheetml.sheet'])) {
$file_type = 'excel';
}
if (in_array($leixing, ['zip','rar','gz'])) {
$file_type = 'zip';
}
if (in_array($leixing, ['pdf'])) {
$file_type = 'pdf';
}
if (in_array($leixing, ['docx','doc','rtf'])) {
$file_type = 'wps';
}
$title = explode("/", $v["url"]);
$title = explode(".", $title[count($title) - 1]);
$arr[] = [
'title' => $title[0],
'name' => $v["name"],
'url' => $v["url"],
'status' => 'finished',
'leixing'=> $file_type,
'type' => 'file',
];
}
}
$create->value($arr);
$create->formType('upload_file');
$create->file_name('file');
$create->maxSize(1024 * 1024 * 1024);
$create->type('drag');
$create->fileType($fileType);
$create->maxLength('1');
$create->nultiple('false');//多图传这个
if($create->props['fileType'] == 'image'){
$create->format(['jpg', 'jpeg', 'png']);
}else if($create->props['fileType'] == 'mp4'){
$create->format(['mp3', 'mp4']);
}else{
$create->format(['xlsx','xls','vnd.ms-excel','csv','vnd.openxmlformats-officedocument.spreadsheetml.sheet','zip','rar','gz','mp3', 'mp4','jpg', 'jpeg', 'png','doc','docx','pdf']);
}
$this->from[] = $create;
return $create;
}
/**
* 日志列表
* @param string $title 标题
* @param array $value 日志内容
*/
public function list_log($title,$value=[]){
$create = new Create('',$title,$value);
$create->formType('list_log');
$create->value($value);
$this->from[] = $create;
return $create;
}
public function list_move($field, $title, $value = '')
{
$create = new Create($field, $title,$value);
$create->formType('list_move');
$create->open('开');
$create->close('关');
$this->from[] = $create;
return $create;
}
/**
* 文本编辑器
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* height 编辑器默认高度
* placeholder 编辑器无内容提示
*/
public function editor($field, $title, $value = '')
{
$this->editorArr[] = $field;
$create = new Create($field, $title,$value);
$create->height(400);
$create->formType('editor');
$create->placeholder("请填写详情");
$this->from[] = $create;
return $create;
}
/**
* 颜色
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
*/
public function color($field, $title, $value = '')
{
$create = new Create($field, $title,$value);
$create->col('24');
$create->formType('color');
$create->is_show(true);
$this->from[] = $create;
return $create;
}
/**
* 设置上传方法
* @param string $url 上传路径
* @param string $title 提示文字
* @param array $type 提交按钮央视
*/
public function button($url,$title = '提交',$type = 'primary'){
$this->buttonArr[] = ['url'=>$url,'title'=>$title,'type'=>$type];
}
/**
* 隐藏
* @param string $field 字段名
* @param array $value value值
*/
public function hidden($field = '',$value = ''){
$create = new Create($field,'',$value);
$create->formType('hidden');
$this->from[] = $create;
return $create;
}
/**
* 时间日期
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
* format 时间格式
*/
public function datetime($field, $title, $value = '')
{
$create = new Create($field, $title,$value);
$create->formType('date');
$create->type('datetimerange');
$create->format('yyyy-MM-dd HH:mm');
$this->from[] = $create;
return $create;
}
/**
* 多级联动
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
*/
public function cascader($field, $title,$value = []){
if (is_array($value)){
foreach ($value as $k => $v){
$value[$k] = intval($v);
}
}
$create = new Create($field,$title,$value);
$create->formType('cascader');
$create->clearable(true);
$create->trigger('click');
$create->changeonselect('true');
$create->filterable('true');
$create->loading('false');
$this->from[] = $create;
return $create;
}
/**
* 下拉框
* @param string $field 字段名
* @param string $title 标题
* @param array $value value值
*/
public function selectone($field, $title, $value = [])
{
if (!is_array($value)){$value = strval($value);}
$create = new Create($field, $title,$value);
$create->formType('select');
$create->col('24');
$create->clearable(true);
$create->filterable(true);
$create->loading(false);
$this->from[] = $create;
return $create;
}
/*复合型输入框*/
public function select_number($field,$title,$value = '')
{
$create = new Create($field, $title);
$create->formType('select_number');
$create->select($field.'type');
$this->from[] = $create;
return $create;
}
/**
* 动态列表
* @param string $field 字段名
* @param string $title 标题
* @param array $columns 列表头部名称
* @param array $value 列表值
* @param string $adds 列表新增行默认数据
* type 前台方法名
* formType 前台显示样式
*/
function Table($field,$title,$columns = [''],$value = [''],$adds = []){
$create = new Create($field, $title);
$create->type('add');
$create->is_type(0);
$create->col('24');
$create->size('');
$create->format('yyyy-MM-dd HH:mm');
$create->formType('info_list');
$create->placeholder('请输入' . $title);
$create->is_show(true);
$create->columns($columns);
$create->add($adds);
$create->value($value);
$create->datas($value);
$create->wei(count($value));
$create->icon('');
$this->from[] = $create;
return $create;
}
/**
* 动态列表-图片字段
* @param string $field 字段名
* @param string $title 标题
* @param string $url 上传地址
* @param string $disabled 禁止修改
* @param array $width 宽度
* @param array $align 显示位置
*/
function Tableimges($field,$title,$url,$disabled=false,$width = 80,$align = 'center'){
$create_info = [
'slot'=>'images',
'url'=>$url,
'width'=> $width,
'align'=>$align,
'title'=>$title,
'key'=>$field,
'disabled'=>$disabled
];
return $create_info;
}
/**
* 动态列表-文本字段
* @param string $field 字段名
* @param string $title 标题
* @param string $disabled 禁止修改
* @param array $width 宽度
* @param array $align 显示位置
*/
function Tabletext($field,$title,$disabled=false,$width = 130,$align = 'center'){
$create_info = [
'slot'=>'input',
'width'=> $width,
'align'=>$align,
'title'=>$title,
'key'=>$field,
'resizable'=>true,
'disabled'=>$disabled
];
return $create_info;
}
/**
* 动态列表-下拉单选字段
* @param string $field 字段名
* @param string $title 标题
* @param string $disabled 禁止修改
* @param array $width 宽度
* @param array $align 显示位置
*/
function Tableradio($field,$title,$disabled=false,$width = 130,$align = 'center'){
$create_info = [
'slot'=>'radio',
'width'=> $width,
'align'=>$align,
'title'=>$title,
'key'=>$field,
'disabled'=>$disabled
];
return $create_info;
}
/**
* 动态列表-操作
* @param string $field 字段名
* @param string $title 标题
* @param array $width 宽度
* @param array $align 显示位置
*/
function Tableaction($field,$title,$width = 130,$align = 'center'){
$create_info = [
'slot'=>'action',
'width'=> $width,
'align'=>$align,
'title'=>$title,
'key'=>$field
];
return $create_info;
}
public function __toString()
{
if(!empty($is_tabs)){
$this->from = [];
foreach ($this->tabs as $key => $val) {
unset($this->tabs[$key]['value']);
foreach ($val['value'] as $k => $v) {
list($rule, $v) = $v->build();
$v['tabs'] = $val['name'];
if (!empty($v['show'])) {
$v['is_show'] = false;
}
if (!empty($v['rules'])) {
$v['rulex'] = $v['rules'];
}
$this->from[] = $v;
if (isset($v['value'])) {
$this->model[$v['field']] = $v['value'];
}
if($v['formType'] == 'select_number'){
$this->model[$v['field']."type"] = $v['select_value'];
}
if($v['formType'] == 'tips_warning' || $v["formType"] == 'hr'){
unset($this->model[$v['field']]);
}
}
}
}else{
foreach ($this->from as $k => &$v) {
list($rule, $v) = $v->build();
if (!empty($v['show'])) {
$v['is_show'] = false;
}
if (!empty($v['rules'])) {
$v['rulex'] = $v['rules'];
}
if (isset($v['value'])) {
$this->model[$v['field']] = $v['value'];
}
if($v['formType'] == 'select_number'){
$this->model[$v['field']."type"] = $v['select_value'];
}
if($v['formType'] == 'tips_warning' || $v["formType"] == 'hr'){
unset($this->model[$v['field']]);
}
}
}
ob_start();
require_once dirname(__FILE__) . '/view/view.php';
$html = ob_get_clean();
return $html;
}
}
//用来生成
class FromBase{
function __construct($field='', $title = '',$value = '')
{
$this->field($field);
$this->title($title);
$this->value($value);
$this->placeholder('请输入' . $title);
$this->col(24);
$this->size('');
$this->calculate(false);
$this->is_show(true);
$this->icon('');
}
function __call($name ='', $arg = [])
{
$this->props[$name] = $arg[0];
return $this;
}
function required($msg = '',$type='')
{
if (empty($type)){
if($this->props['formType'] == 'cascader' || $this->props['formType'] == 'upload' || $this->props['formType'] == 'checkbox'){
$type = 'array';
}else if($this->props['formType'] == 'inputNumber' || $this->props['formType'] == 'select_number'){
$type='number';
}else if($this->props['formType'] == 'select'){
if (is_array($this->props["value"])){
$type = 'array';
}else{
$type = 'string';
}
}else{
$type='string';
}
}
$this->props['rules'] = [
'required' => 'true',
'message' => $msg ?: $this->props['placeholder'],
'trigger' => 'blur',
'type' => $type,
];
return $this;
}
function options($callback='', $label = 'label', $value = 'value')
{
$arr = [
'status' => [
['label' => '是', 'value' => '1'],
['label' => '否', 'value' => '0'],
],
];
if (is_string($callback) && isset($arr[$callback])) {
$this->props['options'] = $arr[$callback];
} elseif (is_callable($callback)) {
$this->props['options'] = $callback();
} elseif (is_object($callback)) {
$callback = $callback->toArray();
$this->options($callback, $label, $value);
} elseif (is_array($callback)) {
$arr = [];
foreach ($callback as $k => $v) {
if (is_string($v)) {
$arr[] = ['label' => $v, 'value' => $k];
} else {
if (is_int($v[$value])){ //整数转字符串
$v[$value] = strval($v[$value]);
}
$arr[] = ['label' => $v[$label], 'value' => $v[$value]];
}
}
$this->props['options'] = $arr;
}
return $this;
}
function build()
{
return [
$this->rule,
$this->props,
];
}
}
class Create extends FromBase{
//验证规则
protected $rule = [];
//重组选择数据
function setOptions($callback, $label = 'label', $value = 'value')
{
return $this->options($callback, $label, $value);
}
}