ListMake.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 ListMake 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 . 'datalist.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 datalist.html已经存在");
  20. exit;
  21. }
  22. }
  23. public function make($table, $path, $other)
  24. {
  25. $listTpl = dirname(dirname(__DIR__)) . '/tpl/list.tpl';
  26. $tplContent = file_get_contents($listTpl);
  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. $field_column = get_cache('crud_l_'.$table);
  40. $listitems="{
  41. fixed: 'left',
  42. field: 'id',
  43. title: '编号',
  44. align: 'center',
  45. width: 80
  46. },";
  47. foreach ($field_column as $key => $vo) {
  48. if($vo['field'] == 'title'){
  49. $listitems.="{
  50. field: '".$vo['field']."',
  51. title: '".$vo['title']."',
  52. },";
  53. }else{
  54. $listitems.="{
  55. field: '".$vo['field']."',
  56. title: '".$vo['title']."',
  57. align: 'center',
  58. width: 100
  59. },";
  60. }
  61. }
  62. $tplContent = str_replace('<namespace>', $namespace, $tplContent);
  63. $tplContent = str_replace('<model>', $model, $tplContent);
  64. $tplContent = str_replace('<listitems>', $listitems, $tplContent);
  65. $tplContent = str_replace('<name>', $other, $tplContent);
  66. $tplContent = str_replace('<pk>', $pk, $tplContent);
  67. file_put_contents(base_path() . $path . DS . 'view' . DS . $model . DS . 'datalist.html', $tplContent);
  68. }
  69. }