TemplateLibraryController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. /**
  3. * 模版库管理(弃用)
  4. * @copyright 2021-浙江引擎力营销策划有限公司
  5. * @author Lc<sunshinecc1@163.com>
  6. * @since 2021-08-01
  7. */
  8. namespace App\Http\Controllers\Admin\Stencil;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Models\Site;
  11. use App\Http\Models\TemplateLibrary;
  12. use App\Http\Models\TemplateLibraryRelation;
  13. use App\Http\Models\TemplateLibraryVar;
  14. use App\Http\Services\TemplateLibraryApiService;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Contracts\View\Factory;
  18. use Illuminate\View\View;
  19. use Illuminate\Http\JsonResponse;
  20. class TemplateLibraryController extends Controller
  21. {
  22. private $templateLibraryApiService;
  23. private $siteId;
  24. /**
  25. * 模版站服务类
  26. * TplController constructor.
  27. * @param TemplateLibraryApiService $templateLibraryApiService
  28. */
  29. public function __construct(TemplateLibraryApiService $templateLibraryApiService)
  30. {
  31. $this->templateLibraryApiService = $templateLibraryApiService;
  32. $this->siteId = 360;
  33. }
  34. /**
  35. * 模版库管理
  36. * @return Factory|View
  37. */
  38. public function templateLibrary()
  39. {
  40. $variable = [];
  41. $trees = TemplateLibrary::query()->with(['templateLibraryRelation'])
  42. ->orderBy('sort', 'asc')
  43. ->whereNull('deleted_at')->get()->toArray();
  44. foreach ($trees as $key => $tree) {
  45. $trees[$key]['name'] = "{$tree['name']}#{$tree['id']}";
  46. $trees[$key]['title'] = $tree['name'];
  47. $trees[$key]['images'] = json_decode($tree['images'], true);
  48. }
  49. $trees = list_to_tree($trees, 'id', 'pid', 'children');
  50. return view('admin.stencil.template_library', [
  51. 'trees' => $trees,
  52. 'variable' => $variable,
  53. 'siteList' => Site::query()->where('is_stencil', 1)->get(),
  54. 'templateLibraryVar' => TemplateLibraryVar::query()->whereNull('deleted_at')->get(),
  55. ]);
  56. }
  57. /**
  58. * 添加模版库列表节点
  59. * @param Request $request
  60. * @return JsonResponse
  61. */
  62. public function templateLibraryAddNode(Request $request)
  63. {
  64. $result = $request->all();
  65. $update = [
  66. 'name' => $result['node'] ?? '',
  67. 'pid' => $result['pid'] ?? 0,
  68. ];
  69. TemplateLibrary::query()->insert($update);
  70. return response()->json(['message' => '添加成功']);
  71. }
  72. /**
  73. * 修改模版库列表节点
  74. * @param Request $request
  75. * @return JsonResponse
  76. */
  77. public function templateLibraryUpdateNode(Request $request)
  78. {
  79. $result = $request->all();
  80. $update = [
  81. 'name' => $result['node'] ?? '',
  82. 'pid' => $result['pid'] ?? 0,
  83. ];
  84. TemplateLibrary::query()->where('id', $result['id'])->update($update);
  85. return response()->json(['message' => '添加成功']);
  86. }
  87. /**
  88. * 删除模版库列表节点
  89. * @param Request $request
  90. * @return JsonResponse
  91. */
  92. public function templateLibraryDelNode(Request $request)
  93. {
  94. $result = $request->all();
  95. $update = [
  96. 'deleted_at' => date('Y-m-d H:i:s'),
  97. ];
  98. TemplateLibrary::query()->where('id', $result['id'])->update($update);
  99. return response()->json(['message' => '添加成功']);
  100. }
  101. /**
  102. * 复制模版库列表节点
  103. * @param Request $request
  104. * @return JsonResponse
  105. */
  106. public function templateLibraryCopyNode(Request $request)
  107. {
  108. $result = $request->all();
  109. $array = TemplateLibrary::query()
  110. ->whereNull('deleted_at')->get()->toArray();
  111. $parentList = TemplateLibrary::query()->where('id', $result['id'])
  112. ->whereNull('deleted_at')->first()->toArray();
  113. $childList = $this->getTemplateLibraryList($array, $result['id']);
  114. $childList[] = $parentList;
  115. $list = $this->arraySort($childList, 'id');
  116. $list = array_merge($list);
  117. $ids = [];
  118. foreach ($list as $key => $item) {
  119. if ($item['pid'] == 0 && $key == 0) {
  120. $update = [
  121. 'name' => $item['name'],
  122. 'alias' => $item['alias'],
  123. 'memo' => $item['memo'],
  124. 'sort' => $item['sort'],
  125. 'pid' => 0,
  126. 'css' => $item['css'],
  127. 'images' => $item['images'],
  128. 'created_at' => date('Y-m-d H:i:s'),
  129. ];
  130. } elseif ($item['pid'] != 0 && $key == 0) {
  131. $ids[$item['pid']] = $item['pid'];
  132. $update = [
  133. 'name' => $item['name'],
  134. 'alias' => $item['alias'],
  135. 'memo' => $item['memo'],
  136. 'sort' => $item['sort'],
  137. 'pid' => $ids[$item['pid']] ?? 0,
  138. 'css' => $item['css'],
  139. 'images' => $item['images'],
  140. 'created_at' => date('Y-m-d H:i:s'),
  141. ];
  142. } else {
  143. $update = [
  144. 'name' => $item['name'],
  145. 'alias' => $item['alias'],
  146. 'memo' => $item['memo'],
  147. 'sort' => $item['sort'],
  148. 'pid' => $ids[$item['pid']],
  149. 'css' => $item['css'],
  150. 'images' => $item['images'],
  151. 'created_at' => date('Y-m-d H:i:s'),
  152. ];
  153. }
  154. $id = TemplateLibrary::query()->insertGetId($update);
  155. $ids[$item['id']] = $id;
  156. }
  157. return response()->json(['message' => '添加成功']);
  158. }
  159. /**
  160. * 递归父级目录
  161. * @param $array
  162. * @param int $pid
  163. * @return array
  164. */
  165. public function getTemplateLibraryList($array, $pid = 0)
  166. {
  167. static $data = [];
  168. foreach ($array as $key => $item) {
  169. if ($item['pid'] == $pid) {
  170. $data[] = [
  171. 'id' => $item['id'],
  172. 'name' => "{$item['name']}",
  173. 'alias' => $item['alias'] ?? '',
  174. 'memo' => $item['memo'] ?? '',
  175. 'sort' => $item['sort'] ?? 0,
  176. 'pid' => $item['pid'] ?? 0,
  177. 'css' => $item['css'] ?? '',
  178. 'images' => $item['images'] ?? json_encode([]),
  179. ];
  180. unset($array[$key]);
  181. $this->getTemplateLibraryList($array, $item['id']);
  182. }
  183. }
  184. return $data;
  185. }
  186. /**
  187. * 添加顶级菜单
  188. * @param Request $request
  189. * @return JsonResponse
  190. */
  191. public function addParentMenu(Request $request)
  192. {
  193. $result = $request->all();
  194. $update = [
  195. 'name' => $result['name'],
  196. 'pid' => 0,
  197. ];
  198. TemplateLibrary::query()->insert($update);
  199. return response()->json(['message' => '添加成功']);
  200. }
  201. /**
  202. * 保存模版信息
  203. * @param Request $request
  204. * @return JsonResponse
  205. */
  206. public function saveTemplateLibrary(Request $request)
  207. {
  208. $result = $request->all();
  209. $update = [
  210. 'name' => $result['name'] ?? '',
  211. 'alias' => $result['alias'] ?? '',
  212. 'memo' => $result['memo'] ?? '',
  213. 'sort' => $result['sort'] ?? 0,
  214. 'pid' => $result['pid'] ?? '',
  215. 'css' => $result['css'] ?? '',
  216. 'images' => json_encode($result['images'] ?? []),
  217. ];
  218. try {
  219. //事务
  220. DB::transaction(function () use ($result, $update) {
  221. TemplateLibrary::query()->where('id', $result['template_id'])->update($update);
  222. if (!empty($result['variableIds'])) {
  223. $variableList = [];
  224. foreach ($result['variableIds'] as $item) {
  225. $variableList[] = [
  226. 'template_library_id' => $result['template_id'],
  227. 'variable_id' => $item,
  228. ];
  229. }
  230. TemplateLibraryRelation::query()->where('template_library_id', $result['template_id'])->delete();
  231. TemplateLibraryRelation::query()->insert($variableList);
  232. }
  233. });
  234. } catch (\Throwable $exception) {
  235. return response()->json(['message' => $exception->getMessage()], 400);
  236. }
  237. if (!empty($result['siteId'] && !empty($result['nodeIds']))) {
  238. $ids = [];
  239. $level = [];
  240. foreach ($result['nodeIds'] as $key => $id) {
  241. $res = explode(':', $id);
  242. $ids[] = $res[0];
  243. $level[$res[0]] = $res[1];
  244. }
  245. $templateLibraryList = TemplateLibrary::query()->whereIn('id', $ids)->get()->toArray();
  246. if (!empty($templateLibraryList)) {
  247. $templateLibraryResult = [];
  248. foreach ($templateLibraryList as $item) {
  249. $templateLibraryResult[] = [
  250. 'id' => $item['id'] ?? 0,
  251. 'name' => $item['name'] ?? '',
  252. 'alias' => $item['alias'] ?? '',
  253. 'description' => $item['memo'] ?? '',
  254. 'rank' => $item['rank'] ?? 0,
  255. 'html' => '',
  256. 'allow_write_file' => 0,
  257. 'allow_clear_cache' => 0,
  258. 'create_time' => time(),
  259. 'parent_id' => $item['pid'],
  260. 'level' => $level[$item['id']],
  261. 'images' => $item['images'] ?? json_encode([]),
  262. ];
  263. }
  264. //清空表
  265. DB::connection($this->templateLibraryApiService->connection($this->siteId))
  266. ->table('content_template')->truncate();
  267. //插入到项目数据库形成新的父子关系
  268. $this->loop($templateLibraryResult);
  269. $variableList = TemplateLibraryVar::query()->get()->toArray();
  270. if (!empty($variableList)) {
  271. $variableListResult = [];
  272. foreach ($variableList as $item) {
  273. $variableListResult[] = [
  274. 'name' => $item['variable'] ?? '',
  275. 'caption' => $item['tag'] ?? '',
  276. 'description' => $item['memo'] ?? '',
  277. 'input_type' => $item['input_type'] ?? '',
  278. 'input_value' => $item['input_value'] ?? '',
  279. 'input_opts' => $item['input_opts'] ?? '',
  280. 'input_length' => $item['input_length'] ?? 0,
  281. 'allow_blank' => $item['allow_blank'] ?? 0,
  282. 'regex_match' => $item['regex_match'] ?? '',
  283. 'regex_error' => $item['regex_error'] ?? '',
  284. 'rank' => $item['rank'] ?? 0,
  285. 'create_time' => $item['create_time'] ?? time(),
  286. 'width' => $item['width'] ?? 0,
  287. 'height' => $item['height'] ?? 0,
  288. 'size' => $item['size'] ?? 0,
  289. ];
  290. }
  291. DB::connection($this->templateLibraryApiService->connection($request['siteId']))->table('content_template_var')->truncate();
  292. DB::connection($this->templateLibraryApiService->connection($request['siteId']))
  293. ->table('content_template_var')->insert($variableListResult);
  294. }
  295. }
  296. }
  297. return response()->json(['message' => '保存成功']);
  298. }
  299. /**
  300. * 插入数据库形成新的父子关系
  301. * @param $array
  302. */
  303. public function loop($array)
  304. {
  305. $ids = [];
  306. foreach ($array as $key => $item) {
  307. if ($item['parent_id'] == 0 && $key == 0) {
  308. $update = [
  309. 'id' => $item['id'],
  310. 'name' => $item['name'],
  311. 'alias' => $item['alias'] ?? '',
  312. 'description' => $item['description'] ?? '',
  313. 'rank' => $item['rank'] ?? 0,
  314. 'html' => $item['html'] ?? 0,
  315. 'allow_write_file' => $item['allow_write_file'] ?? '',
  316. 'allow_clear_cache' => $item['allow_clear_cache'] ?? '',
  317. 'create_time' => $item['create_time'],
  318. 'parent_id' => 0,
  319. 'level' => $item['level'],
  320. 'images' => $item['images'],
  321. ];
  322. } elseif ($item['parent_id'] != 0 && $key == 0) {
  323. $ids[$item['parent_id']] = $item['parent_id'];
  324. $update = [
  325. 'id' => $item['id'],
  326. 'name' => $item['name'],
  327. 'alias' => $item['alias'] ?? '',
  328. 'description' => $item['description'] ?? '',
  329. 'rank' => $item['rank'] ?? 0,
  330. 'html' => $item['html'] ?? 0,
  331. 'allow_write_file' => $item['allow_write_file'] ?? '',
  332. 'allow_clear_cache' => $item['allow_clear_cache'] ?? '',
  333. 'create_time' => $item['create_time'],
  334. 'parent_id' => $ids[$item['parent_id']] ?? 0,
  335. 'level' => $item['level'],
  336. 'images' => $item['images'],
  337. ];
  338. } else {
  339. $update = [
  340. 'id' => $item['id'],
  341. 'name' => $item['name'],
  342. 'alias' => $item['alias'] ?? '',
  343. 'description' => $item['description'] ?? '',
  344. 'rank' => $item['rank'] ?? 0,
  345. 'html' => $item['html'] ?? 0,
  346. 'allow_write_file' => $item['allow_write_file'] ?? '',
  347. 'allow_clear_cache' => $item['allow_clear_cache'] ?? '',
  348. 'create_time' => $item['create_time'],
  349. 'parent_id' => $ids[$item['parent_id']],
  350. 'level' => $item['level'],
  351. 'images' => $item['images'],
  352. ];
  353. }
  354. $id = DB::connection($this->templateLibraryApiService->connection($this->siteId))
  355. ->table('content_template')->insertGetId($update);
  356. $ids[$item['id']] = $id;
  357. }
  358. }
  359. /**
  360. * 添加模版变量
  361. * @param Request $request
  362. * @param $id
  363. * @return Factory|JsonResponse|View
  364. */
  365. public function addVariable(Request $request, $id)
  366. {
  367. if (!$request->ajax()) {
  368. $info = TemplateLibraryVar::query()->where('id', $id)->first();
  369. return view('admin.stencil.add_variable', [
  370. 'id' => $id,
  371. 'info' => $info,
  372. ]);
  373. }
  374. $result = $request->all();
  375. $update = [
  376. 'variable' => $result['variable'] ?? '',
  377. 'tag' => $result['tag'] ?? '',
  378. 'sort' => $result['sort'] ?? 0,
  379. 'memo' => $result['memo'] ?? '',
  380. 'input_type' => $result['input_type'] ?? '',
  381. 'input_opts' => $result['input_opts'] ?? '',
  382. 'input_length' => $result['input_length'] ?? '',
  383. 'width' => $result['width'] ?? '',
  384. 'height' => $result['height'] ?? '',
  385. 'size' => $result['size'] ?? '',
  386. 'input_value' => $result['input_value'] ?? '',
  387. 'regex_match' => $result['regex_match'] ?? '',
  388. 'regex_error' => $result['regex_error'] ?? '',
  389. ];
  390. if (!empty($id)) {
  391. TemplateLibraryVar::query()->where('id', $id)->update($update);
  392. } else {
  393. TemplateLibraryVar::query()->insert($update);
  394. }
  395. return response()->json(['message' => '保存成功']);
  396. }
  397. /**
  398. * 删除模版变量
  399. * @param $id
  400. * @return JsonResponse
  401. */
  402. public function delDelVariable($id)
  403. {
  404. $update = [
  405. 'deleted_at' => date('Y-m-d H:i:s')
  406. ];
  407. TemplateLibraryVar::query()->where('id', $id)->update($update);
  408. return response()->json(['message' => '保存成功']);
  409. }
  410. }