EditMake.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\crud\make\make;
  3. use app\crud\make\ToAutoMake;
  4. use think\facade\App;
  5. use think\facade\Db;
  6. use think\console\Output;
  7. class EditMake implements ToAutoMake
  8. {
  9. public function check($table, $path)
  10. {
  11. !defined('DS') && define('DS', DIRECTORY_SEPARATOR);
  12. $modelName = $table;
  13. $modelFilePath = base_path() . $path . DS . 'view' . DS . $modelName . DS . 'edit.html';
  14. if (!is_dir(base_path() . $path . DS . 'view' . DS . $modelName)) {
  15. mkdir(base_path() . $path . DS . 'view'. DS . $modelName, 0755, true);
  16. }
  17. if (file_exists($modelFilePath)) {
  18. $output = new Output();
  19. $output->error("$modelName . DS . edit.html已经存在");
  20. exit;
  21. }
  22. }
  23. public function make($table, $path, $other)
  24. {
  25. $editTpl = dirname(dirname(__DIR__)) . '/tpl/edit.tpl';
  26. $tplContent = file_get_contents($editTpl);
  27. $model = $table;
  28. $filePath = empty($path) ? '' : DS . $path;
  29. $namespace = empty($path) ? '\\' : '\\' . $path . '\\';
  30. $prefix = config('database.connections.mysql.prefix');
  31. $column = Db::query('SHOW FULL COLUMNS FROM `' . $prefix . $table . '`');
  32. $pk = '';
  33. foreach ($column as $vo) {
  34. if ($vo['Key'] == 'PRI') {
  35. $pk = $vo['Field'];
  36. break;
  37. }
  38. }
  39. /*
  40. //读取数据结构生成字段
  41. $tritems ='';
  42. $detail ='$detail.';
  43. $index =0;
  44. foreach ($column as $key => $vo) {
  45. $field = $vo['Field'];
  46. $title = $vo['Comment']==''?$field:$vo['Comment'];
  47. if($field != 'id'){
  48. if(($index % 3) == 0){
  49. $tritems.="<tr>
  50. <td class='layui-td-gray-2'>{$title}</td>
  51. <td><input type='text' name='{$field}' value='{{$detail}{$field}}' placeholder='请输入{$title}' class='layui-input' autocomplete='off' /></td>";
  52. }else if(($index % 3) == 1){
  53. $tritems.="
  54. <td class='layui-td-gray-2'>{$title}</td>
  55. <td><input type='text' name='{$field}' value='{{$detail}{$field}}' placeholder='请输入{$title}' class='layui-input' autocomplete='off' /></td>";
  56. }else if(($index % 3) == 2){
  57. $tritems.="
  58. <td class='layui-td-gray-2'>{$title}</td>
  59. <td><input type='text' name='{$field}' value='{{$detail}{$field}}' placeholder='请输入{$title}' class='layui-input' autocomplete='off' /></td>
  60. </tr>
  61. ";
  62. }
  63. $index++;
  64. }
  65. }
  66. if(($index % 3) == 1){
  67. $tritems.="<td colspan='4'></td>
  68. </tr>";
  69. }
  70. if(($index % 3) == 2){
  71. $tritems.="<td colspan='2'></td>
  72. </tr>";
  73. }
  74. */
  75. //读取提交的数据生成字段
  76. $field_column = get_cache('crud_e_'.$table);
  77. $tritems ='';
  78. $index =0;
  79. $summernoteIndex=0;
  80. $inputHtml='';
  81. $textareaHtml='';
  82. $uploadHtml='';
  83. $uploadScript = '';
  84. $summernoteHtml='';
  85. $summernoteForm='';
  86. $summernoteScript='';
  87. $datetimeScript='';
  88. foreach ($field_column as $key => $vo) {
  89. $field = $vo['field'];
  90. $title = $vo['title'];
  91. if($vo['type'] == 'summernote'){
  92. $summernoteHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  93. </tr>";
  94. $summernoteScript.= "//".$vo['title']."富文本编辑器
  95. var edit = layui.tinymce.render({
  96. selector: '#container_".$vo['field']."',
  97. height: 500
  98. });";
  99. $summernoteForm.= "data.field.".$vo['field']." = tinyMCE.editors['container_".$vo['field']."'].getContent();
  100. if (data.field.".$vo['field']." == '') {
  101. layer.msg('请先完善".$vo['title']."内容');
  102. return false;
  103. }";
  104. $summernoteIndex++;
  105. }
  106. else if($vo['type'] == 'upload'){
  107. $uploadHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  108. </tr>";
  109. $uploadScript.= "//上传".$vo['title']."
  110. var upload_".$vo['field']." = layui.upload.render({
  111. elem: '#upload_btn_".$vo['field']."',
  112. url: '/admin/api/upload',
  113. done: function (res) {
  114. //如果上传失败
  115. if (res.code == 1) {
  116. return layer.msg('上传失败');
  117. }
  118. //上传成功
  119. $('#upload_box_".$vo['field']." input').attr('value', res.data.filepath);
  120. $('#upload_box_".$vo['field']." img').attr('src', res.data.filepath);
  121. }
  122. });";
  123. }else if($vo['type'] == 'textarea'){
  124. $textareaHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  125. </tr>";
  126. }else if($vo['type'] == 'textarea'){
  127. $textareaHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  128. </tr>";
  129. }
  130. else{
  131. if(($index % 3) == 0){
  132. $inputHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required']);
  133. }else if(($index % 3) == 1){
  134. $inputHtml.=$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required']);
  135. }else if(($index % 3) == 2){
  136. $inputHtml.=$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  137. </tr>
  138. ";
  139. }
  140. if($vo['type'] == 'datetime'){
  141. $datetimeScript.="//日期选择
  142. layui.laydate.render({
  143. elem: '#laydate_".$vo['field']."' //指定元素
  144. });";
  145. }
  146. $index++;
  147. }
  148. }
  149. if(($index % 3) == 1){
  150. $inputHtml.="<td colspan='4'></td>
  151. </tr>";
  152. }
  153. if(($index % 3) == 2){
  154. $inputHtml.="<td colspan='2'></td>
  155. </tr>";
  156. }
  157. $moduleInit = "var moduleInit = ['tool'];";
  158. if($summernoteIndex>0){
  159. $moduleInit = "var moduleInit = ['tool','tinymce'];";
  160. }
  161. $tritems=$inputHtml.$textareaHtml.$uploadHtml.$summernoteHtml;
  162. $tplContent = str_replace('<namespace>', $namespace, $tplContent);
  163. $tplContent = str_replace('<model>', $model, $tplContent);
  164. $tplContent = str_replace('<tritems>', $tritems, $tplContent);
  165. $tplContent = str_replace('<name>', $other, $tplContent);
  166. $tplContent = str_replace('<pk>', $pk, $tplContent);
  167. $tplContent = str_replace('<moduleInit>', $moduleInit, $tplContent);
  168. $tplContent = str_replace('<datetimeScript>', $datetimeScript, $tplContent);
  169. $tplContent = str_replace('<uploadScript>', $uploadScript, $tplContent);
  170. $tplContent = str_replace('<summernoteForm>', $summernoteForm, $tplContent);
  171. $tplContent = str_replace('<summernoteScript>', $summernoteScript, $tplContent);
  172. file_put_contents(base_path() . $path . DS . 'view' . DS . $model . DS . 'edit.html', $tplContent);
  173. }
  174. public function make_form($field, $type, $title,$required)
  175. {
  176. $required_font = '';
  177. $required_verify = '';
  178. if($required==1){
  179. $required_font = '<font>*</font>';
  180. $required_verify = ' lay-verify="required" lay-reqText="请完善'.$title.'"';
  181. }
  182. $tem=[
  183. 'input'=>'<td class="layui-td-gray-2">'.$title.$required_font.'</td>
  184. <td><input type="text" name="'.$field.'" '.$required_verify.' value="{$detail.'.$field.'}" autocomplete="off" placeholder="请输入'.$title.'" class="layui-input"></td>',
  185. 'datetime'=>'<td class="layui-td-gray-2">'.$title.$required_font.'</td>
  186. <td><input type="text" name="'.$field.'" '.$required_verify.' value="{$detail.'.$field.'|time_format=###,\'Y-m-d\'}" readonly readonly id="laydate_'.$field.'" autocomplete="off" placeholder="请选择" class="layui-input"></td>',
  187. 'radio'=>'<td class="layui-td-gray-2">'.$title.$required_font.'</td>
  188. <td>
  189. <input type="radio" name="'.$field.'" value="0" title="选项一" {eq name="$detail.'.$field.'" value="1"} checked{/eq}>
  190. <input type="radio" name="'.$field.'" value="1" title="选项二" {eq name="$detail.'.$field.'" value="2"} checked{/eq}>
  191. </td>',
  192. 'checkbox'=>'<td class="layui-td-gray-2">'.$title.$required_font.'</td>
  193. <td>
  194. <input type="checkbox" name="'.$field.'" value="1" title="选项一" lay-skin="primary" {eq name="$detail.'.$field.'" value="1"} selected{/eq}>
  195. <input type="checkbox" name="'.$field.'" value="2" title="选项二" lay-skin="primary" {eq name="$detail.'.$field.'" value="2"} selected{/eq}>
  196. </td>',
  197. 'select'=>'<td class="layui-td-gray-2">'.$title.$required_font.'</td>
  198. <td>
  199. <select name="'.$field.'" '.$required_verify.'>
  200. <option value="">请选择</option>
  201. <option value="1" {eq name="$detail.'.$field.'" value="1"} selected{/eq}>选项一</option>
  202. <option value="2" {eq name="$detail.'.$field.'" value="2"} selected{/eq}>选项二</option>
  203. </select>
  204. </td>',
  205. 'textarea'=>'<td class="layui-td-gray-2">'.$title.$required_font.'</td>
  206. <td colspan="5"><textarea name="'.$field.'" '.$required_verify.' placeholder="请输入'.$title.'" class="layui-textarea">{$detail.'.$field.'}</textarea></td>',
  207. 'upload'=>'<td class="layui-td-gray-2">'.$title.$required_font.'</td>
  208. <td colspan="5" style="vertical-align:top">
  209. <div class="layui-upload">
  210. <button type="button" class="layui-btn layui-btn-sm" id="upload_btn_'.$field.'">选择上传图片</button>
  211. <div class="layui-upload-list" id="upload_box_'.$field.'">
  212. <img src="{$detail.'.$field.'}" onerror="javascript:this.src=\'{__GOUGU__}/gougu/images/nonepic600x360.jpg\';this.onerror=null;" style="width:200px;max-width:200px" />
  213. <input type="hidden" name="'.$field.'" value="{$detail.'.$field.'}" '.$required_verify.'>
  214. </div>
  215. </div>
  216. </td>',
  217. 'summernote'=>'<td class="layui-td-gray-2">'.$title.$required_font.'</td>
  218. <td colspan="5">
  219. <textarea class="layui-textarea" id="container_'.$field.'">{$detail.'.$field.'}</textarea>
  220. </td>'
  221. ];
  222. return $tem[$type];
  223. }
  224. }