error("$modelName . DS . add.html已经存在"); exit; } } public function make($table, $path, $other) { $addTpl = dirname(dirname(__DIR__)) . '/tpl/add.tpl'; $tplContent = file_get_contents($addTpl); $model = $table; $filePath = empty($path) ? '' : DS . $path; $namespace = empty($path) ? '\\' : '\\' . $path . '\\'; $prefix = config('database.connections.mysql.prefix'); $column = Db::query('SHOW FULL COLUMNS FROM `' . $prefix . $table . '`'); $pk = ''; foreach ($column as $vo) { if ($vo['Key'] == 'PRI') { $pk = $vo['Field']; break; } } /* //读取数据结构生成字段 $tritems =''; $index =0; foreach ($column as $key => $vo) { $field = $vo['Field']; $title = $vo['Comment']==''?$field:$vo['Comment']; if($field != 'id'){ if(($index % 3) == 0){ $tritems.=" {$title} "; }else if(($index % 3) == 1){ $tritems.=" {$title} "; }else if(($index % 3) == 2){ $tritems.=" {$title} "; } $index++; } } if(($index % 3) == 1){ $tritems.=" "; } if(($index % 3) == 2){ $tritems.=" "; } */ //读取提交的数据生成字段 $field_column = get_cache('crud_a_'.$table); $tritems =''; $index =0; $summernoteIndex=0; $inputHtml=''; $textareaHtml=''; $uploadHtml=''; $uploadScript = ''; $summernoteHtml=''; $summernoteForm=''; $summernoteScript=''; $datetimeScript=''; foreach ($field_column as $key => $vo) { $field = $vo['field']; $title = $vo['title']; if($vo['type'] == 'summernote'){ $summernoteHtml.="".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])." "; $summernoteScript.= "//".$vo['title']."富文本编辑器 var edit = layui.tinymce.render({ selector: '#container_".$vo['field']."', height: 500 });"; $summernoteForm.= "data.field.".$vo['field']." = tinyMCE.editors['container_".$vo['field']."'].getContent(); if (data.field.".$vo['field']." == '') { layer.msg('请先完善".$vo['title']."内容'); return false; }"; $summernoteIndex++; } else if($vo['type'] == 'upload'){ $uploadHtml.="".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])." "; $uploadScript.= "//上传".$vo['title']." var upload_".$vo['field']." = layui.upload.render({ elem: '#upload_btn_".$vo['field']."', url: '/admin/api/upload', done: function (res) { //如果上传失败 if (res.code == 1) { return layer.msg('上传失败'); } //上传成功 $('#upload_box_".$vo['field']." input').attr('value', res.data.filepath); $('#upload_box_".$vo['field']." img').attr('src', res.data.filepath); } });"; }else if($vo['type'] == 'textarea'){ $textareaHtml.="".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])." "; } else{ if(($index % 3) == 0){ $inputHtml.="".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required']); }else if(($index % 3) == 1){ $inputHtml.=$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required']); }else if(($index % 3) == 2){ $inputHtml.=$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])." "; } if($vo['type'] == 'datetime'){ $datetimeScript.="//日期选择 layui.laydate.render({ elem: '#laydate_".$vo['field']."' //指定元素 });"; } $index++; } } if(($index % 3) == 1){ $inputHtml.=" "; } if(($index % 3) == 2){ $inputHtml.=" "; } $moduleInit = "var moduleInit = ['tool'];"; if($summernoteIndex>0){ $moduleInit = "var moduleInit = ['tool','tinymce'];"; } $tritems=$inputHtml.$textareaHtml.$uploadHtml.$summernoteHtml; $tplContent = str_replace('', $namespace, $tplContent); $tplContent = str_replace('', $model, $tplContent); $tplContent = str_replace('', $tritems, $tplContent); $tplContent = str_replace('', $other, $tplContent); $tplContent = str_replace('', $pk, $tplContent); $tplContent = str_replace('', $moduleInit, $tplContent); $tplContent = str_replace('', $datetimeScript, $tplContent); $tplContent = str_replace('', $uploadScript, $tplContent); $tplContent = str_replace('', $summernoteForm, $tplContent); $tplContent = str_replace('', $summernoteScript, $tplContent); file_put_contents(base_path() . $path . DS . 'view' . DS . $model . DS . 'add.html', $tplContent); } public function make_form($field, $type, $title,$required) { $required_font = ''; $required_verify = ''; if($required==1){ $required_font = '*'; $required_verify = ' lay-verify="required" lay-reqText="请完善'.$title.'"'; } $tem=[ 'input'=>''.$title.$required_font.' ', 'datetime'=>''.$title.$required_font.' ', 'radio'=>''.$title.$required_font.' ', 'checkbox'=>''.$title.$required_font.' ', 'select'=>''.$title.$required_font.' ', 'textarea'=>''.$title.$required_font.' ', 'upload'=>''.$title.$required_font.'
', 'summernote'=>''.$title.$required_font.' ' ]; return $tem[$type]; } }