AddMake.php 8.4 KB

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