Service.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 crmeb\services\crud\enum\FormTypeEnum;
  13. use crmeb\services\crud\enum\ServiceActionEnum;
  14. use think\helper\Str;
  15. /**
  16. * Class Business
  17. * @package crmeb\services
  18. */
  19. class Service extends Make
  20. {
  21. /**
  22. * @var string
  23. */
  24. protected $name = "services";
  25. /**
  26. * @return string
  27. * @author 等风来
  28. * @email 136327134@qq.com
  29. * @date 2023/4/4
  30. */
  31. protected function setBaseDir(): string
  32. {
  33. return 'app' . DS . 'services' . DS . 'crud';
  34. }
  35. /**
  36. * @param string $name
  37. * @param array $options
  38. * @return Service
  39. * @author 等风来
  40. * @email 136327134@qq.com
  41. * @date 2023/3/23
  42. */
  43. public function handle(string $name, array $options = [])
  44. {
  45. $path = $options['path'] ?? '';
  46. $field = $options['field'] ?? [];
  47. $columnField = $options['columnField'] ?? [];
  48. $hasOneFields = $options['hasOneField'] ?? [];
  49. $this->value['USE_PHP'] = $this->getDaoClassName($name, $path);
  50. $this->value['MODEL_NAME'] = $options['modelName'] ?? $name;
  51. $this->value['NAME_CAMEL'] = Str::studly($name);
  52. $this->value['PATH'] = $this->getfolderPath($path);
  53. return $this->setServiceContent($field, $name, $columnField, $hasOneFields, $options)
  54. ->setService($name, $path);
  55. }
  56. /**
  57. * @param string $name
  58. * @param string $path
  59. * @return $this
  60. * @author 等风来
  61. * @email 136327134@qq.com
  62. * @date 2023/8/12
  63. */
  64. protected function setService(string $name, string $path)
  65. {
  66. //生成service
  67. [$className, $content] = $this->getStubContent($name, $this->name);
  68. $this->value['NAME'] = $className;
  69. $contentStr = str_replace($this->var, $this->value, $content);
  70. $filePath = $this->getFilePathName($path, $this->value['NAME_CAMEL']);
  71. $this->usePath = $this->baseDir . '\\' . $this->value['NAME_CAMEL'];
  72. $this->setContent($contentStr);
  73. $this->setPathname($filePath);
  74. return $this;
  75. }
  76. /**
  77. * 获取请求方法
  78. * @param string $name
  79. * @return string
  80. * @author 等风来
  81. * @email 136327134@qq.com
  82. * @date 2023/9/5
  83. */
  84. protected function getActionContent(string $name)
  85. {
  86. $contentAction = '';
  87. foreach (ServiceActionEnum::SERVICE_ACTION_ALL as $item) {
  88. [, $stub] = $this->getStubContent($name, $item);
  89. $contentAction .= $stub . "\n";
  90. }
  91. return $contentAction;
  92. }
  93. /**
  94. * 获取列表展示字段
  95. * @param array $columnField
  96. * @param array $options
  97. * @return string
  98. * @author 等风来
  99. * @email 136327134@qq.com
  100. * @date 2023/8/12
  101. */
  102. protected function getSelectFieldsContent(array $columnField, array $options)
  103. {
  104. $select = [];
  105. foreach ($columnField as $item) {
  106. //处理查询字段
  107. if (in_array($item['type'], [
  108. FormTypeEnum::FRAME_IMAGES,
  109. FormTypeEnum::DATE_TIME_RANGE,
  110. FormTypeEnum::RADIO,
  111. FormTypeEnum::SELECT,
  112. FormTypeEnum::CHECKBOX
  113. ])) {
  114. $select[] = '`' . $item['field'] . '` as ' . $item['field'] . $this->attrPrefix;
  115. } else {
  116. $select[] = $item['field'];
  117. }
  118. }
  119. if (!empty($options['key'])) {
  120. array_push($select, $options['key']);
  121. }
  122. return implode(',', $select);
  123. }
  124. /**
  125. * @param array $field
  126. * @param string $name
  127. * @param array $columnField
  128. * @param array $hasOneFields
  129. * @param array $options
  130. * @return Service
  131. * @author 等风来
  132. * @email 136327134@qq.com
  133. * @date 2023/8/12
  134. */
  135. protected function setServiceContent(array $field, string $name, array $columnField, array $hasOneFields, array $options = [])
  136. {
  137. //生成form表单
  138. $var = [
  139. '{%KEY%}',
  140. '{%DATE%}',
  141. '{%ROUTE%}',
  142. '{%FORM_PHP%}',
  143. '{%MODEL_NAME%}',
  144. '{%FIELD%}',
  145. '{%WITH%}'
  146. ];
  147. $value = [
  148. $options['key'] ?? 'id',
  149. $this->value['DATE'],
  150. Str::snake($options['route'] ?? $name),
  151. $this->getFormContent($field),
  152. $options['modelName'] ?? $options['menus'] ?? $name,
  153. $this->getSelectFieldsContent($columnField, $options),
  154. $this->getWithFieldsContent($hasOneFields)
  155. ];
  156. //替换模板中的变量
  157. $this->value['CONTENT_PHP'] = str_replace($var, $value, $this->getActionContent($name));
  158. return $this;
  159. }
  160. /**
  161. * 获取表单创建内容
  162. * @param array $field
  163. * @return string
  164. * @author 等风来
  165. * @email 136327134@qq.com
  166. * @date 2023/8/12
  167. */
  168. protected function getFormContent(array $field)
  169. {
  170. $this->value['USE_PHP'] .= "\n" . 'use crmeb\services\FormBuilder;';
  171. $from = [];
  172. foreach ($field as $item) {
  173. if (in_array($item['type'], [
  174. FormTypeEnum::FRAME_IMAGES,
  175. FormTypeEnum::RADIO,
  176. FormTypeEnum::SELECT,
  177. FormTypeEnum::CHECKBOX
  178. ])) {
  179. $fieldPre = $item['field'] . $this->attrPrefix;
  180. } else {
  181. $fieldPre = $item['field'];
  182. }
  183. //处理表单信息
  184. switch ($item['type']) {
  185. case FormTypeEnum::FRAME_IMAGE_ONE:
  186. $from[] = $this->tab(2) . $this->getframeImageOnePhpContent($item['field'], $item['name']) . ';';
  187. break;
  188. case FormTypeEnum::FRAME_IMAGES:
  189. $from[] = $this->tab(2) . $this->getframeImagesPhpContent($item['field'], $item['name'], $fieldPre) . ';';
  190. break;
  191. case FormTypeEnum::DATE_TIME_RANGE:
  192. $tab = $this->tab(2);
  193. $tab3 = $this->tab(3);
  194. $from[] = <<<CONTENT
  195. {$tab}if (isset(\$info['$fieldPre'])) {
  196. {$tab3}\$time = is_array(\$info['$fieldPre']) ? \$info['$fieldPre'] : json_decode(\$info['$fieldPre'], true);
  197. {$tab}} else {
  198. {$tab3}\$time = ['', ''];
  199. {$tab}}
  200. {$tab}\$statTime = \$time[0] ?? '';
  201. {$tab}\$endTime = \$time[1] ?? '';
  202. CONTENT;
  203. $from[] = $this->tab(2) . '$rule[] = FormBuilder::' . FormTypeEnum::DATE_TIME_RANGE . '("' . $item['field'] . '", "' . $item['name'] . '", $statTime, $endTime);';
  204. break;
  205. default:
  206. $valueContent = "''";
  207. $input = '$info["' . $item['field'] . '"] ?? ';
  208. if (in_array($item['type'], [FormTypeEnum::CHECKBOX])) {
  209. $input = "is_string($input []) ? array_map('intval',(array)json_decode($input '', true)) : $input []";
  210. } else if (in_array($item['type'], [FormTypeEnum::RADIO, FormTypeEnum::SELECT])) {
  211. $input = 'isset($info[\'' . $item['field'] . '\']) ? (int)$info[\'' . $item['field'] . '\'] : \'\'';
  212. } else if (FormTypeEnum::SWITCH === $item['type']) {
  213. $input = "isset(\$info['" . $item['field'] . "']) ? (string)\$info['" . $item['field'] . "'] : ''";
  214. } else {
  215. $input = $input . $valueContent;
  216. }
  217. $from[] = $this->tab(2) . '$rule[] = FormBuilder::' . $item['type'] . '("' . $item['field'] . '", "' . $item['name'] . '", ' . $input . ')' . $this->getOptionContent(in_array($item['type'], ['radio', 'select', 'checkbox']), $item['option'] ?? []) . ';';
  218. break;
  219. }
  220. }
  221. return $from ? implode("\n", $from) : '';
  222. }
  223. /**
  224. * 获取关联查询内容
  225. * @param array $hasOneFields
  226. * @return string
  227. * @author 等风来
  228. * @email 136327134@qq.com
  229. * @date 2023/8/12
  230. */
  231. protected function getWithFieldsContent(array $hasOneFields)
  232. {
  233. $with = [];
  234. foreach ($hasOneFields as $item) {
  235. if (isset($item['hasOne'])) {
  236. [$modelName,] = is_array($item['hasOne']) ? $item['hasOne'] : [$item['hasOne'], 'id'];
  237. $modelName = Model::getHasOneNamePases($modelName);
  238. if (!$modelName) {
  239. continue;
  240. }
  241. $with[] = "'" . Str::camel($item['field']) . 'HasOne' . "'";
  242. }
  243. }
  244. return $with ? '[' . implode(',', $with) . ']' : '[]';
  245. }
  246. /**
  247. * 获取选项内容
  248. * @param bool $isOption
  249. * @param array $option
  250. * @return string
  251. * @author 等风来
  252. * @email 136327134@qq.com
  253. * @date 2023/3/23
  254. */
  255. protected function getOptionContent(bool $isOption, array $option = [])
  256. {
  257. if (!$isOption) {
  258. return '';
  259. }
  260. $php = '';
  261. if ($option) {
  262. $attOption = [];
  263. foreach ($option as $item) {
  264. $value = (int)$item['value'];
  265. $attOption[] = $this->tab(3) . "['value'=>{$value}, 'label'=>'{$item['label']}'],";
  266. }
  267. $strOption = implode("\n", $attOption);
  268. $php = "->options([\n" . $strOption . "\n" . $this->tab(2) . "])";
  269. }
  270. return $php;
  271. }
  272. /**
  273. * 单图获取formphp内容
  274. * @param string $field
  275. * @param string $name
  276. * @param bool $required
  277. * @param string $icon
  278. * @param string $width
  279. * @param string $height
  280. * @return string
  281. * @author 等风来
  282. * @email 136327134@qq.com
  283. * @date 2023/4/14
  284. */
  285. protected function getframeImageOnePhpContent(string $field, string $name, bool $required = false, string $icon = 'el-icon-picture-outline', string $width = '950px', string $height = '560px')
  286. {
  287. $name = addslashes($name);
  288. $requiredText = $required ? '->required()' : '';
  289. $content = <<<CONTENT
  290. \$rule[] = FormBuilder::frameImage('$field', '$name', url(config('app.admin_prefix', 'admin') . '/widget.images/index', ['fodder' => '$field']), \$info['$field'] ?? '')->icon('$icon')->width('$width')->height('$height')->Props(['footer' => false])$requiredText
  291. CONTENT;
  292. return $content;
  293. }
  294. /**
  295. * 多图获取formphp内容
  296. * @param string $field
  297. * @param string $name
  298. * @param bool $required
  299. * @param string $icon
  300. * @param int $maxLength
  301. * @param string $width
  302. * @param string $height
  303. * @return string
  304. * @author 等风来
  305. * @email 136327134@qq.com
  306. * @date 2023/4/14
  307. */
  308. protected function getframeImagesPhpContent(string $field, string $name, bool $required = false, string $icon = 'el-icon-picture-outline', int $maxLength = 10, string $width = '950px', string $height = '560px')
  309. {
  310. $name = addslashes($name);
  311. $requiredText = $required ? '->required()' : '';
  312. $tab = $this->tab(2);
  313. $tab3 = $this->tab(3);
  314. $content = <<<CONTENT
  315. if (isset(\$info['$field'])) {
  316. {$tab3}\$pics = is_array(\$info['$field']) ? \$info['$field'] : json_decode(\$info['$field'], true);
  317. {$tab}} else {
  318. {$tab3}\$pics = [];
  319. {$tab}}
  320. {$tab}\$pics = is_array(\$pics) ? \$pics : [];
  321. {$tab}\$rule[] = FormBuilder::frameImages('$field', '$name', url(config('app.admin_prefix', 'admin') . '/widget.images/index', ['fodder' => '$field', 'type' => 'many', 'maxLength' => $maxLength]), \$pics)->maxLength($maxLength)->icon('$icon')->width('$width')->height('$height')->Props(['footer' => false])$requiredText
  322. CONTENT;
  323. return $content;
  324. }
  325. /**
  326. * @param string $name
  327. * @param string $path
  328. * @return string
  329. * @author 等风来
  330. * @email 136327134@qq.com
  331. * @date 2023/3/23
  332. */
  333. protected function getDaoClassName(string $name, string $path)
  334. {
  335. $path = str_replace(['app\\services', 'app/services'], '', $path);
  336. $path = ltrim(str_replace('\\', '/', $path), '/');
  337. return 'use app\dao\crud\\' . ($path ? $path . '\\' : '') . Str::studly($name) . 'Dao;';
  338. }
  339. /**
  340. * @param string $type
  341. * @return string|string[]
  342. * @author 等风来
  343. * @email 136327134@qq.com
  344. * @date 2023/3/13
  345. */
  346. protected function getStub(string $type = 'services')
  347. {
  348. $servicePath = __DIR__ . DS . 'stubs' . DS . 'service' . DS;
  349. $stubs = [
  350. 'index' => $servicePath . 'crudListIndex.stub',
  351. 'form' => $servicePath . 'getCrudForm.stub',
  352. 'save' => $servicePath . 'crudSave.stub',
  353. 'update' => $servicePath . 'crudUpdate.stub',
  354. 'services' => $servicePath . 'crudService.stub',
  355. ];
  356. return $type ? $stubs[$type] : $stubs;
  357. }
  358. }