ViewPages.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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\services\crud\enum\FormTypeEnum;
  15. use crmeb\services\crud\enum\SearchEnum;
  16. use think\App;
  17. use think\helper\Str;
  18. /**
  19. * Class ViewPages
  20. * @author 等风来
  21. * @email 136327134@qq.com
  22. * @date 2023/4/1
  23. * @package crmeb\services\crud
  24. */
  25. class ViewPages extends Make
  26. {
  27. /**
  28. * @var string
  29. */
  30. protected $name = 'pages';
  31. /**
  32. * @var string
  33. */
  34. protected $fileMime = 'vue';
  35. /**
  36. * ViewPages 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 'pages' . DS . 'crud';
  53. }
  54. /**
  55. * @param string $name
  56. * @param string $path
  57. * @param array $options
  58. * @return ViewPages
  59. * @author 等风来
  60. * @email 136327134@qq.com
  61. * @date 2023/4/3
  62. */
  63. public function handle(string $name, array $options = [])
  64. {
  65. $field = $options['field'] ?? [];
  66. $route = $options['route'] ?? '';
  67. $modelName = $options['modelName'] ?? $name;
  68. $tableFields = $options['tableFields'] ?? [];
  69. $searchFields = $options['searchField'] ?? [];
  70. $this->value['AUTH'] = Str::snake($name);
  71. $this->value['COMPONENT_NAME'] = $this->value['AUTH'];
  72. $this->value['KEY'] = $options['key'] ?? 'id';
  73. $this->value['ROUTE'] = $route;
  74. $this->value['PATH_API_JS'] = $options['pathApiJs'] ?? '';
  75. $this->value['NAME_STUDLY'] = Str::studly($name);
  76. $this->value['NAME_CAMEL'] = Str::camel($name);
  77. $this->value['MODEL_NAME'] = $modelName;
  78. $this->setTableVueContent($field)
  79. ->setSearchVueContent($searchFields)
  80. ->setDescriptionContent($tableFields);
  81. return parent::handle($name, $options);
  82. }
  83. /**
  84. * 设置页面table内容
  85. * @param array $field
  86. * @return $this
  87. * @author 等风来
  88. * @email 136327134@qq.com
  89. * @date 2023/8/12
  90. */
  91. protected function setTableVueContent(array $field)
  92. {
  93. $contentVue = [];
  94. foreach ($field as $item) {
  95. $fieldValue = $item['field'];
  96. if (isset($item['type'])) {
  97. if (in_array($item['type'], [FormTypeEnum::RADIO, FormTypeEnum::SELECT, FormTypeEnum::CHECKBOX, FormTypeEnum::FRAME_IMAGES])) {
  98. $fieldValue = $fieldValue . $this->attrPrefix;
  99. }
  100. //组合表单展示数据
  101. switch ($item['type']) {
  102. case FormTypeEnum::FRAME_IMAGE_ONE:
  103. $templateContent = file_get_contents($this->getStub('image'));
  104. $contentVue[] = str_replace([
  105. '{%FIELD%}',
  106. '{%NAME%}'
  107. ], [
  108. $item['field'],
  109. $item['name']
  110. ], $templateContent);
  111. break;
  112. case FormTypeEnum::FRAME_IMAGES:
  113. $templateContent = file_get_contents($this->getStub('images'));
  114. $contentVue[] = str_replace([
  115. '{%FIELD%}',
  116. '{%NAME%}'
  117. ], [
  118. $fieldValue,
  119. $item['name']
  120. ], $templateContent);
  121. break;
  122. case FormTypeEnum::DATE_TIME_RANGE:
  123. $tab = $this->tab(2);
  124. $tab3 = $this->tab(3);
  125. $tab4 = $this->tab(4);
  126. $contentVue[] = <<<CONTENT
  127. $tab<el-table-column label="$item[name]">
  128. $tab3<template slot-scope="scope">
  129. $tab4<span>{{scope.row.{$fieldValue}[0]}}-- {{scope.row.{$fieldValue}[1]}}</span>
  130. $tab3</template>
  131. $tab</el-table-column>
  132. CONTENT;
  133. break;
  134. case FormTypeEnum::SWITCH:
  135. $tab = $this->tab(2);
  136. $tab3 = $this->tab(3);
  137. $tab4 = $this->tab(4);
  138. $contentVue[] = <<<CONTENT
  139. $tab<el-table-column label="$item[name]">
  140. $tab3<template slot-scope="scope">
  141. $tab4<<el-switch :active-value="1" :inactive-value="0" v-model="scope.row.{$fieldValue}" :value="scope.row.{$fieldValue}" size="large" @change="updateStatus(scope.row,'{$fieldValue}')" />
  142. $tab3</template>
  143. $tab</el-table-column>
  144. CONTENT;
  145. break;
  146. default:
  147. $tab = $this->tab(2);
  148. $contentVue[] = <<<CONTENT
  149. $tab<el-table-column prop="$fieldValue" label="$item[name]">
  150. $tab</el-table-column>
  151. CONTENT;
  152. }
  153. }
  154. }
  155. $this->value['CONTENT_TABLE_VUE'] = $contentVue ? implode("\n", $contentVue) : '';
  156. return $this;
  157. }
  158. /**
  159. * 设置搜索页面
  160. * @param array $searchFields
  161. * @return ViewPages
  162. * @author 等风来
  163. * @email 136327134@qq.com
  164. * @date 2023/8/12
  165. */
  166. protected function setSearchVueContent(array $searchFields)
  167. {
  168. $contentSearchVue = [];
  169. //页面顶部搜索
  170. $fieldDatas = [];
  171. foreach ($searchFields as $item) {
  172. $fieldValue = $item['field'];
  173. $tab = $this->tab(2);
  174. $fieldDatas[] = $tab . $fieldValue . ":''";
  175. if (isset($item['search']) && $item['search']) {
  176. if (!$item['type']) {
  177. switch ($item['search']) {
  178. case SearchEnum::SEARCH_TYPE_BETWEEN:
  179. $item['type'] = FormTypeEnum::DATE_TIME;
  180. break;
  181. }
  182. }
  183. switch ($item['type']) {
  184. case FormTypeEnum::DATE_TIME:
  185. case FormTypeEnum::DATE_TIME_RANGE:
  186. $templateContent = file_get_contents($this->getStub('dataPicker'));
  187. $contentSearchVue[] = str_replace([
  188. '{%FIELD%}',
  189. '{%NAME%}'
  190. ], [
  191. $fieldValue,
  192. $item['name']
  193. ], $templateContent);
  194. break;
  195. case FormTypeEnum::CHECKBOX:
  196. case FormTypeEnum::RADIO:
  197. case FormTypeEnum::SELECT:
  198. $optionContentVue = [];
  199. if (!empty($item['options'])) {
  200. $tab = $this->tab(2);
  201. foreach ($item['options'] as $option) {
  202. $optionContentVue[] = <<<CONTENT
  203. $tab<el-option value="$option[value]" label="$option[label]"></el-option>
  204. CONTENT;
  205. }
  206. }
  207. if ($optionContentVue) {
  208. $templateContent = file_get_contents($this->getStub('select'));
  209. $contentSearchVue[] = str_replace(
  210. [
  211. '{%FIELD%}',
  212. '{%NAME%}',
  213. '{%CONTENT_SELECT_OPTION_VUE%}'
  214. ],
  215. [
  216. $fieldValue,
  217. $item['name'],
  218. implode("\n", $optionContentVue)
  219. ],
  220. $templateContent
  221. );
  222. }
  223. break;
  224. default:
  225. $templateContent = file_get_contents($this->getStub('input'));
  226. $contentSearchVue[] = str_replace([
  227. '{%FIELD%}',
  228. '{%NAME%}'
  229. ], [
  230. $fieldValue,
  231. $item['name']
  232. ], $templateContent);
  233. break;
  234. }
  235. }
  236. }
  237. $this->value['FROM_DATA_CONTENT_VUE'] = $fieldDatas ? "\n" . implode(",\n", $fieldDatas) . ',' : '';
  238. if ($contentSearchVue) {
  239. $templateContent = file_get_contents($this->getStub('form'));
  240. $this->value['CONTENT_SEARCH_VUE'] = str_replace(
  241. [
  242. '{%CONTENT_FORM_VUE%}'
  243. ],
  244. [
  245. implode("\n", $contentSearchVue)
  246. ],
  247. $templateContent);
  248. $this->value['CLASS_NAME'] = 'mt16';
  249. }
  250. return $this;
  251. }
  252. /**
  253. * 获取查看详情字段展示内容
  254. * @param array $tableFields
  255. * @return ViewPages
  256. * @author 等风来
  257. * @email 136327134@qq.com
  258. * @date 2023/8/12
  259. */
  260. protected function setDescriptionContent(array $tableFields)
  261. {
  262. $descriptionContent = '';
  263. foreach ($tableFields as $item) {
  264. $tab = $this->tab(3);
  265. $fieldValue = $item['field'];
  266. if (in_array($item['from_type'], [FormTypeEnum::RADIO, FormTypeEnum::SELECT, FormTypeEnum::CHECKBOX, FormTypeEnum::FRAME_IMAGES])) {
  267. $fieldValue = $fieldValue . $this->attrPrefix;
  268. }
  269. if (FormTypeEnum::FRAME_IMAGES === $item['from_type']) {
  270. $descriptionContent .= <<<CONTENT
  271. $tab<el-descriptions-item label="$item[comment]"><el-image v-for="item in info.$fieldValue" :src="item" :preview-src-list="info.$fieldValue"></el-descriptions-item>\n
  272. CONTENT;
  273. } else if (FormTypeEnum::FRAME_IMAGE_ONE === $item['from_type']) {
  274. $descriptionContent .= <<<CONTENT
  275. $tab<el-descriptions-item label="$item[comment]"><el-image :src="info.$fieldValue" :preview-src-list="info.$fieldValue"></el-descriptions-item>\n
  276. CONTENT;
  277. } else {
  278. $descriptionContent .= <<<CONTENT
  279. $tab<el-descriptions-item label="$item[comment]">{{info.$fieldValue}}</el-descriptions-item>\n
  280. CONTENT;
  281. }
  282. }
  283. $this->value['CONTENT_DESCRIPTIONS_VUE'] = $descriptionContent;
  284. return $this;
  285. }
  286. /**
  287. * @param string $path
  288. * @param string $name
  289. * @return string
  290. * @author 等风来
  291. * @email 136327134@qq.com
  292. * @date 2023/4/4
  293. */
  294. protected function getFilePathName(string $path, string $name): string
  295. {
  296. $path = ltrim(str_replace('\\', '/', $path), '/');
  297. return $this->getBasePath($path) . $name . DS . 'index.' . $this->fileMime;
  298. }
  299. /**
  300. * 获取模板内容
  301. * @param string $type
  302. * @return string
  303. * @author 等风来
  304. * @email 136327134@qq.com
  305. * @date 2023/4/1
  306. */
  307. protected function getStub(string $type = 'index')
  308. {
  309. $pagesPath = __DIR__ . DS . 'stubs' . DS . 'view' . DS . 'pages' . DS . 'crud' . DS;
  310. $stubs = [
  311. 'index' => $pagesPath . 'index.stub',
  312. 'image' => $pagesPath . 'image.stub',
  313. 'images' => $pagesPath . 'images.stub',
  314. 'input' => $pagesPath . 'input.stub',
  315. 'form' => $pagesPath . 'form.stub',
  316. 'select' => $pagesPath . 'select.stub',
  317. 'dataPicker' => $pagesPath . 'date-picker.stub',
  318. ];
  319. return $type ? $stubs[$type] : $stubs['index'];
  320. }
  321. }