ViewApi.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 crmeb\exceptions\CrudException;
  15. use crmeb\services\crud\enum\ActionEnum;
  16. use think\App;
  17. use think\helper\Str;
  18. /**
  19. * Class ViewApi
  20. * @author 等风来
  21. * @email 136327134@qq.com
  22. * @date 2023/4/1
  23. * @package crmeb\services\crud
  24. */
  25. class ViewApi extends Make
  26. {
  27. /**
  28. * @var string
  29. */
  30. protected $name = 'api';
  31. /**
  32. * @var string
  33. */
  34. protected $fileMime = 'js';
  35. /**
  36. * ViewApi constructor.
  37. * @param App $app
  38. */
  39. public function __construct(App $app)
  40. {
  41. parent::__construct($app);
  42. $this->basePath = $this->adminTemplatePath;
  43. }
  44. /**
  45. * @return string
  46. * @author 等风来
  47. * @email 136327134@qq.com
  48. * @date 2023/4/4
  49. */
  50. protected function setBaseDir(): string
  51. {
  52. return 'api' . DS . 'crud';
  53. }
  54. /**
  55. * @param string $name
  56. * @param array $options
  57. * @return ViewApi
  58. * @author 等风来
  59. * @email 136327134@qq.com
  60. * @date 2023/4/4
  61. */
  62. public function handle(string $name, array $options = [])
  63. {
  64. $path = $options['path'] ?? '';
  65. $route = $options['route'] ?? '';
  66. if (!$route) {
  67. throw new CrudException(500045);
  68. }
  69. return $this->setJsContent($name, $route)
  70. ->setApi($name, $path);
  71. }
  72. /**
  73. * 设置页面JS内容
  74. * @param string $name
  75. * @param string $route
  76. * @return $this
  77. * @author 等风来
  78. * @email 136327134@qq.com
  79. * @date 2023/8/12
  80. */
  81. protected function setJsContent(string $name, string $route)
  82. {
  83. $contentJs = '';
  84. foreach (ActionEnum::ACTION_ALL as $item) {
  85. $contentJs .= file_get_contents($this->getStub($item)) . "\n";
  86. }
  87. $var = [
  88. '{%ROUTE%}',
  89. '{%NAME_CAMEL%}',
  90. '{%NAME_STUDLY%}',
  91. ];
  92. $value = [
  93. $route,
  94. Str::camel($name),
  95. Str::studly($name),
  96. ];
  97. $contentJs = str_replace($var, $value, $contentJs);
  98. $this->value['CONTENT_JS'] = $contentJs;
  99. return $this;
  100. }
  101. /**
  102. * 设置页面api内容
  103. * @param string $name
  104. * @param string $path
  105. * @return $this
  106. * @author 等风来
  107. * @email 136327134@qq.com
  108. * @date 2023/8/12
  109. */
  110. protected function setApi(string $name, string $path)
  111. {
  112. //生成api
  113. [, $content] = $this->getStubContent($name, $this->name);
  114. $contentStr = str_replace($this->var, $this->value, $content);
  115. $filePath = $this->getFilePathName($path, Str::camel($name));
  116. $this->setPathname($filePath);
  117. $this->setContent($contentStr);
  118. return $this;
  119. }
  120. /**
  121. * @param string $path
  122. * @param string $name
  123. * @return string
  124. * @author 等风来
  125. * @email 136327134@qq.com
  126. * @date 2023/4/4
  127. */
  128. protected function getFilePathName(string $path, string $name): string
  129. {
  130. $path = ltrim(str_replace('\\', '/', $path), '/');
  131. return $this->getBasePath($path) . $name . '.' . $this->fileMime;
  132. }
  133. /**
  134. * 模板文件配置
  135. * @param string $type
  136. * @return mixed
  137. */
  138. protected function getStub(string $type = 'api')
  139. {
  140. $servicePath = __DIR__ . DS . 'stubs' . DS . 'view' . DS . 'api' . DS;
  141. $stubs = [
  142. 'index' => $servicePath . 'getCrudListApi.stub',
  143. 'create' => $servicePath . 'getCrudCreateApi.stub',
  144. 'save' => $servicePath . 'crudSaveApi.stub',
  145. 'status' => $servicePath . 'crudStatusApi.stub',
  146. 'edit' => $servicePath . 'getCrudEditApi.stub',
  147. 'read' => $servicePath . 'getCrudReadApi.stub',
  148. 'delete' => $servicePath . 'crudDeleteApi.stub',
  149. 'update' => $servicePath . 'crudUpdateApi.stub',
  150. 'api' => $servicePath . 'crud.stub',
  151. ];
  152. return $type ? $stubs[$type] : $stubs;
  153. }
  154. }