ViewPages.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace crmeb\services\crud;
  14. use think\App;
  15. use think\helper\Str;
  16. /**
  17. * Class ViewPages
  18. * @author 等风来
  19. * @email 136327134@qq.com
  20. * @date 2023/4/1
  21. * @package crmeb\services\crud
  22. */
  23. class ViewPages extends Make
  24. {
  25. /**
  26. * @var string
  27. */
  28. protected $name = 'pages';
  29. /**
  30. * @var string
  31. */
  32. protected $fileMime = 'vue';
  33. /**
  34. * ViewPages constructor.
  35. * @param App $app
  36. */
  37. public function __construct(App $app)
  38. {
  39. parent::__construct($app);
  40. $this->basePath = $this->adminTemplatePath;
  41. }
  42. /**
  43. * @return string
  44. * @author 等风来
  45. * @email 136327134@qq.com
  46. * @date 2023/4/4
  47. */
  48. protected function setBaseDir(): string
  49. {
  50. return 'pages' . DS . 'crud';
  51. }
  52. /**
  53. * @param string $name
  54. * @param string $path
  55. * @param array $options
  56. * @return ViewPages
  57. * @author 等风来
  58. * @email 136327134@qq.com
  59. * @date 2023/4/3
  60. */
  61. public function handle(string $name, array $options = [])
  62. {
  63. $field = $options['field'] ?? [];
  64. $route = $options['route'] ?? '';
  65. $columnStr = [];
  66. $contentVue = [];
  67. foreach ($field as $key => $item) {
  68. $keyName = 'key';
  69. $fieldValue = $item['field'];
  70. if (isset($item['type'])) {
  71. switch ($item['type']) {
  72. case 'frameImageOne':
  73. $keyName = 'slot';
  74. $templateContent = file_get_contents($this->getStub('image'));
  75. $contentVue[] = str_replace(['{%field%}'], [$item['field']], $templateContent);
  76. break;
  77. case 'frameImages':
  78. $keyName = 'slot';
  79. $templateContent = file_get_contents($this->getStub('images'));
  80. $contentVue[] = str_replace(['{%field%}'], [$item['field']], $templateContent);
  81. break;
  82. }
  83. if (in_array($item['type'], ['radio', 'select', 'checkbox'])) {
  84. $fieldValue = $fieldValue . $this->attrPrefix;
  85. }
  86. }
  87. $columnStr[] = ($key == 0 ? $this->tab(2) : '') . "{\n" . $this->tab(3) . "title:\"{$item['name']}\"," . $this->tab(2) . "\n" . $this->tab(3) . "$keyName:\"{$fieldValue}\"\n" . $this->tab(2) . "}\n";
  88. }
  89. $this->value['auth'] = Str::snake($name);
  90. $this->value['componentName'] = $this->value['auth'];
  91. $this->value['key'] = $options['key'] ?? 'id';
  92. $this->value['route'] = $route;
  93. $this->value['content-vue'] = $columnStr ? "\n" . implode($this->tab(2) . ',', $columnStr) . $this->tab(2) . ',' : '';
  94. $this->value['pathApiJs'] = $options['pathApiJs'] ?? '';
  95. $this->value['nameStudly'] = Str::studly($name);
  96. $this->value['nameCamel'] = Str::camel($name);
  97. $this->value['content-table-vue'] = $contentVue ? implode("\n", $contentVue) : '';
  98. return parent::handle($name, $options);
  99. }
  100. /**
  101. * @param string $path
  102. * @param string $name
  103. * @return string
  104. * @author 等风来
  105. * @email 136327134@qq.com
  106. * @date 2023/4/4
  107. */
  108. protected function getFilePathName(string $path, string $name): string
  109. {
  110. $path = ltrim(str_replace('\\', '/', $path), '/');
  111. return $this->getBasePath($path) . $name . DS . 'index.' . $this->fileMime;
  112. }
  113. /**
  114. * @param string $type
  115. * @return string
  116. * @author 等风来
  117. * @email 136327134@qq.com
  118. * @date 2023/4/1
  119. */
  120. protected function getStub(string $type = 'index')
  121. {
  122. $pagesPath = __DIR__ . DS . 'stubs' . DS . 'view' . DS . 'pages' . DS . 'crud' . DS;
  123. $stubs = [
  124. 'index' => $pagesPath . 'index.stub',
  125. 'image' => $pagesPath . 'image.stub',
  126. 'images' => $pagesPath . 'images.stub',
  127. ];
  128. return $type ? $stubs[$type] : $stubs['index'];
  129. }
  130. }