Service.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\crud;
  12. use think\helper\Str;
  13. /**
  14. * Class Business
  15. * @package crmeb\services
  16. */
  17. class Service extends Make
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $name = "services";
  23. /**
  24. * @return string
  25. * @author 等风来
  26. * @email 136327134@qq.com
  27. * @date 2023/4/4
  28. */
  29. protected function setBaseDir(): string
  30. {
  31. return 'app' . DS . 'services' . DS . 'crud';
  32. }
  33. /**
  34. * @param string $name
  35. * @param array $options
  36. * @return Service
  37. * @author 等风来
  38. * @email 136327134@qq.com
  39. * @date 2023/3/23
  40. */
  41. public function handle(string $name, array $options = [])
  42. {
  43. $path = $options['path'] ?? '';
  44. $this->value['use-php'] = $this->getDaoClassName($name, $path);
  45. $this->value['modelName'] = $options['modelName'] ?? $name;
  46. $field = $options['field'] ?? [];
  47. $action = ['index', 'form', 'save', 'update'];
  48. $contentAction = '';
  49. foreach ($action as $item) {
  50. [$class, $stub] = $this->getStubContent($name, $item);
  51. $contentAction .= $stub . "\n";
  52. }
  53. if ($field) {
  54. //生成form表单
  55. $var = ['{%key%}', '{%date%}', '{%route%}', '{%form-php%}', '{%modelName%}', '{%field%}'];
  56. $value = [$options['key'] ?? 'id', $this->value['date'], Str::snake($options['route'] ?? $name)];
  57. $from = [];
  58. $select = [];
  59. foreach ($field as $item) {
  60. //处理查询字段
  61. if (in_array($item['type'], ['radio', 'select', 'checkbox'])) {
  62. $select[] = $item['field'] . ' as ' . $item['field'] . $this->attrPrefix;
  63. } else {
  64. $select[] = $item['field'];
  65. }
  66. //处理表单信息
  67. switch ($item['type']) {
  68. case 'frameImageOne':
  69. $from[] = $this->tab(2) . $this->getframeImageOnePhpContent($item['field'], $item['name']) . ';';
  70. break;
  71. case 'frameImages':
  72. $from[] = $this->tab(2) . $this->getframeImagesPhpContent($item['field'], $item['name']) . ';';
  73. break;
  74. default:
  75. $valueContent = "''";
  76. $input = '$info["' . $item['field'] . '"] ?? ';
  77. if (in_array($item['type'], ['checkbox'])) {
  78. $valueContent = '[]';
  79. $input = '(array)(' . $input . $valueContent . ')';
  80. } else if ($item['type'] == 'radio') {
  81. $valueContent = '-1';
  82. $input = '(int)(' . $input . $valueContent . ')';
  83. } else {
  84. $input = $input . $valueContent;
  85. }
  86. $from[] = $this->tab(2) . '$rule[] = FormBuilder::' . $item['type'] . '("' . $item['field'] . '", "' . $item['name'] . '", ' . $input . ')' . $this->getOptionContent(in_array($item['type'], ['radio', 'select', 'checkbox']), $item['option'] ?? []) . ';';
  87. break;
  88. }
  89. }
  90. if ($from) {
  91. $this->value['use-php'] .= "\n" . 'use crmeb\services\FormBuilder;';
  92. $value[] = implode("\n", $from);
  93. } else {
  94. $value[] = '';
  95. }
  96. $value[] = $options['modelName'] ?? $options['menus'] ?? $name;
  97. if (!empty($options['key'])) {
  98. array_push($select, $options['key']);
  99. }
  100. $value[] = implode(',', $select);
  101. if ($value && $var) {
  102. $contentAction = str_replace($var, $value, $contentAction);
  103. }
  104. }
  105. //生成service
  106. [$className, $content] = $this->getStubContent($name, $this->name);
  107. $this->value['nameCamel'] = Str::studly($name);
  108. $this->value['name'] = $className;
  109. $this->value['path'] = $this->getfolderPath($path);
  110. $this->value['content-php'] = $contentAction;
  111. $contentStr = str_replace($this->var, $this->value, $content);
  112. $filePath = $this->getFilePathName($path, $this->value['nameCamel']);
  113. $this->usePath = $this->baseDir . '\\' . $this->value['nameCamel'];
  114. $this->setContent($contentStr);
  115. $this->setPathname($filePath);
  116. return $this;
  117. }
  118. /**
  119. * @param array $option
  120. * @return string
  121. * @author 等风来
  122. * @email 136327134@qq.com
  123. * @date 2023/3/23
  124. */
  125. protected function getOptionContent(bool $isOption, array $option = [])
  126. {
  127. if (!$isOption) {
  128. return '';
  129. }
  130. if (!$option) {
  131. $option = [
  132. ['value' => 1, 'label' => '开启'],
  133. ['value' => 0, 'label' => '关闭']
  134. ];
  135. }
  136. $php = '';
  137. if ($option) {
  138. $attOption = [];
  139. foreach ($option as $item) {
  140. $value = (int)$item['value'];
  141. $attOption[] = $this->tab(3) . "['value'=>{$value}, 'label'=>'{$item['label']}'],";
  142. }
  143. $strOption = implode("\n", $attOption);
  144. $php = "->options([\n" . $strOption . "\n" . $this->tab(2) . "])";
  145. }
  146. return $php;
  147. }
  148. /**
  149. * 单图获取formphp内容
  150. * @param string $field
  151. * @param string $name
  152. * @param string $icon
  153. * @param string $width
  154. * @param string $height
  155. * @return string
  156. * @author 等风来
  157. * @email 136327134@qq.com
  158. * @date 2023/4/14
  159. */
  160. protected function getframeImageOnePhpContent(string $field, string $name, bool $required = false, string $icon = 'ios-add', string $width = '950px', string $height = '560px')
  161. {
  162. $name = addslashes($name);
  163. $requiredText = $required ? '->required()' : '';
  164. $content = <<<CONTENT
  165. \$rule[] = FormBuilder::frameImage('$field', '$name', url(config('app.admin_prefix', 'admin') . '/widget.images/index', ['fodder' => '$field']), \$info['$field'] ?? '')->icon('$icon')->width('$width')->height('$height')->modal(['footer-hide' => true])$requiredText
  166. CONTENT;
  167. return $content;
  168. }
  169. /**
  170. * 多图获取formphp内容
  171. * @param string $field
  172. * @param string $name
  173. * @param string $icon
  174. * @param int $maxLength
  175. * @param string $width
  176. * @param string $height
  177. * @return string
  178. * @author 等风来
  179. * @email 136327134@qq.com
  180. * @date 2023/4/14
  181. */
  182. protected function getframeImagesPhpContent(string $field, string $name, bool $required = false, string $icon = 'ios-images', int $maxLength = 10, string $width = '950px', string $height = '560px')
  183. {
  184. $name = addslashes($name);
  185. $requiredText = $required ? '->required()' : '';
  186. $content = <<<CONTENT
  187. \$rule[] = FormBuilder::frameImages('$field', '$name', url(config('app.admin_prefix', 'admin') . '/widget.images/index', ['fodder' => '$field', 'type' => 'many', 'maxLength' => $maxLength]), \$info['$field'] ?? [])->maxLength($maxLength)->icon('$icon')->width('$width')->height('$height')->modal(['footer-hide' => true])$requiredText
  188. CONTENT;
  189. return $content;
  190. }
  191. /**
  192. * @param string $name
  193. * @param string $path
  194. * @return string
  195. * @author 等风来
  196. * @email 136327134@qq.com
  197. * @date 2023/3/23
  198. */
  199. protected function getDaoClassName(string $name, string $path)
  200. {
  201. $path = str_replace(['app\\services', 'app/services'], '', $path);
  202. $path = ltrim(str_replace('\\', '/', $path), '/');
  203. return 'use app\dao\crud\\' . ($path ? $path . '\\' : '') . Str::studly($name) . 'Dao;';
  204. }
  205. /**
  206. * @param string $type
  207. * @return mixed|string|string[]
  208. * @author 等风来
  209. * @email 136327134@qq.com
  210. * @date 2023/3/13
  211. */
  212. protected function getStub(string $type = 'services')
  213. {
  214. $servicePath = __DIR__ . DS . 'stubs' . DS . 'service' . DS;
  215. $stubs = [
  216. 'index' => $servicePath . 'crudListIndex.stub',
  217. 'form' => $servicePath . 'getCrudForm.stub',
  218. 'save' => $servicePath . 'crudSave.stub',
  219. 'update' => $servicePath . 'crudUpdate.stub',
  220. 'services' => $servicePath . 'crudService.stub',
  221. ];
  222. return $type ? $stubs[$type] : $stubs;
  223. }
  224. }