ReadMake.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 ReadMake 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 . 'read.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 . view.html已经存在");
  20. exit;
  21. }
  22. }
  23. public function make($table, $path, $other)
  24. {
  25. $readTpl = dirname(dirname(__DIR__)) . '/tpl/read.tpl';
  26. $tplContent = file_get_contents($readTpl);
  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>{{$detail}{$field}}</td>";
  52. }else if(($index % 3) == 1){
  53. $tritems.="
  54. <td class='layui-td-gray-2'>{$title}</td>
  55. <td>{{$detail}{$field}}</td>";
  56. }else if(($index % 3) == 2){
  57. $tritems.="
  58. <td class='layui-td-gray-2'>{$title}</td>
  59. <td>{{$detail}{$field}}</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_r_'.$table);
  77. $tritems ='';
  78. $index =0;
  79. $inputHtml='';
  80. $textareaHtml='';
  81. $uploadHtml='';
  82. $summernoteHtml='';
  83. foreach ($field_column as $key => $vo) {
  84. if($vo['type'] == 'summernote'){
  85. $summernoteHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  86. </tr>";
  87. }
  88. else if($vo['type'] == 'upload'){
  89. $uploadHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  90. </tr>";
  91. }else if($vo['type'] == 'textarea'){
  92. $textareaHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  93. </tr>";
  94. }
  95. else{
  96. if(($index % 3) == 0){
  97. $inputHtml.="<tr>".$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required']);
  98. }else if(($index % 3) == 1){
  99. $inputHtml.=$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required']);
  100. }else if(($index % 3) == 2){
  101. $inputHtml.=$this->make_form($vo['field'], $vo['type'], $vo['title'],$vo['required'])."
  102. </tr>";
  103. }
  104. $index++;
  105. }
  106. }
  107. if(($index % 3) == 1){
  108. $inputHtml.="<td colspan='4'></td>
  109. </tr>";
  110. }
  111. if(($index % 3) == 2){
  112. $inputHtml.="<td colspan='2'></td>
  113. </tr>";
  114. }
  115. $tritems=$inputHtml.$textareaHtml.$uploadHtml.$summernoteHtml;
  116. $tplContent = str_replace('<namespace>', $namespace, $tplContent);
  117. $tplContent = str_replace('<model>', $model, $tplContent);
  118. $tplContent = str_replace('<tritems>', $tritems, $tplContent);
  119. $tplContent = str_replace('<name>', $other, $tplContent);
  120. $tplContent = str_replace('<pk>', $pk, $tplContent);
  121. file_put_contents(base_path() . $path . DS . 'view' . DS . $model . DS . 'read.html', $tplContent);
  122. }
  123. public function make_form($field, $type, $title,$required)
  124. {
  125. $tem=[
  126. 'input'=>'<td class="layui-td-gray-2">'.$title.'</td>
  127. <td>{$detail.'.$field.'}</td>',
  128. 'datetime'=>'<td class="layui-td-gray-2">'.$title.'</td>
  129. <td>{$detail.'.$field.'|time_format=###,\'Y-m-d\'}</td>',
  130. 'radio'=>'<td class="layui-td-gray-2">'.$title.'</td>
  131. <td>
  132. {eq name="$detail.'.$field.'" value="0"}选项一{/eq}
  133. {eq name="$detail.'.$field.'" value="1"}选项二{/eq}
  134. </td>',
  135. 'checkbox'=>'<td class="layui-td-gray-2">'.$title.'</td>
  136. <td>
  137. {eq name="$detail.'.$field.'" value="1"}选项一{/eq}
  138. {eq name="$detail.'.$field.'" value="2"}选项二{/eq}
  139. </td>',
  140. 'select'=>'<td class="layui-td-gray-2">'.$title.'</td>
  141. <td>
  142. {eq name="$detail.'.$field.'" value="1"}选项一{/eq}
  143. {eq name="$detail.'.$field.'" value="2"}选项二{/eq}
  144. </td>',
  145. 'textarea'=>'<td class="layui-td-gray-2">'.$title.'</td>
  146. <td colspan="5">{$detail.'.$field.'}</td>',
  147. 'upload'=>'<td class="layui-td-gray-2">'.$title.'</td>
  148. <td colspan="5">
  149. <img src="{$detail.'.$field.'}" onerror="javascript:this.src=\'{__GOUGU__}/gougu/images/nonepic600x360.jpg\';this.onerror=null;" style="width:200px; max-width:200px" />
  150. </td>',
  151. 'summernote'=>'<td class="layui-td-gray-2">'.$title.'</td>
  152. <td colspan="5">
  153. {$detail.'.$field.'|raw}
  154. </td>'
  155. ];
  156. return $tem[$type];
  157. }
  158. }